Working With Multiple Ruby Versions Has Never Been This Easy

The Ruby VM market is thriving, and we increasingly find ourselves spending the morning in JRuby and the afternoon in Ruby Enterprise Edition. Or Thursday using Leopard Ruby and Friday using Ruby 1.9. Or ... well, you get the idea. So we needed a lightweight tool that eliminates the cost (in both time and frustration) of switching between Ruby versions. We wanted something drop-dead simple. Meet the Ruby Switcher.

Ruby Switcher

Hello, Ruby Switcher

The Ruby Switcher debuted earlier this year as a means for toggling between a handful of pre-installed Ruby versions. And once you bit the bullet and manually installed those VMs, and assuming you installed them in the "right" location, the Ruby Switcher offered crazy-fast switching between those few Ruby versions. But we're passionate about continuous improvement and maintaining a sharp set of tools, so we recognized that switching Ruby versions was Step 2; Step 1 (i.e., installing those Ruby versions in the first place) needed a healthy dose of automation as well. Today's Ruby Switcher makes both steps insanely simple, and it adds support for more (and newer) Ruby versions as well.

Pick a Ruby, Any Ruby

Enough talk! Let's download the Ruby Switcher and get this party started.

  cd
  curl -O -L http://github.com/relevance/etc/raw/26ae85c2f6c7d2640a3c75d619ad7ab8fc1cc570/bash/ruby_switcher.sh
  echo "source ~/ruby_switcher.sh" >> .bash_profile
  source .bash_profile

That's it. Let's quickly verify that you have your platform's core developer tools installed, and then you'll be ready to put the Ruby Switcher to work.

Prerequisites - Compilers, Libraries, and Whatnot

If you're on OS X, you'll need to download and install Xcode. (If you just want to switch between Leopard Ruby and JRuby, you can skip this step. If you want to use any other versions of Ruby, you'll need Xcode.)

If you're using Ubuntu, use apt-get to grab a few essential packages:

  sudo apt-get update
  sudo apt-get install build-essential zlib1g-dev libreadline5-dev libssl-dev

For other *nix variants ... well, clearly you enjoy figuring this stuff out.

Ruby 1.9

Yehuda Katz recently asked the Ruby community what we need in order adopt Ruby 1.9. Having an easy way to experiment with Ruby 1.9 is surely a good place to start. What could be easier than a single command?

  install_ruby_191

The installation will take a few minutes. In the meantime, why not check out Bruce Williams' slides on Ruby 1.9 from last year's Lone Star Ruby Conference — Ruby 1.9: What's New and Why it Matters?

  ...
  checking build system type... i686-pc-linux-gnu
  checking host system type... i686-pc-linux-gnu
  checking target system type... i686-pc-linux-gnu
  checking for gcc... gcc
  ...
  installing rdoc
  Using ruby 1.9.1p129 (2009-05-12 revision 23412) [i686-linux]
  Successfully installed rake-0.8.7
  1 gem installed

By the time you're done reading through the Ruby 1.9 highlights, you'll be ready to take it for a spin. Type ruby -v to verify that you're rockin' with 1.9.1.

  ruby 1.9.1p129 (2009-05-12 revision 23412) [i686-linux]

JRuby

Installing and using JRuby is just as easy.

First, be sure that you have the JDK installed. If you're using OS X, you're all set. If you're on Ubuntu, you can install it with apt-get. (For other Linux distros, you can ask your native package manager for the JDK bits.)

  sudo apt-get update
  sudo apt-get install sun-java6-jre sun-java6-jdk

With the JDK in place, installing JRuby is a cinch.

  install_jruby

Verify the results with ruby -v.

  jruby 1.3.1 (ruby 1.8.6p287) (2009-06-15 2fd6c3d) (Java HotSpot(TM) Client VM 1.6.0_10) [i386-java]

Wait a minute: shouldn't we have typed jruby -v instead of ruby -v? Sure you can do that if you really want to. But we're not big on having to remember to type jirb instead of irb, or jgem instead of just gem, etc. Once we tell the shell to use JRuby, we want the normal Ruby commands to just work. The Ruby Switcher ensures that they do.

Ruby.*

Looking for yet another Ruby version? See what else the Ruby Switcher has to offer:

  • install_ree_186
  • install_ruby_186
  • install_ruby_187
  • install_jruby_120

And if you find yourself wanting some other Ruby variant, be sure to check out the Ruby Switcher's internals; you can likely adapt one of the existing installation functions to meet your exact need.

Willy-nilly Switching

Once you have the desired Ruby versions installed, switching between them couldn't be easier. Each of the install commands comes with a corresponding use command to instruct your shell to switch to the specified Ruby version:

Install It Use It
install_ruby_191 use_ruby_191
install_ruby_186 use_ruby_186
install_ruby_187 use_ruby_187
install_jruby use_jruby
install_jruby_120 use_jruby_120
install_ree_186 use_ree_186
N/A [1] use_leopard_ruby

And these commands are shell-specific! So while one terminal window is using Ruby 1.8.7 to run your front-end Rails app, you can have another terminal using JRuby to run your back-end messaging code.

Ruby 1.9, the Community, and You

Want to see whether your app runs on Ruby 1.9? Flip your shell to use_ruby_191 and try it out. It's that kind of experimentation that will allow the Ruby community to make the migration to Ruby 1.9.

See a problem with a certain gem on Ruby 1.9? Let the gem author know about it and chime in at isitruby19.com. Better yet, write a test that proves the bug and pass that test along to the author. Better still, fix the issue and submit a patch. Any one of these steps moves us forward, and all the while you've still got your default Ruby installation standing safely by for your day job.

When switching between Ruby versions is this seamless, there's no reason not to experiment.

Notes

[1] Ruby is installed by default on OS X Leopard, so there's no need for an install_leopard_ruby command.

Image courtesy of bdu (flickr.com/bdu). [Creative Commons License]