RVM : Set The Controls For The Heart Of Ruby

Many ruby library versions work better with given ruby version. This includes rails versions as well. Another thing is that there are more than one implementation of ruby, for example jruby. If you want to easily switch between implementations and versions you will have to use a dedicated tool. It is easy to install ruby using homebrew or apt-get, but it is not so easy to manage its versions and implementations. Enter RVM – the Ruby Version Manager.

We will begin by installing RVM using our favorite HTTP client – cURL.

curl -L https://get.rvm.io | bash
rvm requirements

Now if you use mac/linux/unix you will have RVM installed. If you use windows (shame on you, and you call yourself a developer) you can’t use RVM (maybe you can using cygwin, who knows), try using PIK instead. By the way there are quite a few installation options on the RVM home site.
The second line may output some instruction you must do before using RVM.

Now we can list our ruby versions using:

rvm list

RVM will tell us that we have no rubies installed. We can check out the available rubies running:

rvm list known

We will see MRI rubies (these are the Matz’s rubies – the C rubies), Jrubies, IronRubies, etc… We are able to install not only different versions but different patches of versions as well. I’m going to install the latest path of ruby 1.93.

rvm install ruby-1.9.3

This will download the ruby source and will compile it. Now if we type

rvm list

We will see our new ruby. With

rvm use ruby-1.9.3

we are already using it. The list command has a legend. We can see that there is something called default ruby. This is the ruby that will be used in all your sessions. Let we set 1.9.3 as our default ruby:

rvm use --default 1.9.3

We have only one ruby now… Let us install one more, ruby 2.0.0, patch 195:

echo progress-bar >> ~/.curlrc
rvm install ruby-2.0.0-p195 -j 3

The first command will add a progress bar when your are downloading a ruby. My machine has four cores and the -j options tells RVM to compile the newly downloaded ruby using 3 of them.
Now we have two ruby versions and we can see them in detail running the ‘rvm list’ command, we can switch between them using the ‘rvm use <ruby-version[-patch]>’ command. The important thing is that if we install gems (these are ruby libraries), they will be installed only for the current ruby. For example let us install rails for ruby 2.0.0:

rvm use 2.0.0
gem install rails

With the first command I want to show you that ‘use’ works only with versions as well (for MRI rubies). The second command installs the rails gem for the current ruby2.0.0. This will install many gems, because Rails has many dependencies (you can read some manga, while waiting).

Now when I run

rails -v

the output is Rails 3.2.13. But if I switch rubies and run it again:

rvm use 1.9.3
rails -v

I have an error. Rails is not installed. So I installed rails only for ruby 2.0.0 and if I want to install rails for 1.9.3, I will have to select ruby 1.9.3 and install it there.

But what if I want to live on the edge! What if I want to use ruby 2.0.0 with rails 4. I don’t want to remove rails 3.2 from ruby 2.0.0, because I am working on some project with it. How can I switch between different versions of one library for a given ruby, how can I have different set of libraries, different gem sets. RVM has gemsets. These are independent spaces filled with gems, they are unlimited for any ruby version. We can see all gemsets for all ruby versions by running:

rvm list gemsets

The output for me is:

rvm gemsets

=> ruby-1.9.3-p429 [ x86_64 ]
   ruby-1.9.3-p429@global [ x86_64 ]
   ruby-2.0.0-p195 [ x86_64 ]
   ruby-2.0.0-p195@global [ x86_64 ]

All ruby versions have a default gemset, the one that is used without specifying gemsets and a global gemset, all the gems in the global gemset are accessible by all other gemsets for the given ruby. Now we are going to create a new gemset for ruby 2.0.0-p195 called rails4. It will contain the Rails 4 gem. Run the following:

rvm use 2.0.0
rvm gemset create rails4
rvm gemset use rails4
rvm gemset list

Now we are using ruby 2.0.0, we’ve created a new gemset called rais4 with the second line, with the third line we told RVM to use the newly created gemset and the last command lists the gemsets of the current ruby. The output of the last command looks like this:

gemsets for ruby-2.0.0-p195 (found in /home/meddle/.rvm/gems/ruby-2.0.0-p195)
=> rails4

We can also choose to use a given ruby with a given gemset using ‘rvm use <ruby>@<gemset>’.
OK we are ready to install Rails 4:

gem install rails --version 4.0.0.beta1 --no-ri --no-rdoc

OK, I showed you how to use RVM, how to manage rubies and gemsets. All that is left is to show you a few more useful commands and to call it a post.

  1. You can remove a gemsets using:
    rvm gemset delete <gemset>
  2. You can use the ruby installed on your system (without RVM through for example apt-get) using:
    rvm system
  3. You can update RVM using:
    rvm get stable
  4. You can remove RVM and all the rubies installed with it using:
     rvm implode

So this is it. And one more thing… From this post on, I’m going to have an useful links section at the end of all posts:

Useful Links:

  1. RVM site – https://rvm.io
  2. Ruby Toolbox’s page about Ruby Version Managers – https://www.ruby-toolbox.com/categories/ruby_version_management
  3. Cool post about RVM and Rbenv – http://jonathan-jackson.net/rvm-and-rbenv
  4. My post about cURL link

Now today is 29-th may, the birthday of my dear friend and ex-colleague Icata Banchev, so Happy Birtchday ICO!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s