Railsmagazine60x60 RVM – The Ruby Version Manager

by Markus Dreier

Issue: Vol 2, Issue 1 - All Stuff, No Fluff

published in June 2010

Introduction

Do you know these situations? Perhaps you broke your system's Ruby version, or perhaps you want to try out the newest Ruby version, or perhaps you feel nostalgic and want to use an older Ruby? However, you also want it to be very simple to all of these things, right?! You don't want to have to fight with compiling every single version by hand, worrying about where to install each different interpreter version to, worry about name prefixes and suffix's so that they don't overwrite one another, etc.

In this article I will introduce you to rvm, the Ruby Version Manager (RVM). RVM is a command line tool written by Wayne E. Seguin which enables you to handle multiple ruby interpreter environments, specifying everything from ruby interpreter to sets of gems.

A Brief History

Before we start on how to use RVM, we go back through the time, way back and see how the idea for RVM was born. It was on August 21st 2009 when Wayne E. Seguin and his co-worker Jim Lindley had a situation in which they needed to easily switch between using three ruby interpreters and to deploy the different applications to production with all of them using their specific interpreters and gems. Additionally they needed the ability to easily and repeatably install the interpreters and gems consistently.

Wayne E. Seguin went home and until the next day he had written a basic tool in only 300 lines of shell scripting. The development of RVM still continues at a fast pace and is updated daily by him. Today, rvm contains approximately 4100 lines of code. 

Installing RVM

Now let us see how to use RVM and what we can do with it. RVM works according to Wayne on all *nix systems, so if you're a Linux/MacOSX or FreeBSD user, fire up your console and lets get started with installing rvm. The recommended way from the developer itself is to install it from the GitHub repository with the following command:

$ mkdir -p ~/.rvm/src/

$ cd ~/.rvm/src && rm -rf ./rvm/

$ git clone git://github.com/wayneeseguin/rvm.git

$ cd rvm

$ ./install

(Note: it is assumed that you have git installed, if you do not head on over to http://git-scm.com/ to download and install git.) This might take a while depending on your internet connection. You should now have RVM installed as your user, so lets get to the next step.

Before start installing our rubies and gems make sure, if we have installed earlier, that we update RVM to the latest version with the following command:

$ rvm update –head

Be sure to read and follow all of the instructions emitted by the installation line above. Be sure to activate RVM for new shells by placing the line

'if [[ -s $HOME/.rvm/scripts/rvm ]] ; then source

$HOME/.rvm/scripts/rvm ; fi'

at the end of your ~/.bash_profile and ~/.bashrc file, and ensuring that there is not a line ending with '&& return' in your ~/.bashrc. 

Installing rubies

Perfect, we now have the newest version of RVM installed. Now for the part we really need for developing our own applications with Ruby and all of it's possibilities. We can choose now out of many possibilities of ruby interpreters we want installed. The most commonly used is installing a specific patch level, which is the default. Let's install three ruby interpreters by specifying their versions (MRI ruby is default interpreter):

$ rvm install 1.8.6,1.8.7,1.9.1

After running this command (and waiting for a while, depending on CPU speed and network bandwidth) We should find that we have 3 ruby interpreters installed for each of the latest patch versions. RVM obtains the default patch levels are specified to rvm in the 'key=value' flat file ~/.rvm/config/db, these settings can be overwritten by the user in ~/.rvm/config/user.

To see the rubies we have instealled we simply type: rvm list to which we should now see something similar to:

rvm Rubies

ruby-1.8.6-p398 [ x86_64 ]

ruby-1.8.7-p249 [ x86_64 ]

ruby-1.9.1-p378 [ x86_64 ]

System Ruby

system [ ] 

Selecting Rubies

If we wish to use Ruby 1.8.6, we simply select this in our current shell by typing rvm 1.8.6. We can then verify that we have the correct ruby by typing ruby -v and we can also verify that the entire environment is correct by typing rvm info. RVM operates on a per-shell basis so this environment is only active for the current shell, if we open a new shell then we will be back to the system environment, which brings us to... 

Setting a Default Ruby

If we want to use a specific ruby as a default for all newly opened shells, say for example 1.9.1, we set the default by typing: rvm 1.9.1 --default. Then when we do rvm list we now see:

rvm Rubies

ruby-1.8.6-p398 [ x86_64 ]

ruby-1.8.7-p249 [ x86_64 ]

=> ruby-1.9.1-p378 [ x86_64 ]

Default Ruby (for new shells)

ruby-1.9.1-p378 [ x86_64 ]

System Ruby

system [ ]

Now every time we open a new shell we will find ruby -v to be the RVM installed 1.9.1 and gem list to be the installed gems for RVM's 1.9.1 interpreter.

Ruby Gems

This now brings us to installing gems, those little bundles of joy that we need so badly in order to produce our magnificent code! After selecting a ruby version with rvm 1.9.1, we can install gems using the standard gem install <gem name> (notice, no sudo!). RVM sets up your environment such that gems install to a separate directory for each distinct ruby interpreter. This means that we must install the gems forevery installed Ruby interpreter that we wish to the gem with. RVM provides an easy way to install a single gem to multiple interpreters: rvm 1.8.6,1.8.7 gem install ruby-debug will install ruby-debug to both 1.8.6 and 1.8.7, while rvm 1.9.1 gem install ruby- debug19 will install ruby-debug19 to only RVM's 1.9.1 ruby. To install a gem to all interpreters simply omit the selectors: rvm gem install shoulda.

sudo(n't)

It is very important to kick the habit of using 'sudo' to install gems. When sudo gem install X is used gem install X runs as the root user with root's environment setup and not RVM's carefully constructed environment.

Summary

This is a very brief tutorial that only scratches the surface. For further information and more detailed documentation, visit RVM's website (http://rvm.beginrescueend.com/). If you have any questions and/or issues regarding RVM, please visit the #rvm channel on rc.freenode.net. Wayne E. Seguin (wayneeseguin) is active there whenever he is conscious and will usually answer your query immediately. If he doesn't just hang out in the room as he answers you when he returns. This is how Wayne continues the development of RVM: close contact to the users and intensework on their problems to make RVM a better solution.

Hope you've found this article interesting. In the next episode we will start building an application from scratch.