If you’re into Ruby and are not yet using RVM (ruby version manager) you’re doing yourself a disservice. It’s a great little tool that allows you to easily have multiple Ruby installs on the one machine and will manage all the associated complexity for you (@markmansour originally put me onto it). You can switch between different Ruby versions instantly and if you need to make sure that your code works with multiple Ruby versions (e.g. 1.8 and 1.9, or 1.8 and JRuby), then you will really, really love it. Well, I hope you’re excited, so lets get you set up with your very own RVM install, you do need Linux (I am using Ubuntu), so if you need to work with multiple Rubies on windows, may god have mercy on your soul.
Installing RVM And Multiple Rubies
Ok, first thing first, RVM is a ruby gem so you will need to have some sort of Ruby install on your system already. It is a bit of a pain, but a small price to pay for the blessing you’re about to receive. Setting up rvm is pretty simple.
Firstly, install the gem:
gem install rvm
Once that’s done, we need to add some hooks, RVM comes with a convenient script, but unless your gem bin directory is in your path (which it isn’t in my case) you will need to go to the rvm installation directory to run the script:
cd ~/.gem/ruby/1.8/gem/rvm-x.x.Y/ && scripts/install
Replace the x.x.Y with the rvm version. The last thing to do is to add an extra line into your .bashrc:
echo 'if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then source "$HOME/.rvm/scripts/rvm" ; fi' >> .bashrc
At this point your rvm install is good to go and you can forget about using your original system Ruby from now on, instead lets install some rvm managed Rubies. I am going to install Ruby Enterprise (i.e. ree – all the other Rubies follow the same pattern):
rvm install ree
You will need to wait for rvm to do it’s thing:
Installing Ruby Enterprise Edition from source to: /home/alan/.rvm/rubies/ree-1.8.7-2010.01 Downloading ruby-enterprise-1.8.7-2010.01, this may take a while depending on your connection... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 7295k 100 7295k 0 0 176k 0 0:00:41 0:00:41 --:--:-- 154k Extracting ruby-enterprise-1.8.7-2010.01 ... Installing ree-1.8.7-2010.01, this may take a while, depending on your cpu(s)... Installing rubygems dedicated to ree-1.8.7-2010.01... Installing rubygems for /home/alan/.rvm/rubies/ree-1.8.7-2010.01/bin/ruby Installation of rubygems ree-1.8.7-2010.01 completed successfully. Installing rake Installing gems for ree-1.8.7-2010.01. Installing rake Installation of gems for ree-1.8.7-2010.01 is complete.
To install other Rubies you can do the following:
rvm install 1.8.7
rvm install 1.9.1
rvm install jruby
The above will install the latest version of the Rubies that you specified. After you have finished, check that all your Ruby installations are there:
rvm list ree-1.8.7-2010.01 [ x86_64 ] ruby-1.8.7-p248 [ x86_64 ] ruby-1.9.1-p378 [ x86_64 ] system [ ]
As you can see, I have several Rubies installed, including a system one which is your original Ruby (the one under which rvm is installed), it is also the one that is currently used as the default Ruby installation by every shell that you open e.g.:
ruby –v ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
We can fix that however, lets say we want ree to be the default Ruby from now on, all we need is this:
rvm ree --default
rvm list => ree-1.8.7-2010.01 [ x86_64 ] ruby-1.8.7-p248 [ x86_64 ] ruby-1.9.1-p378 [ x86_64 ] => (default) ree-1.8.7-2010.01 [ x86_64 ] system [ ]
Now every shell we start will be using ree as it’s default Ruby:
rvm use default
ruby –v ruby 1.8.7 (2009-12-24 patchlevel 248) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2010.01
Pretty handy, but what if I want to quickly switch the Ruby version I am currently using in my shell. All you need to do is this:
rvm use 1.9.1
And, magically my shell is using a different ruby:
ruby –v ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]
This is really all you need to know to start using rvm, there are lots of other more advanced commands, but for regular day-to-day usage I haven’t really found a need for any of them.
A couple of points to remember. Firstly, every time you use rvm to install a new Ruby version, gem and rake will come for free, i.e. rvm will install them for you for that particular Ruby installation. This of course means that all rvm Ruby installations have their own set of gems, so if you have 30 gems installed in one Ruby install and want to try your app out on another, you will need to install all those gems again for that Ruby – makes sense. The rvm site has all the info you need to work effectively, so go forth and explore if you feel like you need to know more.
Getting All The Rubies To Work With My IDE (Netbeans)
So you’re working happily with your multiple Rubies in the shell and then you crack open Netbeans for some of the more complex Ruby editing and find that none of your rvm-managed Ruby installation are there and there doesn’t seem to be any way to get them into Netbeans. Normally you would go to Tools->Ruby Platforms under Netbeans to add new Ruby versions:
The easiest thing to do is to autodetect platforms, but this doesn’t seem to find any of your rvm managed Rubies. If you try to add platform manually you still run into trouble, rvm installs it’s Rubies in a sub-directory under .rvm in your home folder. Unfortunately Netbeans won’t let you dig into hidden directories to find Rubies – stupid.
The only way around it seems to be to launch Netbeans from a shell where the rvm-managed Ruby platform you want to add is the default Ruby. If you do that, then autodetect platforms seems to find the Ruby installation fine. So, to add our 1.9.1 Ruby install to Netbeans we do the following:
rvm use 1.9.1 cd /home/alan/programs/netbeans_6_8/bin ***or wherever your netbeans is installed ./netbeans
Once Netbeans opens go to Tools->Ruby Platforms and then press autodetect platforms, Netbeans should add Ruby 1.9.1 to it’s list. It’s a pain to do things this way when you have lots of Rubies you want to add to Netbeans, but that’s just the way it is. If you know of a better/faster way, then do share!
Well, there you go, we’re now set up with multiple Rubies in the shell and in our IDE, just imagine the fun we can have :).
For more tips and opinions on software development, process and people (and rubies :)) subscribe to skorks.com today.
Image by jaja_1985’s