How to Install Ruby 1.9.3 on CentOS 6.5


Problem

You want to install the same version of Ruby that Chef 11 uses on your CentOS 6.5 workstation. You don’t want to use rvm or another Ruby version manager.

Solution

Build and install Ruby from source. Automate the entire process using either Chef-Solo or Ansible.

Prerequisites

  • CentOS 6.5 (will probably work with other 6.x versions)
  • root account
  • Chef 11 workstation installed
  • Chef-solo configured.
  • Chef-solo cookbook created (e.g., ‘ruby-cookbook’ to hold the recipe)

Chef Source

The default.rb file is shown below. Put this is the recipes/ directory of your chef-colo cookbook. I purposely left the steps verbose so you could perform these manually if you wanted.

#############################
# Prepare the workstation
#############################

# Install compilers and libraries needed to build ruby
execute "yum groupinstall 'Development Tools'" do 
  action :run
end

packages = [
  "ruby",
  "ruby-devel",
  "ruby-irb",
  "ruby-libs",
  "ruby-rdoc",
  "rubygems"
]

# Remove the default Ruby 1.8.7
packages.each do |package|
    yum_package package do
        action :remove
    end
end

# Store source files here
source_directory = '/usr/local/src'

#############################################
# Install libyaml which is needed by gem
#############################################

# Check: http://pyyaml.org/download/libyaml for the latest version.
libyaml_version = 'yaml-0.1.5'

# Download libyaml source
remote_file "#{source_directory}/#{libyaml_version}.tar.gz" do
  source "http://pyyaml.org/download/libyaml/#{libyaml_version}.tar.gz"
end

# Extract the source
execute "tar xzvf #{libyaml_version}.tar.gz" do
  cwd source_directory 
  action :run
end

# Configure source for compiling
execute "./configure --prefix=/usr/local" do
  cwd "#{source_directory}/#{libyaml_version}" 
  action :run
end

# Compile libyaml
execute "make" do
  cwd "#{source_directory}/#{libyaml_version}" 
  action :run
end

# Now install it
execute "make install" do
  cwd "#{source_directory}/#{libyaml_version}" 
  action :run
end

# Delete the tarball. Keep the source just in case.
file "#{source_directory}/#{libyaml_version}.tar.gz" do
  action :delete
end

#############################################
# Install Ruby version 1.9.3
#############################################

# Check: https://www.ruby-lang.org/en/downloads/ for the latest version.
ruby_version = 'ruby-1.9.3-p484'

# Download Ruby source
remote_file "#{source_directory}/#{ruby_version}.tar.gz" do
  source "http://cache.ruby-lang.org/pub/ruby/1.9/#{ruby_version}.tar.gz"
end

# Extract the source
execute "tar xzvf #{ruby_version}.tar.gz" do
  cwd source_directory 
  action :run
end

# Configure source for compiling. Set libraries paths and skip doc install.
execute "./configure --prefix=/usr/local --disable-install-doc --with-opt-dir=/usr/local/lib" do
  cwd "#{source_directory}/#{ruby_version}" 
  action :run
end

# Compile it
execute "make" do
  cwd "#{source_directory}/#{ruby_version}" 
  action :run
end

# Now install it
execute "make install" do
  cwd "#{source_directory}/#{ruby_version}" 
  action :run
end

# Delete the tarball. Keep the source just in case.
file "#{source_directory}/#{ruby_version}.tar.gz" do
  action :delete
end

#################################################
# Test install
#
# ruby -v should show 'ruby-1.9.3-p484' or
# the same name you enter for ruby_version above.
# 
# If it doesn't then reboot and it should. I would
# add a reboot step here but I don't know how to 
# do this with Chef.
#################################################

#############################################
# Install gem and Chef related packages
#############################################

# Update gem
execute "gem update --system" do
  action :run
end

gems = [
  "rubocop",
  "foodcritic",
  "serverspec",
  "rpsec",
  "bundle",
  "rake",
  "berkshelf"
]

# Install useful Ruby gems for Chef
gems.each do |package|
    gem_package package do
        action :install
    end
end

# Reboot now if you hadn't before.

Ansible Source

I’m including this to show you how Ansible and Chef syntax differ. Running this only requires an inventory file. You do not need to create a directory structure like with Chef-Solo.

---

