How to Manage Ruby Installations Easily with RVM

Those unfamiliar with software development may be surprised at how many so-called grey areas there can be. Even if you know you want to write, say, a Rails app, you’ve got several more specific decisions to make. What database should you use? Which version of Ruby? Which gems should be installed? Will it conflict with any other Ruby versions or gem sets? RVM, the Ruby Version Manager, can take care of that. RVM allows you to install, manage, and switch between Ruby versions on the fly so you can develop and test software in multiple Ruby versions with no hassle. This guide will show you how to install RVM on Ubuntu and use it to manage your Ruby environment.

Note: This is not a “How To Code Ruby” article, it deals only with using RVM to manage your Ruby installations.

How it Works

The most common RVM setup goes something like this – A user installs RVM as an application, either system-wide or within their home. Through RVM, the user’s chosen Ruby versions are installed automatically into that user’s home. Using RVM commands, the user specifies which version they’d like to have active at any given time. All calls made to Ruby during that time are routed to the appropriate Ruby executable and run as normal. There’s no virtualization or complex wrappers or chroots to worry about, it acts more-or-less like a clever series of symlinks and environment variables that direct all Ruby calls to the chosen version.

Installation

First we need a few prerequisites. Your standard Ubuntu installation should contain much of what’s needed, but some needed packages may be missing. To build RVM properly, install the following packages from Ubuntu Software Center or from the command line with:

sudo apt-get install git build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev

This readies our system to properly build and install RVM and Ruby. We won’t need to do the usual configure/make routine on this one as RVM actually includes a simple build script which should do the work for you. Once all the above dependencies are installed, run the following from the command line:

bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

This will fetch the source and deliver a nice message from RVM’s developer.

rvm-installationscript

Adding a Ruby Version

At this point you’ve got RVM but no usable Ruby install to run with it. Getting RVM to install one is as simple as a single shell command. For the first example, we’ll install the 1.8.7 stable version from source with one RVM command:

rvm install 1.8.7

rvm-install187

RVM will handle the entire process including configuration, compilation and the installation of RubyGems.

rvm-install187-2

Post-Install

Once complete, there’s a little bit of environment setup we’ve got to do. For RVM to be able to properly intercept Ruby calls, we’ve got to add the following line to .bashrc or .bash_profile:

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

Restart your shell and it should take effect. To test, just enter

type rvm | head -1

Which should respond with “rvm is a function”. If not, something’s broken along the line.

Managing Ruby Versions

Now that everything is set up, we can actually use RVM! Assuming you’ve installed version 1.8.7 as in the example above, it can be selected with

rvm use 1.8.7

RVM should give you a response confirming your choice and showing you the location of the Ruby executable. This can be verified later using the standard system which command.

rvm-switch

Additional versions can be installed the same way, and gems can be added and removed from RVM versions exactly as they would in a normal system Ruby install. You can pick your version on the fly to be sure your app works as intended without ever worrying about conflicts and environment mismatches. Enjoy!