Ruby versioning with Ruby Version Manager (RVM)

Lorna Tumuhairwe
The Andela Way
Published in
3 min readDec 21, 2017

If you are not very experienced with Ruby Version Manager (RVM) and are into exploring different rails applications, cloning people’s github repositories and reading through their code, you might have encountered situations where your Ruby or Rails versions clash. For example, what happens when one project requires a lower version of Ruby than the ruby version installed on your computer? Do you downgrade the Ruby version installed? Maybe. But what if you install another ruby project, that requires a newer version? You may get caught up in the loop of constantly upgrading and downgrading. RVM is the answer in such situations.

RVM is a command-line tool which allows you to easily install, manage, and work with multiple Ruby environments. It helps you maintain different versions of Ruby and all your other gemsets separate from the system defaults and other version defaults.

A gem is simply how libraries are called in Ruby and a gemset is a compartmentalized independent Ruby setup containing its own gems.

RVM usage

Let’s stop talking about it and get to it already, but first, check your version of Ruby by running:

ruby -v

To install another Ruby version, let’s follow these steps.

Step 1

Install rvm. I’ll refer you to the RVM installation instructions at this point to get the proper instructions for your OS. The steps after this assume that RVM is installed.

Step 2

Install another Ruby version using RVM by running:

rvm install 2.4.2

Where 2.4.2 is the version of Ruby you want to install and you can also install as many Ruby versions as you like using the same method.

NOTE: While trying to install an older version of Ruby on a macOS high sierra, you may encounter this error.

ERROR: While executing gem … (Gem::Exception) Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources error. 

In this case, install openssl

brew install openssl

Or run this command if openssl is already installed

rvm reinstall 2.3.0 --with-openssl-dir=`brew --prefix openssl`

Step 3

You can now list the versions of Ruby that are installed by the command below:

rvm list

The output of the above command should look similar to the block below.

=* ruby-2.1.1 [ x86_64 ]
ruby-2.3.1 [ x86_64 ]
ruby-2.4.1 [ x86_64 ]
ruby-2.4.2 [ x86_64 ]
# => — current
# =* — current && default
# * — default

To use any of the versions, simply type rvm <version> or rvm use <version> and to know which Ruby you are currently using type the command below.

ruby -v

You should now see the Ruby version you chose previously.

Managing multiple rails versions using RVM

You can then install Ruby on Rails(rails) in the current Ruby version using the command below.

gem install rails -v 4.0.3

So far you are able to have different Ruby versions having one rails version each. But what if you need to have a project using a different version of rails but similar Ruby version? You will need to create a different gemset for each setup.

Gemsets

To create new gemsets run the command below:

rvm gemset create rails503 rails320

This creates two gemsets with different names rails503and rails320

To display the gemsets associated with a Ruby version, type the command below.

rvm gemset list

and to use any of the gemsets run,

rvm <ruby-version>@rails503

You can now install rails inside this gemset by running the command below.

gem install rails -v 4.0.3

Switching gemsets

In case you want to use a particular gemset associated with the Ruby version that is currently in use, run the command below.

rvm gemset use rails320

To know the name of the gemset you are currently using, simply run the command below.

rvm gemset name

The to list all gemsets for all the Ruby versions installed run,

rvm gemset list_all

If you don’t use a gemset at all, you get the gems in the ‘default’ set. If you use a specific gemset (say @rails320), it will inherit gems from that Ruby’s @global. The ‘global’ gemset is to allow you to share gems to all your gemsets. The ‘default’ gemset is used when no specific version is installed.

Happy Ruby versioning!!

--

--