- hosts: localhost
  vars:
    source_directory: /usr/local/src
    libyaml_version: yaml-0.1.5 # Check: http://pyyaml.org/download/libyaml for the latest version.
    ruby_version: ruby-1.9.3-p484 # Check: https://www.ruby-lang.org/en/downloads/ for the latest version.
  remote_user: root
  connection: local
  tasks:

  #############################
  # Prepare the workstation
  #############################

  - name: Install compilers and libraries needed to build ruby
    yum: name="@Development Tools" state=present

  - name: Remove the default Ruby 1.8.7 packages
    yum: name={{ item }} state=removed
    with_items:
      - ruby
      - ruby-devel
      - ruby-irb
      - ruby-libs
      - ruby-rdoc
      - rubygems

  #############################################
  # Install libyaml which is needed by gem
  #############################################

  - name: Download libyaml source
    get_url: url=http://pyyaml.org/download/libyaml/{{ libyaml_version }.tar.gz 
             dest={{ source_directory }}/{{ libyaml_version }.tar.gz

  - name: Extract the source
    command: tar xzvf {{ libyaml_version }}.tar.gz chdir={{ source_directory }} 

  - name: Configure source for compiling
    command: ./configure --prefix=/usr/local chdir={{ source_directory }}/{{ libyaml_version }} 

  - name: Compile libyaml
    command: make chdir={{ source_directory }}/{{ libyaml_version }}

  - name: Now install it
    command: make install chdir={{ source_directory }}/{{ libyaml_version }}

  - name: Delete the tarball. Keep the source just in case.
    file: path={{ source_directory }}/{{ libyaml_version }}.tar.gz
          state=absent

  #############################################
  # Install Ruby version 1.9.3
  #############################################

  - name: Download libyaml source
    get_url: url=http://cache.ruby-lang.org/pub/ruby/1.9/{{ ruby_version }.tar.gz 
             dest={{ source_directory }}/{{ ruby_version }.tar.gz

  - name: Extract the source
    command: tar xzvf {{ ruby_version }}.tar.gz chdir={{ source_directory }} 

  - name: Configure source for compiling. Set libraries paths and skip doc install.
    command: ./configure --prefix=/usr/local --disable-install-doc --with-opt-dir=/usr/local/lib 
             chdir={{ source_directory }}/{{ ruby_version }} 

  - name: Compile Ruby a
    command: make chdir={{ source_directory }}/{{ ruby_version }}

  - name: Now install it
    command: make install chdir={{ source_directory }}/{{ ruby_version }}

  - name: Delete the tarball. Keep the source just in case.
    file: path={{ source_directory }}/{{ ruby_version }}.tar.gz
          state=absent

  #################################################
  # Test install
  #
  # ruby -v should show 'ruby-1.9.3-p484' or
  # the same name you enter for ruby_version above.
  # 
  # If it doesn't then reboot and it should. I would
  # add a reboot step here but I don't know how to 
  # do this with Chef.
  #################################################

  #############################################
  # Install gem and Chef related packages
  #############################################

  - name: Update gem
    command: gem update --system

  - name: Install useful Ruby gems for Chef
    gem: name={{ item }} state=latest include_dependencies=yes user_install=no
    with_items:
      - rubocop
      - foodcritic
      - serverspec
      - rpsec
      - bundle
      - rake
      - berkshelf

# Reboot now if you hadn't before.


Categories: DevOps, Software

Tags: , , ,

3 replies

  1. Hey awesome recipie! Thanks! I’ve been looking everywhere for this!

    One question though… it’s failing when installing Development Tools… not really an error it just says the following:

    Transaction Summary

    Install 6 Package(s)
    Upgrade 4 Package(s)

    Total download size: 27 M
    Is this ok [y/N]: Exiting on user Command
    STDERR:
    —- End output of yum groupinstall ‘Development Tools’ —-
    Ran yum groupinstall ‘Development Tools’ returned 1

    Any tips?

    Thanks!

    Like

    • Hello,

      I’m glad it was helpful. You have the correct command: yum groupinstall "Development Tools". I can’t tell from your error message where there is a problem. You could run yum grouplist and check under “Installed Groups” to see if Development Tools is listed. Is this error preventing the other steps from running?

      –Aaron

      Like

  2. Hey! Thanks for the quick reply!
    Yes, this error was haulting the deployment.

    I JUST fixed it by adding option ‘-y’ to the execute command. So now reads

    yum groupinstall -y "Development Tools"

    Everything ran fine after that =)

    Now I’m having an issue when updating gems with the –system option. I’ll post a solution if I can find one =)

    Thanks again!

    Like

Share Your Ideas

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: