Nov 28 2009

How to use multiple versions of Ruby in Ubuntu, Leopard and other Unix Operating Systems using RVM

Ruby Rails Ubuntu Linux Unix RVM | comments

There are many ways on how to run multiple versions of Ruby on Unix. The most simple solution is by creating a shell script.

Michael Greenly wrote a script to do just that and it worked great for me for several months. Check out his post.

This was how I switched from Ruby 1.8.7 to Ruby 1.9.2 Preview 1 and back to Ruby 1.8.7.

               $ sudo select_ruby
              [sudo] password for katz:
              
              current = ruby 1.8.7 (2008-05-31 patchlevel 0) [i686-linux]
              
              Select Option
              
              1. ruby-1.8.7
              2. ruby-1.9.2-preview1
              3. exit
              
               Your choice? : 2
               $ ruby -v
              ruby 1.9.2dev (2009-07-18 trunk 24186) [i686-linux]
               $ sudo select_ruby
              
              current = ruby 1.9.2dev (2009-07-18 trunk 24186) [i686-linux]
              
              Select Option
              
              1. ruby-1.8.7
              2. ruby-1.9.2-preview1
              3. exit
              
               Your choice? : 1
               $ ruby -v
              ruby 1.8.7 (2008-05-31 patchlevel 0) [i686-linux]
              

Very neat.

RVM

But lately I found out about Ruby Version Manager and it seems to be a more sensible way to install multiple versions of Ruby. Here's how I installed it on Ubuntu.

               $ sudo gem install rvm
              [sudo] password for katz:
              Successfully installed rvm-0.0.84
              1 gem installed
              Installing ri documentation for rvm-0.0.84...
              Installing RDoc documentation for rvm-0.0.84...
               $ rvm-install
              
              rvm - shell scripts that allows a user to manage multiple 
              ruby versions in their own account.
              
                  Installing to /home/katz/.rvm...
                  Adding 'if [[ -s /home/katz/.rvm/scripts/rvm ]] ; 
              then source /home/katz/.rvm/scripts/rvm ; fi' to /home/katz/.bash_profile.
                  Adding 'if [[ -s /home/katz/.rvm/scripts/rvm ]] ; 
              then source /home/katz/.rvm/scripts/rvm ; fi' to /home/katz/.bashrc.
                  Adding 'if [[ -s /home/katz/.rvm/scripts/rvm ]] ; 
              then source /home/katz/.rvm/scripts/rvm ; fi' to /home/katz/.zshrc.
              
              rvm notes for Linux
              
              *  For JRuby (if you wish to use it) you will need:
              
                $ aptitude install sun-java6-bin sun-java6-jre sun-java6-jdk
              
              *  For MRI & ree (if you wish to use it) you will need:
              
               $ aptitude install bison build-essential zlib1g-dev libssl-dev libreadline5-dev libxml2-dev
              
               *  In order to use rvm the following line must occur in your 
              shell's loading files, after all path/variable settings.:
               *  if [[ -s /home/katz/.rvm/scripts/rvm ]] ; 
              then source /home/katz/.rvm/scripts/rvm ; fi
               *  CLOSE THIS SHELL AND OPEN A NEW ONE in order to use rvm.
              
              RTFM:
                   http://rvm.beginrescueend.com/
              
              w⦿‿⦿t!
              
                  ~ Wayne
              
              rvm 0.0.84 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]
              

