Taking the helm of ruby with Ruby Version Manager

  • Kevin Faustino
Ruby

Ruby Version Manager) the name says it all. The sole purpose of this gem is to mange which version of ruby your system is currently utilizing. The days of creating symbolic links to different installations of ruby are over. With a single command, RVM will change the version of ruby and its gems for your entire user profile.

Developers using Mac OS X can start using ruby the moment they boot up their machine for the first time. In fact, the latest version of Mac OS X Snow Leopard ships with ruby 1.8.7 MRI patch level 77. The problem with system versions such as the one bundled with Mac OS X is that they stagnate, causing developers to miss out on the latest patches applied to ruby. For example. my current default version of ruby is 1.8.7 MRI patch level 248.

Besides the benefits of patches, why would a developer with a default version of ruby care about managing it? According to a recent report by New Relic, 63.5% of all deployments use ruby 1.8.6 MRI. This indicates that a large portion of the ruby community does not want to make the jump to higher version numbers. RVM provides you the tools to test, benchmark and switch your ruby versions with minimal effort. Before RVM, you would have had to compile which version of ruby you wanted, manage the symlink to it and update the PATH environment variable. If you wanted another version installed, you would have to link it to another name such as ruby19.

Installing RVM

Before installing RVM, make sure you have the latest Xcode developer tools installed as you will experience compilation issues with certain gems if you do not. Mongrel for example will not compile its native extensions without the latest Xcode developer tools.

To get started, you must first install the gem:

sudo gem install rvm

Once installed, initialize the RVM installation and follow all prompts:

rvm-install

Finally, install any version of ruby you would like to use, in this case the latest version of 1.8.7 MRI:

rvm install 1.8.7

Switching Between Ruby Versions

To switch between versions of ruby, pass the use action to RVM:

rvm use 1.9.1 
rvm use 1.9.1-p378

RVM allows you to set a default version via the –default flag to easily switch to the version of ruby you use most:

rvm 1.8.7 --default 
rvm default

If you ever feel the need to use the original system version of ruby, RVM allows switching via the system action:

rvm system

Installing Gems

All gems are installed in their own gem directory for each version of ruby. You would install gems with RVM the same way you do now, except you do not prefix sudo to the gem command. RVM runs under your user profile. By running sudo, you are telling the system to run as the root user which does not have your RVM setup.

Example, install the rails gem:

gem install rails

RVM also allows installation of gems on multiple versions of ruby:

rvm 1.8.7,1.9.1 gem install rails

RVM Configuration

To set common flags when compiling ruby gems, RVM allows settings to be placed in a .rvmrc file.

Here are the contents of my ~/.rvmrc file:

rvm_archflags="-arch x86_64"

MySQL Gem

Some gems require flags such as the MySQL gem. To install the MySQL gem make sure you have set rvm_archflags in your .rvmrc file.

gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

The Benefits

Besides the benefit of being able to experiment with any ruby interpreter and version, RVM has a few killer features as well. For example, RVM allows the execution of ruby programs and rake tasks across all of your ruby versions, which ensures backwards compatibility and future proofing of your ruby gems, applications, etc.

To run a ruby program against all installed ruby interpreters, use the ruby action:

rvm ruby hello_world.rb

As with most actions, the ruby action can be restricted to a few versions:

rvm ruby 1.8.7,jruby hello_word.rb

The feature I use most often is the ability to run your rake tasks across all interpreters. This is done via the rake action.

rvm rake spec

Running the above command runs all of your specs across the installed interpreters. Since running your specs is a common task, RVM provides a shortcut:

rvm specs

Finally, with minimal effort, RVM can help you learn how fast your code will run under each ruby version through the benchmark action.

rvm benchmark hello_world.rb

Ruby Version Manager is an amazing project by Wayne E. Seguin. It is an essential tool for gem authors and any ruby developer who has to maintain applications across multiple versions of ruby. I have been using RVM for over 6 months and it has made managing ruby a pleasure.


Comments

blog comments powered by Disqus