Installing Specific Ruby Version via Vagrant

If you’re using Vagrant for your local Rails development environment you’ll probably want to install a specific Ruby version to match your production environment. It’s pretty easy with chruby cookbook. Here’s how it might look if you want to use Ruby 2.0:

# Berksfile
site :opscode

cookbook 'apt'
cookbook 'chruby'


# Vagrantfile
require 'berkshelf/vagrant'

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "precise32"

  # Use Berksfile
  config.berkshelf.enabled = true

  # Enable provisioning with chef solo, specifying a cookbooks path, roles
  # path, and data_bags path (all relative to this Vagrantfile), and adding
  # some recipes and/or roles.
  config.vm.provision :chef_solo do |chef|
    chef.add_recipe 'apt'
    chef.add_recipe 'chruby::system'

    chef.json = {
      chruby' => {
        'rubies' => {
          '2.0.0-p247' => true,
          '1.9.3-p392' => false
        },
        'default' => '2.0.0-p247'
      }
    }
  end
end

The downside is that compiling it from source takes some time. However, you won’t be doing that often, so I guess you’ll be fine.

The alternative is to use apt-get to install Ruby packages for Ubuntu from Brightbox. That will be blazing fast.

Share on Twitter, Facebook
Prev Next