seawolfsanctuary

home :: blog :: rss feed :: identi.ca / twitter :: diaspora* :: last.fm :: rss

RVM: Ruby Projects in a World of their Own

March
19

RVM LogoWhether you’re just starting to use Ruby or have been for a while, when you pull in the first Gem you had better be using RVM, the Ruby Version Manager. Why? Well, RVM allows you to easily set-up not only multiple versions of Ruby but also per-project sets of Gems, and for you to seamlessly switch between them. By having a number of persistent Rubies on your system simultaneously, each project lives safely in it’s own world so you can reliably separate and distribute them without leaving anything out. All this can be done away from your system’s own Ruby version and Gem set, too.

Approach

The common approach is to set up the Rubies inside your own Home directory, under the .rvm directory, but I’ve had no problems with a system-wide installation. (Except, that is, for remembering that my Rubies are installed under /usr/local and not in my Home directory, so needing elevated privileges to use RVM or some user-group work-around.) A quick round-up of the commands and methods for setting up and using system-wide RVM is the purpose of this post.

Bear in mind that Ruby is cross-platform and so all the tools around it have to be too. That’s not to say there are problems with it, just something to keep in mind that any stumbling block may be a Mac OS / Linux / Windows thing rather than the tool you’re having problems with.

Installation

We begin by, you guessed it, installing RVM. The easy way is by its script, which is downloaded and ran:

curl -k https://rvm.beginrescueend.com/install/rvm > ~/rvm_install.sh
chmod +x ~/rvm_install.sh
./~/rvm_install.sh

I’m using SliTaz Linux, whose user management tools are slightly different from those in a number of other distributions because of the glorious (yes, sarcasm) BusyBox. Because of this, the RVM user group automation doesn’t work, so I need to run:

sudo addgroup seawolf rvm

to add my user to the rvm group.

Like all great software, RVM’s user-level configuration is found in one file. Let’s take a look:

cat /usr/local/rvm/examples/rvmrc

I’m not going to go over this configuration as the defaults should be more than fine, and you can read a lot more than I know at the RVM website.

Speaking of documentation, you should really take a look at your platform-specific bugs and notices, either from the website or with the command:

rvm notes

I had a few gotchas in learning RVM but nothing huge; there’s plenty around the web and I’ve heard the IRC channel is particularly excellent for support (and chat, Wayne’s a nice guy).

Using RVM

With the paperwork out of the way, it’s the moment you’ve been waiting for: installing Ruby! You are in fact coming to download, compile and install a Ruby with one, yes one, command. This one, to be precise:

rvm install 1.8.7

Whether or not this actually works is a matter for your system, but that’s how it done. I’m not going to write the standard software compilation blurb, so any problems: you know what to do! I like to completely overwrite a failed compile/install with it’s original source before attempting it again, so this time instead run:

rvm install 1.8.7 --force

Lovely. Now, the fun bit: let’s do a similar thing with another Ruby, say the latest Ruby Enterprise Edition:

rvm install ree

Whoo! You’ve now your system’s own Ruby (whatever that may be), 1.8.7 and even REE installed and ready to go, all in one system! But how can you actually use them? Simple:

rvm list

You should see proof of multiple Rubies. Pick any one of them, for example:

rvm use 1.8.7

Note there’s one unlisted; ‘system’ is whatever Ruby your system has installed, usually by your package manager.

RVM & RubyGems

So, now that you know how to switch between them, you can install some Gems for a particular Ruby. Gems are as usual installed under Ruby’s path, this doesn’t change with RVM. By switching with ‘use’ you also switch to the Ruby’s Gems. Let’s have a look to see which Gems are installed with your ‘used’ Ruby. What would you normally do? Probably this:

gem list

RVM takes control over Gems since it’s all part of one particular Ruby by keeping everything under the .rvm directory inside your home. Every Ruby, version and gem lives in there. Have a go at installing some Gem you like (say, Bundler) with:

gem install bundler

To illustrate the Gem handling, let’s switch back to the system Ruby with:

rvm use system

Now, if you ask which Gems are available now, you should get back your original list:

gem list

You should see your previous gems listed there and not whatever one you just installed. Switch back and watch it change again!

Using RVM with Multiple Projects

Long-time or occasional developers may have projects written using various versions of Ruby, to the point that each project is different. There is a handy way to remember which should be used, and that is to let RVM automagically switch between them for you! It will do this when you change into the project’s root directory by noticing a ‘.rvmrc’ file, with the contents as:

rvm <version>@<project-name> --create

You may see what this file is really doing: these instructions are carried out by RVM each time we change into a directory and the file is detected, so it is automatically switching to a project-centric gemset each time it loads this file. I imagine other RVM commands can be used here too, should your workflow deem it necessary. It’s great practice to use this file as soon as you create a new project, so it’s not mixed-up with anything else, as in a similar workflow to Bundler. The two happen to work very well together, as you’d suspect.

Thanks Wayne!

So, there we have multiple versions of Ruby existing on the same machine at the system level, and numerous projects switching to their appropriate Ruby and Gems. RVM is a fantastic tool for anyone with multiple Ruby projects, or even just one since it allows for testing over multiple Rubies very succinctly. A big, big thank-you to Wayne, the creator of RVM for his excellent and hard work!