Using Multiple Rubies Seamlessly On The One Machine With Rvm

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:

imageThe 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

  • http://rvm.beginrescueend.com Wayne E. Seguin

    Alan,

    A most excellent writeup, thank you very much for walking people through Netbeans usage with rvm. I will be linking a FAQ section on Netbeans to this article from the rvm documentation.

    For installation it should be noted that there are several methods to install, I prefer the direct from github one. They can be found here: http://rvm.beginrescueend.com/install/ Additionally, you can keep your rvm updated by using ‘rvm update –head’. (Note that rvm 0.1.0 changed the rubies location so uninstall & reinstall is a good idea for your rubies.)

    With the platforms, if someone wants to find out the values to put in those fields they can ‘rvm use X’ then do ‘rvm info’ and all the necessary information should be output to the terminal. Alternatively you can find the information necessary in each ruby’s wrapper script, 1.9.1-p378 environment information, for example, can be seen in ~/.rvm/bin/ruby-1.9.1-p378 .

    The documentation is continuously evolving, you can find the gems(ets) documentation here: http://rvm.beginrescueend.com/gemsets/#managing used to manage gems. So the issue you mentioned about gems being per interpreter installation can be mitigated from there using techniques like ‘rvm gems dump; rvm 1.8.7,1.9.1,ree gems load’.

    If you or anyone you know has questions or ideas for rvm you can find me in #rvm on irc.freenode.net during EST hours (and most EST nights).

    Please enjoy rvm!

    ~Wayne

    • http://www.skorks.com Alan Skorkin

      Thanks for the helpful hints Wayne and thanks for taking the time to create such a great tool, I am sure everyone who uses it appreciates it very much, I know I do :).

  • http://consoci8.com Fadhli Rahim

    Where were we without RVM eh?

    Regarding Netbeans can’t find your .rvm files. What I did to solve the problem was I’d created a symlink to the .rvm folder so that Netbeans can browse the .rvm folder through the symlink. The only thing is you have to click ‘Add Platform…’ instead of the ‘Autodetect Platform’ button.

    Hope this helps. Cheers. :-)

    • http://www.skorks.com Alan Skorkin

      Ah yeah that would work, why didn’t I think of that :). Thanks for the handy hint.

  • Pingback: crispycode » CC Links 13

  • jeff

    Netbeans still doesn’t seem to be running tests with the proper ruby version… I’ve added the 1.8.7 interpreter to NB, it’s marked as “default” in my rvm, yet when I run an ActiveSupport::TestCase in NB it outputs “RUBY_VERSION” as 1.8.6.

    Any ideas?

    • http://www.skorks.com Alan Skorkin

      I don’t tend to run my tests through netbeans much, I use the console. Having said that, when you create a project in Netbeans, you need to actually pick the Ruby version that it will use. If you created it before you added 1.8.7 to netbeans it will still be using the Ruby version it was using when you created it. You need to go and change the Ruby version that your project is using. I’ve been caught out with that a couple of times now.

  • Trouble

    I am wondering how did you manage to get gems working?

    I made a sym link, and added the Ruby (1.9) interpreter to it. NetBeans seemed to autodetect the rest, but the are some interesting quirks:

    In a project with the 1.9.1 interpreter selected
    Running it via (F6)
    puts `ruby –version` #=> prints that it is 1.8.7

    however, certain syntax limited to ruby 1.9.1 only works, like:
    proc = ->(a,b) {a+b} ; p proc.call(1,2) #=> 3
    hash = {one:1, two:2}; p hash[:one] #=> 1

    I have dozens of gems installed for 1.9.1, yet requiring any of them leads to an error that they cannot be loaded :/

    • http://www.skorks.com Alan Skorkin

      Hmm, I never had this kind of trouble, but like I said I don’t tend to run my tests or anything also through netbeans, I use it purely as an editor. I run all my tests etc. through the console.

    • Jay

      This is probably too late to help you, but I had a similar issue. You have to change the gem home path that Netbeans autodetects. In my case, the gems that RVM uses are located in ~/.rvm/gems/ruby-1.9/gems but Netbeans autodetects the path to be something like ~/.rvm/rubies/ruby-1.9.x/lib/ruby/gems/1.9.x/ which isn’t correct. However, if you try to change the gem path you run into a couple of problems.

      1) Netbeans doesn’t let you browse “hidden” directories (in this case .rvm). This can be resolved with a symlink (as mentioned above by another commenter), or you can type the path directly in the text input, and then press Open.

      2) Once you browse to the place where it seems the gems are stored (“~/.rvm/gems/ruby-1.9/gems”) you get an error from Netbeans about it being a non-empty existing directory, but isn’t a valid RubyGems repository. Drop the “gems” off the end of that path and Netbeans accepts the path, and all seems well (so far).

      Perhaps this will help others. :)

  • http://matpalm.com mat kelcey

    hey al,
    you’ll be happy to know i’m using this article to install rvm on my new dev box!
    ( but watch out; if anything goes wrong i’ll sic andrew onto you! )
    mat

    • http://www.skorks.com Alan Skorkin

      yay, I feel so useful :)

  • Pingback: C’è sempre qualche cosa che non va « Ra.Blog

  • http://mastrodonato.info Marco Mastrodonato

    Hello Alan,
    i tried your way to set netbeans, it was a pain but it works perfectly. With symlink i had some issues, it didn’t find gems… btw now it works, thank you!

    • http://www.skorks.com Alan Skorkin

      Hi Marco,

      No worries, glad you found it useful.

  • http://www.startbreakingfree.com Brian Armstrong

    Thanks this saved my bacon today!

  • Adam Tretkowski

    “if you need to work with multiple Rubies on windows, may god have mercy on your soul”

    Windows has PIK – http://github.com/vertiginous/pik
    I tried and I am happy :-)

    • http://www.skorks.com Alan Skorkin

      Hey Adam,

      Cheers for that – good to know.

  • http://www.mattslay.com Matt Slay

    Speaking of the NetBeans Platforms dialog box, I’ve always been confused by the “Gem Home” and “Gem Path” settings… What’s the difference in these?

  • http://ihelpxu.blogspot.com ihelpXu

    Looks like NetBeans (ver 6.9.1) now finds the different versions. All I did was “autodetect platforms” and it found all my RVM installed versions.

    • http://www.skorks.com Alan Skorkin

      Sweet, that’s good to know

    • stahlstift

      I can confirm this.
      Netbeans 6.9.1 finds automatically all my rvm installed platforms + the built in JRuby and the system default (1.8.7 mac os x)

  • Nico

    Anyone an idea how to ad gemsets per netbeans project?

  • Eric Proctor

    I’m not at home to try this yet, but just wondering if you could just create a ruby script and launch that…something like…

    #!/usr/bin/env ruby

    system(`rvm use 1.9.1`)
    # is this next line even necessary since I can call netbeans from the command line
    # and it starts netbeans (I think)
    system(`cd /path/to/netbeans/install/bin`)
    system(`./netbeans`)

    Then save it, make it executable with chmod +x and go to town. I may try when I go home tonight.

  • Eric Proctor

    Oooh… on the previous, I think we can do it with single quotes instead of backticks. My bad!

  • Pingback: Delicious Bookmarks for March 13th from 08:29 to 10:40 « Lâmôlabs

  • Marc

    Ruby platforms for Netbeans are defined in the following file:

    ~/.netbeans//build.properties

    So you can simply edit the gem paths and gem homes in there.

  • Sorin Popa

    Hello, Alan and everyone reading this! Thanks for this!

    I am using rvm (the simply amazing rvm – 1k1 thx, Wayne!) and Netbeans 6.9.1 on Ubuntu 12.10.
    I noticed that in Netbeans 6.9.1, for “Ruby Platforms”, the following happens..

    1) “Add platform…”

    a) allows you to dig in hidden folders (not sure if enabling this in Nautilus helped or not, but it’s still working after disabling hidden files view in Nautilus);

    b) selecting the ruby interpreter triggers the loading of all correct paths (including for gems home)

    2) “Browse…” for GEM_HOME does not allow digging the hidden folders.

    3) “Autodetect Platforms” does detect all rvm rubies, but with the wrong paths for gems, as the earlier posts were indicating.

    I hope this helps!
    Sorin