Tuesday, May 31, 2011

Using RVM to Install Rails 3.1: Best Practices

Wayne E. Seguin describes RVM as:

a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems.

This article focuses on the workflow of installing RVM, and use it to install Ruby 1.9.2 (MRI), create gemsets and install Rails 3.1 Release Candidate, while considering current best practices.

Installing Ruby Version Manager

If your Ubuntu 11.04 is still fresh from the oven, you may want to first install git and curl. Having satisfied the prerequisites, installing the latest RVM release version from git is as easy as running:

If at any point you want to start all over, run rvm implode and we'll be ready for a fresh start. For a complete removal, follow these details.

On the other hand, if RVM is already installed but we are behind on the updates,
rvm get latest will do the trick.

Setting up Ruby

  1. Feel free to run rvm list known for all the Ruby implementation made available through RVM
  2. We are focusing on the latest stable release, so we'll choose to:
    rvm install 1.9.2
  3. And we will use it as the default version for our system:
    rvm use 1.9.2 --default

Using gemsets

Gemsets are compartmentalized independent ruby setups, each containing it's own version of ruby, gems and irb. RubyGems — the package manager for Ruby projects — is already available for us, since RVM includes it automatically (try which gem).

For this tutorial we will install side by side the latest Rails stable release (3.0.7) and the latest release candidate (3.1), so let's prepare the terrain:

  1. Start by creating our gemset(s):
    rvm gemset create rails307 rails31
  2. The result can be verified by listing the available gemsets:
    rvm gemset list
  3. If a gem's name still leaves room for confusion, simply delete it and create a more meaningful one (e.g., rails31rc):
    rvm gemset delete rails31
  4. As a best practice, remember to always use one gemset per project*.

Installing Rails

  1. Now that we have multiple gemsets installed, we must first select the one we want to use, and we can also set it as the default gemset by passing it the --default flag:
    rvm use 1.9.2-p180@rails307 [--default]
  2. Installing rails is as easy as installing any other gem: we only need to specify it's name, but we can always choose a specific version, or to speed up the installation process by skipping the documentation:
    gem install rails [-v 3.0.7] [--no-rdoc --no-ri]

  3. Next we switch to the gemset created to hold the latest Release Candidate (e.g., 1.9.2-p180@rails31rc) and we install it by passing in the --pre flag.
    Scratch that; the --pre flag is not working as of June 1st. You might want to read this before installing Rails 3.1, but long story short, this should do the trick:
    gem install rails -v ">=3.1.0rc"

Bonus feature

Switching from one project to another, from a client to a personal project, from testing the release candidate to developing using the latest stable version, always having to manually switch from using a gemset to another can impact productivity. The project .rvmrc files can increase the development speed by setting up our project's ruby environment when we switch to the project root directory.

The rule of thumb here is to use a .rvmrc file for each project, for both development and deployment.*

* Make sure to check the RVM best practices!

How are gemsets improving your workflow? What other tips & tricks have you discovered while setting up your environment, or while setting up a new project? I would be more than happy to learn something new from you!


  1. rvm gemset use uxolo --rvmrc


    echo "rvm use 1.9.2-p180@uxolo" > ~/projects/uxolo/.rvmrc

  2. you should add this to the last line of your bashrc to use rvm command

    if [[ -s $HOME/.rvm/scripts/rvm ]] ; then source $HOME/.rvm/scripts/rvm ; fi


  3. @Kimooz: Here is a detailed guide to installing RVM, but if you carefully follow the instructions in the installation output, everything should run smoothly.

  4. I highly recommend against simply putting the RVM use command directly into the rvmrc alone. Instead in the project directory run this,

    user$ rvm --rvmrc --create use 1.9.2-p180@uxolo

    Then edit and adjust the generated .rvmrc file. Be sure to have at least RVM 0.6.18 if you use Bundler as it has extra lines for that as well.

  5. Additionally, since you use the same scheme as I do you might find BDSM's interactive project code useful. Specifically the p / pe commands.

    Then you can do

    user$ pe ux

    This will cd into the ~/projects/uxolo directory and launch your defined EDITOR with that directory as a specified target.

    See the documentation page for details =>

  6. Thanks for the tips Wayne, I was actually looking over BDSM yesterday.

    Will try how it behaves with RubyMine!

  7. Update: fixed --pre flag issue and added reference to the complementary, more detailed instructions: read this before installing Rails 3.1.

  8. FYI,

    rvm 1.9.2@rails31 --create

    Creates and then switches to the gemset rails31 under mri 1.9.2

  9. Thanks god this made my life easy

  10. Thanks for this post, it cleared up somethings for me. I'm confused about the per-project gemset suggestion though. The suggestion makes sense, but you name your gemsets rails307 and rails31, which are rails versions, not projects. I feel like I missed something there.

  11. a was the hole day trying installing rails but it wont work ?? please help me .I am a beginner

    that is my output
    :~$ gem install rails -v ">=3.1.0rc"
    ERROR: Loading command: install (LoadError)
    no such file to load -- zlib
    ERROR: While executing gem ... (NameError)
    uninitialized constant Gem::Commands::InstallCommand
    alaedine@alaedine-shark-attack:~$ gem install rails -v ">=3.1.0rc"
    ERROR: Loading command: install (LoadError)
    no such file to load -- zlib
    ERROR: While executing gem ... (NameError)
    uninitialized constant Gem::Commands::InstallCommand

    1. Since you are a beginner I would suggest to run the ruby setup again with zlib installed..For more details refer the foll. link: