Install multiple versions of Ruby with RVM on *nix environments

As a curious Ruby developer, you might have to deal with scenarios where:

  • you have several projects and each of them works with a different Ruby version,

  • you have several projects that each support different versions of the same Gem,

  • you want to test how your Rails project behaves with the latest update to the language,

  • you want to test a different interpreter, like JRuby or IronRuby

RVM was craeted thinking on help developers to cover all those scenarios (and even more).

Install RVM

The first thing you will need to do is install the mpapis public key (mpapis is the username of Michal Papis, RVM main developer):

$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

You will need to install gpg2 package to run the previous command (if you're on Mac, you can install it using Homebrew).

Once the public key is installed in your computer, you can install RVM:

curl -sSL https://get.rvm.io | bash -s stable --ruby

If you are not familiar with the command line, the cURL command gets content from a given URL, and options passed are:

  • -s: Don't output anything in the command line terminal
  • -S: Don't show any error
  • -L: If the server reports that the requested page has moved to a different location, this option will make curl redo the request on the new place.

Once the command finishes, you will need to add the follow to your .bash_profile or .bashrc:

[ [ -s "$HOME/.rvm/scripts/rvm" ] ] && source "$HOME/.rvm/scripts/rvm"

The previous line loads RVM into a shell session.

Install Rubies

The primary RVM use is to have multiple Ruby version installed in the same machine.  With the following command, you can list the available Ruby versions to install:

rvm list known

From the list, identified the Ruby version you needed (i.e. 1.9.3) and then install it:

rvm install 1.9.3

And that's it! Know you have installed Ruby 1.9.3 in your machine, but you can continue installing more version as you needed.  For example, to have the latest stable version, 2.2, just enter:

rvm install 2.2

and it will install that version in your machine.

To switch between different version, you can use the following command:

rvm use RUBY_VERSION

So, if you want to switch from version 1.9.3 to 2.2, you just need to type:

rvm use 2.2

and the command line will notify you that you are currenty using Ruby version 2.2.

I hope you find this useful: these are the baby steps to start with RVM, and I think they a good introduction to the tool.  Of course, you will find more detailed information of what RVM capable of in its official web site.