Multiple Ruby versions with RVM

Rails 3 is just around the corner. It is optimized for the better performance and superior features of Ruby 1.9, but it also plays nicely with version 1.8.7. Ruby 1.8.6, on the other hand, cannot run Rails 3. This might not be a problem for you at all. If you are on a Mac, you were probably faced with Ruby 1.8.7 last year at the latest, when the upgrade to Snow Leopard changed the built-in Ruby version from 1.8.6 to 1.8.7. Most applications built for 1.8.6 runs fine on 1.8.7 – but many servers and deployment environments still runs only 1.8.6.

For me, the situation is like this: My standard version of Ruby on my development machine is 1.8.7. I deploy to many different environments, some (the ones I have a degree of control over) runs 1.8.7 and others (such as Heroku and some customer’s servers) runs 1.8.6. I can test and code away happily only to get some ugly exceptions when I deploy because I have called count on an array instead of length. Further, I also want to play with the new features of Ruby 1.9 and run Rails 3 on it. And that’s not to mention that I also use alternative Ruby implementations such as JRuby for projects where I need to tap into Java libraries.

So that’s at least four different versions and implementations of Ruby I want to use at various times. I need to compile gems separately for each of them, and I don’t want them to clutter up my default and very functional Ruby 1.8.7 installation which I still use most of the time.

Ruby Version Manager (rvm) to the rescue! This very welcome open source project allows us to easily install, manage and switch between multiple Ruby versions and implementations with a single command. The software itself is also easy to install:


sudo gem install rvm
rvm-install

Once you have followed the instructions and pasted the line of script into the correct file and restarted (or sourced) your shell, I recommend that you upgrade to the edge version of rvm by running this command:


rvm update --head

This gives you access to the newest versions of the installation scripts, and that is needed if you for instance want to install JRuby:


rvm install jruby
rvm jruby

That second line is all you need to switch between ruby implementations – here we switch to JRuby. Note that this is not a system-wide change – the switch is done simply by changing a few environment variables in your current shell so ruby, rake and so on now refers to the JRuby versions.

One gotcha is that gem will installed the wrong place if you have the —user-install flag in your .gemrc file. The only current resolution I know is to remove the flag.

Never the less, I will highly recommend that you install Ruby Version Manager and play with different Ruby version and implementations – it has never been easier.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>