I recently started using
rvm for all of my projects.
rvm is designed to help ruby developers work with multiple versions of ruby on their system. I recently came up with a great way always knowing with version of ruby is in use by
rvm. But before I go into that, let’s talk about some details about
Installing a few rubys
Once you get
rvm installed, you only need to run
rvm install 1.9.2. That command will download and build the latest version of ruby 1.9.2 from source. If you also work with Phusion’s ruby enterprise edition, you can install it from source by running
rvm install ree.
After running those two commands, you will have three versions of ruby installed on your computer, the system version, ruby 1.9.2 and ruby enterprise edition. However, if you run
ruby --version you’ll notice that the system version is the one that is getting executed. Here’s what doing so looks like on my Mac running Mac OS X version 10.6.6.
cloudraker:~ mscottford$ ruby --version ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
However, if you first run
rvm use 1.9.2, the running
ruby --version should give you exactly what you expect. Try switching to ruby enterprise edition with
rvm use ree. Again, running
ruby --version should confirm that the switch took place correctly. Should you want to return to using your system’s version of ruby, just execute
rvm use system.
If you ever need to check which version of
rvm is active, you can run
rvm current. This will output the name of the ruby that
rvm has setup. We’ll discuss a better way to determine which ruby is active a little later. But first, let’s talk about how
rvm helps us manage gems for each project.
Working with gemsets
Since just having different versions of ruby is not enough,
rvm also gives us the ability to create different sets of gems that are completely isolated from each other. By default each version of ruby that we install gets its own gemset. We also have the ability to create named gemsets.
We create gemsets with the command
rvm gemset create gemset_name. This will create a gemset for the currently selected version of ruby. One thing to keep in mind is that creating a gemset does not automatically switch you to that gemset. To do that you’ll need to use the
rvm use command, for example
rvm use 1.9.2@gemset_name. If you need to figure out which gemset is active, you can run the
rvm currrent command. Once again, a better way to keep track of this is on it’s way.
Here’s a longer example that shows how to create and work with gemsets.
$ rvm use 1.8.6 $ rvm gemset create funkyness 'funkyness' gemset created (/Users/mscottford/.rvm/gems/ruby-1.8.6-p399@funkyness). $ rvm current ruby-1.8.6-p399 $ rvm use 1.8.6@funkyness Using /Users/mscottford/.rvm/gems/ruby-1.8.6-p399 with gemset funkyness $ rvm current ruby-1.8.6-p399@funkyness $ rvm use 1.8.6 Using /Users/mscottford/.rvm/gems/ruby-1.8.6-p399 $ rvm current ruby-1.8.6-p399
.rvmrc, and stop thinking
To make it impossible to forget which of your projects are using which versions of ruby and even then which gemsets,
rvm will look for a
.rvmrc in each directory that you switch into with the
Here’s an example.
$ rvm current system $ cd funkyness $ rvm current ruby-1.8.6-p399@funkyness
Okay. That looks like magic. What’s going on?
To answer that question, let’s take a peek inside of ~/funkyness/.rvmrc.
rvm 1.8.6@funkyness --create
With that one line,
rvm will switch to ruby version 1.8.6 and gemset funkyness. It will even create it for you if it does not exist.
Since this feature could potentially be used to trick you into running malicious code on your system,
rvm asks you to trust a
.rvmrc file the first time that it reads it. You only have to do this once however.
What’s this post about again?
Now that I’ve explained the finer points about using
rvm, I can finally start to vent a little.
I have several ruby projects that I’m working on at the moment. Some are for fun, but most are for my paying clients. I only recently started using
.rvmrc files, and I’ve yet to create them for all of my projects. This means that for some projects, I don’t really need to think about which version of ruby is getting run, because it is the version that I’ve specified in the
.rvmrc file. For other projects, however, I need to remember to run
rvm use with the correct version of ruby for that project.
But I’d hate to run
rvm use if I don’t need to. And running
rvm current all them time seems a little silly. The solution that I’ve come up with is to alter the bash prompt to always let me know the current version of ruby that is in use by rvm.
To get started I used my favorite search engine to see if someone had already tackled this problem. I found one really good example that even introduced some color, however it was also using some
git magic to include the current branch on the prompt. A few modifications later, I came up with my own version that just displays the ruby that is in use by
rvm, and it does so while looking like it was copied and pasted out of Textmate.
Here’s what it looks like.
:rvm => 'system' ~ $ cd funkyness :rvm => 'ruby-1.8.6@funkyness' funkyness $ cd .. :rvm => 'system' ~ $