Once you’re addicted to Ruby development, you’re going to want to experiment with different versions of Ruby. At first blush, you might think that it’s somewhat difficult. RVM makes this an absolute breeze. In this tutorial, I’m going to get you up and running with RVM.
0 — What is RVM?
Without RVM, it’s pretty difficult to have more than one version of Ruby on your computer.
RVM stands for Ruby enVironment Manager. Here’s the idea. As a Ruby developer, you might run into situations where you’d need to use multiple versions of Ruby. For example, you started with Ruby 1.8.6, but now you want to experiment with 1.9.2. Or, you normally work with the 1.9 branch, but you want to contribute to an open source project that’s still on the 1.8 branch.
Without RVM, it’s pretty difficult to have more than one version of Ruby on your computer. Or, what if you want to try one of the alternate Ruby interpreters, like Rubinius or JRuby? How can you take one out for a spin without giving up your faithful MRI install? This is where RVM steps in. It’s basically a framework that handles multiple installations of Ruby for you, and makes it easy for you to switch between them.
Two caveats apply today:
You should be pretty familiar with the command line.
You should be on a Unix system. This means you’re probably running either Mac OS X, or some version of Linux. As much as I wish RVM was available on Windows, it isn’t. If you’re on Windows, you might try Pik; I understand it’s similar, but I haven’t had a chance to use it yet.
1 — How do I install RVM?
Installing RVM is a bit awkward. First, you need to have Git installed. Then, you download a script, and run it to perform the installation. The RVM site recommends doing it this way:
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
Even if you’re fairly comfortable with the command line, you’ll probably pause when you see this. It’s easier to read it backwards: we’re using
curl to download the script that the URL points to; the
-s command lets
curl know you want to download the script silently (without any output). This is the script that installs RVM.
Then, to run this script automatically, we’re using the double less-than sign to pass the script to the command on the left side of the operator. In our case, that’s
bash. You might know bash as a shell, or the terminal that you’re probably running right now. This runs the script in a bash shell (which is running inside the bash shell in your terminal!).
The command above downloads and runs the script, which will install RVM.
But we’re not done quite yet. To make sure RVM runs properly, you must add the following snippet to your
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
As you see from the comment, “this loads RVM into a shell session.” Specifically—if you’re curious—this loads the
rvm command as a shell function (like the ones you might add to your
~/.bash_profile file), instead of as a binary (a program that you run in the shell, like
But remember, RVM is a fast-growing library, and it's possible that the installation method will change. For the most up-to-date ways to install RVM, check the documentation.
Now that we have RVM installed, let’s actually use it!
2 — How do I use RVM?
The two big features that RVM offers are Rubies and Gemsets. Let’s look at each of these in turn, and then look at some other customizations.
Rubies are the primary (though not exclusive) intent of RVM; remember what RVM stands for? Ruby enVironment Manager. So, let’s install some rubies!
If you’ve been using Ruby for a while, you’ll know there are several different Ruby implementations to choose from. You can see which ones are available with RVM by using this command:
rvm list known
By the way, take a look at this syntax; most of the RVM commands you’ll use are like this:
rvm, a category, and a command for that category. Predictably, this will list all the Ruby implementations (and versions of implementations) that you can install.
So let’s pick a version and install it. However, there's something we should do first. It's possible that there might be some dependancies that your platform requires before you can install the ruby of your choice. Incredibly, RVM has your back here as well: run the command
rvm notes to see what dependencies you should install. It will list the different rubies you can install, and what you should install first for each of them.
I generally exclusively use Ruby 1.9.2, so let’s install that. To install a Ruby, use the command
rvm install, followed by the implementation and version you want, as listed with
rvm list known.
rvm install ruby-1.9.2-p180
You can install as many versions of Ruby as you like just by using this command.
This command will take a few minutes to run, but once it’s done, you’ll have the version of ruby you selected installed on your computer.
You can install as many versions of Ruby as you like by using this command. But, now that you’ve got more than one version installed, how do you use them, or switch between them? Also, what about the Ruby implementation that likely came with your computer?
It’s pretty simple. To use a given Ruby, use this command:
rvm use <ruby string> # in abstract rvm use 1.9.2-p180
If that’s all you do, you’ll have to type that each time you open a terminal; otherwise, you'll default to the built-in Ruby. To avoid this, add the flag
--default. This also gives you a shortcut to switch back to your default Ruby at any time; if you’ve switched to another Ruby, just type
rvm default to switch back.
To make sure you’re using the right version, type
ruby -v; you should see the version of Ruby that use just selected.
If you’re using a few different Rubies, you’ll want to keep track of what you’ve got; that’s why you have the
rvm list command. Here’s my current inventory:
ruby-1.8.7-p302 [ x86_64 ] ruby-1.8.7-p334 [ x86_64 ] ruby-1.9.2-p0 [ x86_64 ] ruby-1.9.2-p180 [ x86_64 ]
As you might guess, the
=> beside the last one means that's the one I'm currently using.
If you want to remove a Ruby, it’s just as easy. Use
rvm remove <ruby string> to get rid of everything related to that Ruby. If you think you might want to install that Ruby again in the future, just use
rvm uninstall; this leaves the source code and other pieces in place.
While multiple Rubies are cool, gemsets—at least for me—are where it’s at. Basically, a gemset is a set of gems that you use exclusively (or almost exclusively). This is great in two cases:
- You want to use different versions of a given gem, probably with different versions of Ruby.
- You want to “sandbox” your gems, so only certain gems are available at a given time.
Let’s see how this works.
rvm gemset create <gemset name>
Of course, you're free to name your gemset whatever you want. To see all your gemsets, use
rvm gemset list. If you do this now, you’ll see two gemsets (assuming you’ve just run the command above). One will be
<gemset name> (or whatever you called it) and
global gemset is a special one; as you can probably guess, the gems in the
global gemset are available no matter which gemset you’re using.
You can see the benefit to this: there are some gems you just always want, no matter what you’re doing. Really, there’s another gemset, the default gemset, which isn’t listed and doesn’t have a name. If you’re not using a specific gemset, you have the gems in the default gemset (and, of course, the global gemset) available.
You can see your set of gemsets by using the command
rvm gemset list; as before,
=> marks the gemset you're currently using. If no gemset is marked, you're using the default gemset.
Nothing could be easier than switching gemsets:
rvm gemset use <gemset name>
Once you’re using a given gemset, you can install gems with
gem install gem-name. There’s an important note here. Don’t use
sudowhen installing that; doing so completely ignores that RVM is running the show here and installs it to the whole system, outside the RVM environment. So, do just
gem install gem-name.
As mentioned, the default and global gemsets are created when you install a new Ruby. If you have certain gems that you want installed by default when you install a new Ruby, add them to the appropriate gems file. In
~/.rvm/gemsets/, you’ll find two files:
global.gems. Just list the gems by name, one of each line. These gems will be installed in the appropriate gemset when you install a new Ruby.
These gems files are also a great way to store a list of gems. You can export a gemset with
rvm gemset export; this will save a list of the gems in the current gemset as
default.gems in whatever folder you’re currently in. If you want to give this file a specific name, just add it at the end of that command. Then, you can import that list of gems in a different Ruby or even on a different computer (think checking it into source control) with the
rvm gemset import gem-list-file-name.
One more thing about gemsets: you can use them in your
rvm use commands. Do this:
rvm use <ruby string>@<gemset name>
.rvmrc files are really cool. Consider them the settings panel of RVM. You can have a .rvmrc file in three different places:
/etc/rvmrc sets options for the whole system;
~/.rvmrc sets options for the current user; a
.rvmrc file in a specific directory customizes things for that project.
The global and user
.rvmrc files are mainly compile options for installing new Rubies. However, the project-specific
.rvmrc files are the fun ones.
Here's how you do it: when you start a new project, you'll what to create a
.rvmrc file in that directory. Let’s say you have a rails app that needs Ruby 1.8.6 to run; you have Ruby 1.9.2 as the default, and you don’t want to have to remember to switch Rubies when you’re working on that project. So, type this command:
rvm --rvmrc --create 1.8.6-p334
This will create a heavily commented
.rmvrc file that will change your Ruby environment seamlessly when you switch into that directory. Be sure to read the comments in there to see what exactly is being done to switch environments, and what you can do with
It gets even better with gemsets. I use Ruby 1.9.2 for all my projects, but I like to have a different gemset for each project. So, if I’ve got a project that’s called
superproj, I can do this:
rvm --rvmrc --create 1.9.2@superproj
Just add an at-sign after the Ruby name and follow that with your gemset name. As expected, this will seamlessly load the right gems for that project. If this gemset hasn't been created yet, RVM will do that for you, too.
It's possible that you'll want to find out what ruby and gemset you're currently using. There are two ways to do this: firstly, you can just use the command
rvm info. This will tell you what ruby and gemset you're using (among many other things).
If you want something more permanent, you can put the name right into your prompt. To do this, add a line to your
~/.bash_profile. The example the RVM documentation gives is this:
What this does is run the script
~/.rvm/bin/rvm-prompt and put the output into your prompt (then, it appends whatever your prompt currently is on the end). This will put the ruby name and gemset name at the beginning of your prompt. Even better, if you're familiar with customizing your prompt, check out the documentation for more ways to customize that output.
Conclusion: Have Fun with It!
I hope I’ve demonstrated well enough that there’s really no reason for not using RVM if you’re on a Unix box. Personally, I haven’t yet found it necessary to use more than one interpreter/version of Ruby, but I find the gemsets and project-specific
.rvmrc files to be incredibly handy.
But there’s more to learn about RVM than I’ve shown here. For more education, be sure to check out the excellent documentation. And let me know in the comments if you have any questions.