Enhance your Ruby development with RVM gemsets and .rvmrc

RVM (Ruby Version Manager) allows you to install and manage multiple versions of Ruby on the same computer. In addition to separate Ruby environments, RVM also lets you create independent gemsets. When you create a separate gemset for each project, you start with a clean slate and then only install the gems pertinent to each project.

If you don't already have RVM and Ruby 1.9.3 installed, you can be up and running by following my tutorial for setting up a Mac for development.

Once you have RVM installed, you can create and use a new gemset by running:

$ rvm gemset create my_gemset
$ rvm gemset use my_gemset

You can also use the shortcut:

$ rvm use 1.9.3@my_gemset --create

This creates the gemset "my_gemset", associates it with version 1.9.3 of Ruby, and sets it as the active gemset.

To view the gems installed in the current gemset in use, run:

$ gem list

Since we haven't installed anything in our new gemset yet, you should see the following:

*** LOCAL GEMS ***

When gems are installed, they generate by default two kinds of documentation called "ri" and "rdoc" which slow down the installation. If, like me and most developers, you would rather read the documentation online and have your gems install quickly, then you can disable the documentation generation by creating a file called ".gemrc" in your user's home directory (using your favorite text editor) and adding the following lines:

install: --no-rdoc --no-ri
update: --no-rdoc --no-ri

If you're comfortable with the command line, you might find it faster to create and update the file via the following Terminal command:

$ echo -e "install: --no-rdoc --no-ri \nupdate: --no-rdoc --no-ri" >> ~/.gemrc

Once you have more than one gemset, you will need an easy way to find out which gemset is the currently-active one. To display the ruby version and gemset currently in use (this info will appear before every command prompt in Terminal), add the following line to your .bash_profile using your favorite text editor:

PS1="\$(~/.rvm/bin/rvm-prompt) $PS1"

You can also do it all in one step from Terminal:

$ echo 'PS1="\$(~/.rvm/bin/rvm-prompt) $PS1"' >> ~/.bash_profile

Another way to determine the active gemset is to run:

$ rvm gemset list

This will list all your gemsets, one on each line, and indicate the active one with a =>.

To delete a gemset:

$ rvm gemset delete gemset_name

Another handy RVM feature is the project .rvmrc file. This is a file you create at the root of your project's directory, which tells RVM to automatically load a specific gemset every time you cd into that directory. If you're running patch 125 of Ruby 1.9.3, and if your gemset is called "casablanca", then you would add the following line to a new file in your project's root directory, and save the file as ".rvmrc":

$ rvm use ruby-1.9.3-p125@casablanca

or rvm 1.9.3@casablanca for short.

The one-liner:

$ echo "rvm 1.9.3@casablanca" >> ~/path/to/your_project_directory/.rvmrc

Now that you know about all the pieces, you can tie them all together by creating and using a new gemset, along with its rvmrc file, all in two steps:

$ cd /path/to/your_project_directory
$ rvm --rvmrc --create use 1.9.3@my_awesome_rails_app