Since you don't want to install all versions of Ruby, you might want to do this:

              rvm list --all
              (ruby-)1.8.0-tv1_8_0
              (ruby-)1.8.0-tv1_8_0_preview1
              (ruby-)1.8.0-tv1_8_0_preview2
              (ruby-)1.8.0-tv1_8_0_preview3
              (ruby-)1.8.0-tv1_8_0_preview4
              (ruby-)1.8.0-tv1_8_0_preview5
              (ruby-)1.8.0-tv1_8_0_preview6
              (ruby-)1.8.0-tv1_8_0_preview7
              (ruby-)1.8.1-tv1_8_1
              (ruby-)1.8.1-tv1_8_1_preview1
              (ruby-)1.8.1-tv1_8_1_preview2
              (ruby-)1.8.1-tv1_8_1_preview3
              (ruby-)1.8.1-tv1_8_1_preview4
              (ruby-)1.8.2-tv1_8_2
              (ruby-)1.8.2-tv1_8_2_preview1
              (ruby-)1.8.2-tv1_8_2_preview2
              (ruby-)1.8.2-tv1_8_2_preview3
              (ruby-)1.8.2-tv1_8_2_preview4
              (ruby-)1.8.3-tv1_8_3
              (ruby-)1.8.3-tv1_8_3_preview1
              (ruby-)1.8.3-tv1_8_3_preview2
              (ruby-)1.8.3-tv1_8_3_preview3
              (ruby-)1.8.4-tv1_8_4
              (ruby-)1.8.4-tv1_8_4_preview1
              (ruby-)1.8.4-tv1_8_4_preview2
              (ruby-)1.8.5-tv1_8_5
              (ruby-)1.8.5-tv1_8_5_11
              (ruby-)1.8.5-tv1_8_5_113
              (ruby-)1.8.5-tv1_8_5_114
              (ruby-)1.8.5-tv1_8_5_115
              (ruby-)1.8.5-tv1_8_5_12
              (ruby-)1.8.5-tv1_8_5_2
              (ruby-)1.8.5-tv1_8_5_231
              (ruby-)1.8.5-tv1_8_5_35
              (ruby-)1.8.5-tv1_8_5_52
              (ruby-)1.8.5-tv1_8_5_preview1
              (ruby-)1.8.5-tv1_8_5_preview2
              (ruby-)1.8.5-tv1_8_5_preview3
              (ruby-)1.8.5-tv1_8_5_preview4
              (ruby-)1.8.5-tv1_8_5_preview5
              (ruby-)1.8.6-tv1_8_6
              (ruby-)1.8.6-tv1_8_6_110
              (ruby-)1.8.6-tv1_8_6_111
              (ruby-)1.8.6-tv1_8_6_114
              (ruby-)1.8.6-tv1_8_6_230
              (ruby-)1.8.6-tv1_8_6_286
              (ruby-)1.8.6-tv1_8_6_287
              (ruby-)1.8.6-tv1_8_6_36
              (ruby-)1.8.6-tv1_8_6_368
              (ruby-)1.8.6-tv1_8_6_369
              (ruby-)1.8.6-tv1_8_6_383
              (ruby-)1.8.6-tv1_8_6_preview1
              (ruby-)1.8.6-tv1_8_6_preview2
              (ruby-)1.8.6-tv1_8_6_preview3
              (ruby-)1.8.7-tv1_8_7
              (ruby-)1.8.7-tv1_8_7_160
              (ruby-)1.8.7-tv1_8_7_17
              (ruby-)1.8.7-tv1_8_7_173
              (ruby-)1.8.7-tv1_8_7_174
              (ruby-)1.8.7-tv1_8_7_22
              (ruby-)1.8.7-tv1_8_7_71
              (ruby-)1.8.7-tv1_8_7_72
              (ruby-)1.8.7-tv1_8_7_preview1
              (ruby-)1.8.7-tv1_8_7_preview2
              (ruby-)1.8.7-tv1_8_7_preview3
              (ruby-)1.8.7-tv1_8_7_preview4
              (ruby-)1.9.0-tv1_9_0_0
              (ruby-)1.9.0-tv1_9_0_1
              (ruby-)1.9.0-tv1_9_0_2
              (ruby-)1.9.0-tv1_9_0_3
              (ruby-)1.9.0-tv1_9_0_4
              (ruby-)1.9.0-tv1_9_0_5
              (ruby-)1.9.1-tv1_9_1_0
              (ruby-)1.9.1-tv1_9_1_126
              (ruby-)1.9.1-tv1_9_1_127
              (ruby-)1.9.1-tv1_9_1_128
              (ruby-)1.9.1-tv1_9_1_129
              (ruby-)1.9.1-tv1_9_1_243
              (ruby-)1.9.1-tv1_9_1_preview1
              (ruby-)1.9.1-tv1_9_1_preview2
              (ruby-)1.9.1-tv1_9_1_rc1
              (ruby-)1.9.1-tv1_9_1_rc2
              (ruby-)1.9.2-tv1_9_2_preview1
              (ruby-)1.9.2-tv1_9_2_preview2
              (ruby-)1.8.6(-p383)
              (ruby-)1.8.6-head
              (ruby-)1.8.7(-p174)
              (ruby-)1.8.7-head
              (ruby-)1.9.1(-p243)
              (ruby-)1.9.1-head
              (ruby-)1.9.2-preview1
              jruby-1.2.0
              jruby-1.3.0
              jruby-1.3.1
              jruby(-1.4.0) # the default
              jruby-head
              rbx(-head) # rubinius head, the default rbx
              rbx(-head) --jit (rubinius head with LLVM enabled)
              ree-1.8.6
              ree(-1.8.7) # the default
              ree-1.8.6-head
              ree-1.8.7-head
              mput(-head) # shyouhei head, the default mput
              

I think most relevant and acceptable versions are Ruby 1.8.7 and Ruby 1.9 versions. I prefer to have Ruby 1.9.1-tv1_9_1_rc2 and 1.8.7 versions.

Do not worry, it will not do anything about the Ruby version installed on root.

              rvm install ruby-1.9.1 #this will download the stable version of Ruby 1.9
              rvm install ruby-1.8.7-tv1_8_7_72
              

So now I have these Ruby Versions:

              rvm list
              ruby-1.8.7-tv1_8_7_72  
              (ruby -v # => ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux]) )
              ruby-1.9.1-p243  
              ( ruby -v # => ruby 1.9.1p243 (2009-07-16 revision 24175) [i686-linux]) )
              system ( ruby -v # => /opt/ruby/bin/ruby
              ruby 1.8.7 (2008-05-31 patchlevel 0) [i686-linux] )
              

And now I could switch to using Ruby 1.9.1.

              
               $ rvm use ruby-1.9.1-p243
               Now using ruby 1.9.1 p243 
               $ ruby -v
              ruby 1.9.1p243 (2009-07-16 revision 24175) [i686-linux]
               $ irb
              ruby-1.9.1-p243 > exit
               $ gem list
              
              *** LOCAL GEMS ***
              
              rake (0.8.7)
              rubygems-update (1.3.5)
              
              

And today I found out that Rails 2.3.5 is released. Perfect. :)

Update:

As Wayne has noted on the comment below. "Use" is optional and "ruby" is optional. So to switch to using 1.9.1, use:

              
               $ rvm 1.9.1-p243