Installing and Using Rvm on Mac OS X, Creating Gemsets and Reverting to Original Environment

What is RVM and why should you use it? RVM is a Ruby interpreter, version management tool. In short, it enables you to switch between different versions and releases of Ruby (for instance, version 1.8.6, 1.8.7, jruby 1.9.2, ruby enterprise edition) on the same machine, while associating different gems with each version of the ruby interpreter. This is super useful and awesome. If you want to play with Rails 3 and Ruby 1.9.1, for 5 minutes, and then want to switch back to your production apps, which are running on Rails 2.3.5 and Ruby 1.8.7, you can do so with a single command from the terminal. With RVM this is a fairly simple process so there is no reason not to install it. You can also revert back to your system settings (not using RVM) with a single command. After all Rails is just a gem, so you can easily create and manage different RVM “gemsets”, (sets of different gems), for the different versions of Ruby (rubies as RVM refers to them) you have installed.

Installing RVM

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

Next you have to add rvm to your bash profile

# place in ~/.bash_profile as the very last line
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

To check everything went well

type rvm | head -n1

Should tell you “rvm is a function”

How to add ruby, pass it the version to install

rvm install 1.8.7

*The current terminal session will load this environment. New sessions will not. To use a version of ruby and set it as the default, pass it the –default option

rvm use 1.8.7 --default

Next create a gemset, which will make available different gems for different versions

rvm gemset create rails_2_3_5

When you run “gem list”, you should see nothing!

gem install rails -v=2.3.5

Set a default rvm and default gemset, specify which gemset with the @ sign and include the –default option

rvm use 1.8.7@rails_2_3_5 --default
which gem
gem list
ruby --version
rails --version

And to get back to where you started and revert to using your original ruby setup

rvm system

For upgrading your version of RVM check out this post I wrote http://seanbehan.com/ruby/how-to-upgrade-rvm-on-mac-os-x/

Finally, you can create a .rvmrc file and put it in any directory and when you cd into that directory the environment specified in the file will be loaded automatically. This way you don’t have to remember the version and gemsets and type them into the console. All you have to do is put the ruby version and gemset name in the file like so

ruby1.8.7@rails2.3.5

You’ll be prompted to trust the .rvmrc file the first time, type “y” for yes. Also, subdirectories will inherit this .rvmrc so you can just put it in the parent directory like

rails2/
     .rvmrc
     app1
     app2
rails3/
     .rvmrc
     app1
     app2

And both app1 and app2 will use the .rvmrc environment while your rails3 directory apps will load the environment in its directory!

More information available here:
http://rvm.beginrescueend.com/rvm/install/
http://www.stjhimy.com/posts/4
http://eddorre.com/posts/installing-rails-3-beta-4-using-rvm

  • me

    thank you so much for this mate, you are a star. I banged my head on screen all day to work out this rvm issue, then i came across this

  • http://berlin-guide.r76.se/vader/ Mars

    Great instructions, this ruby/rvm/java/osx thing is a pain in the butt. Still though, worth it when it works.

  • Pingback: Upgrading Ruby and RoR on Mac OS « I do it my way

  • satya

    simple way to install RVM Thank you very much

  • Ted Todorov

    Hi,
    First — thank you for the great work!

    I followed your instructions, and after rvm install 1.8.7
    got this error:

    ……..

    compiling readline
    ………..
    readline.c:730: error: for each function it appears in.)
    make[1]: *** [readline.o] Error 1
    make: *** [all] Error 1

    After googling, found:
    rvm_archflags=”-arch x86_64″
    rvm install 1.8.7

    Then:
    rails –version
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems.rb:827:in `report_activate_error’: Could not find RubyGem rails (>= 0) (Gem::LoadError)
    – google yielded restarting the console.

    That works! Hope my troubleshooting helps out other experiencing the same thing.

  • Pingback: bits and pieces » Blog Archive » rvm to the rescue

  • Greg

    I followed your instructions and things seem to work fine. However, when I go to create a new Rails project using v2.3.5 or just type in “rails” I get an error (below). Any thoughts on how to fix?

    Thanks for the tutorial!
    Greg

    greg$ rails test old
    /Users/greg/.rvm/gems/ruby-1.8.7-p334@rails_2_3_5/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:55: uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)
    from /Users/greg/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require’
    from /Users/greg/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require’
    from /Users/greg/.rvm/gems/ruby-1.8.7-p334@rails_2_3_5/gems/activesupport-2.3.5/lib/active_support.rb:56
    from /Users/greg/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require’
    from /Users/greg/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require’
    from /Users/greg/.rvm/gems/ruby-1.8.7-p334@rails_2_3_5/gems/rails-2.3.5/bin/../lib/rails_generator.rb:28
    from /Users/greg/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require’
    from /Users/greg/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require’
    from /Users/greg/.rvm/gems/ruby-1.8.7-p334@rails_2_3_5/gems/rails-2.3.5/bin/rails:14
    from /Users/greg/.rvm/gems/ruby-1.8.7-p334@rails_2_3_5/bin/rails:19:in `load’
    from /Users/greg/.rvm/gems/ruby-1.8.7-p334@rails_2_3_5/bin/rails:19

  • Greg

    Oh…one more thing. In the above it says “rails test old”; however, I actually typed “rails testold” with testold being the name of the project.

    Thanks,
    Greg

  • Andres Yajamin

    Thanks, very clear and usefull post,

    I would add the link with the use of the .rvmc which let you load a default rvm profile at each app project

    http://rvm.beginrescueend.com/workflow/rvmrc/

  • bong den

    Very nice dude!

  • Fernando

    very nice, very easy, thanks a lot!

  • LantzR

    Thanks for the clear notes! The url you cite has moved.

    bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

  • http://nickpachulski.com Nick Pachulski

    First good RVM help I’ve seen. Helped me out a ton. Thanks a lot,

    Nick

  • Pingback: Installing and using RVM | Victor Sumner

  • Marshall

    Thanks so much for this. So simple clean and quick

  • Pingback: How to install Arachni on Mac OS X Lion : Secure By Default

  • Pingback: Back to Ruby on Rails – Phillip Kohtz