Monday, May 23, 2011

Installing RVM (Ruby Version Manager) on Ubuntu 10.10

Wayne E. Seguin's RVM is the new hotness, so I decided to install it. Here I describe how I did it on my Dell laptop running Ubuntu 10.10. I assume you already have Ruby and git installed. I'll be following the installation instructions on the RVM page for a single-user install, not a multi-user install.

First, make sure you have git installed, and then find out what shell you're using:
$ ps -p$$ -ocmd=
bash -v
This tells me I'm using the Bash shell, which I can confirm by asking Bash explicitly what version it is:
$ bash --version
GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)

The RVM installation instructions show three steps to follow:
  1. Download and run the RVM installation script
  2. Load RVM into your shell sessions as a function
  3. Reload shell configuration & test
We'll go through each of these in turn.

Step one: Download and run the RVM installation script

Run the installation script by copying exactly the command specified in the instructions:
$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
curl -s https://rvm.beginrescueend.com/install/rvm
Successfully checked out branch ''
remote: Counting objects: 430, done.
remote: Compressing objects: 100% (361/361), done.
remote: Total 384 (delta 263), reused 0 (delta 0)
Receiving objects: 100% (384/384), 49.60 KiB, done.
Resolving deltas: 100% (263/263), completed with 36 local objects.
From git://github.com/wayneeseguin/rvm
   07698bc..8bbab99  master     -> origin/master
From git://github.com/wayneeseguin/rvm
 * [new tag]         1.6.10     -> 1.6.10
 * [new tag]         1.6.11     -> 1.6.11
 * [new tag]         1.6.12     -> 1.6.12
 * [new tag]         1.6.13     -> 1.6.13
 * [new tag]         1.6.14     -> 1.6.14
 * [new tag]         1.6.6      -> 1.6.6
 * [new tag]         1.6.7      -> 1.6.7
 * [new tag]         1.6.8      -> 1.6.8
 * [new tag]         1.6.9      -> 1.6.9
First, rewinding head to replay your work on top of it...
Fast-forwarded master to 8bbab996123063aaeb5f7025f34aa742fc107b57.
Successfully pulled (rebased) from origin 

  RVM:  Shell scripts enabling management of multiple ruby environments.
  RTFM: https://rvm.beginrescueend.com/
  HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)
  
Upgrading the RVM installation in /home/andy/.rvm/~/.rvm ~/.rvm/src/rvm
~/.rvm/src/rvm

    Correct permissions for base binaries in /home/andy/bin...
    Copying manpages into place.


Upgrade Notes

  * rvm_trust_rvmrcs has been changed to rvm_trust_rvmrcs_flag for consistency

  * Ruby package dependency list for your OS is given by:
    rvm notes

  * If you encounter any issues with a ruby 'X' your best bet is to:
    rvm remove X ; rvm install X

  * If you wish to have the 'pretty colors' again, set in ~/.rvmrc:
    export rvm_pretty_print_flag=1

  * If you see the following error message: Unknown alias name: 'default'
    re-set your default ruby, this is due to a change in how default works.


WARNING: You have RUBYOPT set in your current environment.
This may cause rubies to not work as you expect them to as it is not supported
by all of them If errors show up, please try un-setting RUBYOPT first.


Upgrade of RVM in /home/andy/.rvm/ is complete.


Andy Sturges,

Thank you very much for using RVM! I sincerely hope that RVM helps to
make your work both easier and more enjoyable.

If you have any questions, issues and/or ideas for improvement please
join#rvm on irc.freenode.net and let me know, note you must register
(http://bit.ly/5mGjlm) and identify (/msg nickserv  ) to
talk, this prevents spambots from ruining our day.

My irc nickname is 'wayneeseguin' and I hang out in #rvm typically

  ~09:00-17:00EDT and again from ~21:00EDT-~23:00EDT

If I do not respond right away, please hang around after asking your
question, I will respond as soon as I am back.  It is best to talk in
#rvm itself as then other users can help out should I be offline.

Be sure to get head often as rvm development happens fast,
you can do this by running 'rvm get head' followed by 'rvm reload'
or opening a new shell

  w⦿‿⦿t

    ~ Wayne

It appears that everything worked, but we got one big warning:
WARNING: You have RUBYOPT set in your current environment.
This may cause rubies to not work as you expect them to as it is not supported
by all of them If errors show up, please try unsetting RUBYOPT first.
What is RUBYOPT, what is my current environment, and how do I unset it? First, try this:

$ echo $RUBYOPT
rubygems

RUBYOPT is an environment variable that I set a long time ago because I got tired of requiring rubygems in all of my ruby scripts. But I guess it's time to remove it. RubyGems is a gem management system, but it's not the only one; a particular library might prefer to use another one, so by setting it as an environment variable, I force all libraries to use my choice, and those libraries might have issues with that. I had set this environment variable by adding the following line to the file ~./bashrc:

export RUBYOPT="rubygems"
To unset it, I'll just delete or comment out that line, and restart the shell. Now if I try echoing the value of that variable again, I get nothing:

$ echo $RUBYOPT

With that taken care of, I'll run the RVM installation script again, and this time I get a much shorter output with no warnings:
$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
Successfully checked out branch ''
Current branch master is up to date.
Successfully pulled (rebased) from origin 

  RVM:  Shell scripts enabling management of multiple ruby environments.
  RTFM: https://rvm.beginrescueend.com/
  HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)
  
Upgrading the RVM installation in /home/andy/.rvm/~/.rvm ~/.rvm/src/rvm
~/.rvm/src/rvm

    Correct permissions for base binaries in /home/andy/bin...
    Copying manpages into place.


Upgrade Notes

  * rvm_trust_rvmrcs has been changed to rvm_trust_rvmrcs_flag for consistency

  * Ruby package dependency list for your OS is given by:
    rvm notes

  * If you encounter any issues with a ruby 'X' your best bet is to:
    rvm remove X ; rvm install X

  * If you wish to have the 'pretty colors' again, set in ~/.rvmrc:
    export rvm_pretty_print_flag=1

  * If you see the following error message: Unknown alias name: 'default'
    re-set your default ruby, this is due to a change in how default works.

Upgrade of RVM in /home/andy/.rvm/ is complete.


Andy Sturges,

Thank you very much for using RVM! I sincerely hope that RVM helps to
make your work both easier and more enjoyable.

If you have any questions, issues and/or ideas for improvement please
join#rvm on irc.freenode.net and let me know, note you must register
(http://bit.ly/5mGjlm) and identify (/msg nickserv  ) to
talk, this prevents spambots from ruining our day.

My irc nickname is 'wayneeseguin' and I hang out in #rvm typically

  ~09:00-17:00EDT and again from ~21:00EDT-~23:00EDT

If I do not respond right away, please hang around after asking your
question, I will respond as soon as I am back.  It is best to talk in
#rvm itself as then other users can help out should I be offline.

Be sure to get head often as rvm development happens fast,
you can do this by running 'rvm get head' followed by 'rvm reload'
or opening a new shell

  w⦿‿⦿t

    ~ Wayne

That's it for step one. Now on to step 2.

Step 2: Load RVM into your shell sessions as a function

The shell session (remember our shell is called "bash," which is the default shipped with Ubuntu) can be changed by modifying the file ~/.bash_profile, which is loaded each time a shell is started. We could edit it with the command:
$ vim ~/.bash_profile
but the RVM installation instructions give us a one-line terminal command that will append the correct line to the correct file, so just do it their way:
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
This adds the line "[[ -s "$HOME/.rvm/scripts/rvm" ]]" to the file ~/.bash_profile. That's it for step two.

Step 3. Reload shell configuration & test

As in the instructions, reload your bash profile:
$ source .bash_profile
Then type the following command, after which you hope to see the output "rvm is a function":
$ type rvm | head -1
rvm is a function
This worked for me once, but after I closed and re-opened the terminal, I got the following output:
$ type rvm | head -1
rvm is /home/andy/bin/rvm
The problem, I believe, lies in the difference between .bash_profile and .bashrc: the first runs only when you log in, and the second runs every time you open a terminal (shell) window. If we add the line in question to the file .bashrc, I get the desired output every time I run the type command. We can add line to the .bashrc file by slightly modifying and then re-running the command, replacing ".bash_profile" with ".bashrc":
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bashrc
Now if I run the command type rvm | head -1, I get the correct output each time. Not sure if this is the proper "fix", but there it is. [Edit: Wayne (RVM author) points out that some developers place non-interactive environment setup in .bashrc and then source it from .bash_profile.]

Now run the command rvm notes to see if RVM has any dependencies:
$ rvm notes


Notes for Linux ( DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.10
DISTRIB_CODENAME=maverick
DISTRIB_DESCRIPTION="Ubuntu 10.10" )

NOTE: 'ruby' represents Matz's Ruby Interpreter (MRI) (1.8.X, 1.9.X)
             This is the *original* / standard Ruby Language Interpreter
      'ree'  represents Ruby Enterprise Edition
      'rbx'  represents Rubinius

bash >= 3.2 is required
curl is required
git is required (>= 1.7 recommended)
patch is required (for ree and some ruby-head's).

If you wish to install rbx and/or Ruby 1.9 head (MRI) (eg. 1.9.2-head),
then you must install and use rvm 1.8.7 first.

If you wish to have the 'pretty colors' again,
  set 'export rvm_pretty_print_flag=1' in ~/.rvmrc.

dependencies:
# For RVM
  rvm: bash curl git

# For Ruby (MRI & ree)  you should install the following OS dependencies:
  ruby: /usr/bin/apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev

# For JRuby (if you wish to use it) you will need:
  jruby: /usr/bin/apt-get install curl g++ openjdk-6-jre-headless
  jruby-head: /usr/bin/apt-get install ant openjdk-6-jdk

# In addition to ruby: dependencies,
  ruby-head: subversion

# For IronRuby (if you wish to use it) you will need:
  ironruby: /usr/bin/apt-get install curl mono-2.0-devel
As you can see, I'm running Ubuntu 10.10, and it lists the following dependencies:
  • build-essential
  • bison
  • openssl
  • libreadline6
  • libreadline6-dev
  • curl
  • git-core
  • zlib1g
  • zlib1g-dev
  • libssl-dev
  • libyaml-dev
  • libsqlite3-0
  • libsqlite3-dev
  • sqlite3
  • libxml2-dev
  • libxslt-dev
  • autoconf
  • libc6-dev
  • ncurses-dev
That's a lot, but it gives us the full command, and if a package is already installed, then apt-get will just skip it, so we can copy the whole command and run it, prepending "sudo" and removing the "/usr/bin/" part:
$ sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'libxslt1-dev' instead of 'libxslt-dev'
Note, selecting 'libncurses5-dev' instead of 'ncurses-dev'
build-essential is already the newest version.
build-essential set to manually installed.
curl is already the newest version.
libncurses5-dev is already the newest version.
libncurses5-dev set to manually installed.
libreadline6 is already the newest version.
libreadline6-dev is already the newest version.
libreadline6-dev set to manually installed.
libxslt1-dev is already the newest version.
zlib1g is already the newest version.
zlib1g-dev is already the newest version.
zlib1g-dev set to manually installed.
git-core is already the newest version.
libc6-dev is already the newest version.
libsqlite3-0 is already the newest version.
libsqlite3-dev is already the newest version.
libxml2-dev is already the newest version.
libxml2-dev set to manually installed.
openssl is already the newest version.
sqlite3 is already the newest version.
The following extra packages will be installed:
  automake autotools-dev libyaml-0-2 m4
Suggested packages:
  autoconf2.13 autoconf-archive gnu-standards autoconf-doc libtool gettext bison-doc
The following NEW packages will be installed:
  autoconf automake autotools-dev bison libssl-dev libyaml-0-2 libyaml-dev m4
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,122kB of archives.
After this operation, 12.9MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://us.archive.ubuntu.com/ubuntu/ maverick/main m4 i386 1.4.14-3 [276kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ maverick/main autoconf all 2.67-2ubuntu1 [569kB]
Get:3 http://us.archive.ubuntu.com/ubuntu/ maverick/main autotools-dev all 20100122.1 [70.7kB]
Get:4 http://us.archive.ubuntu.com/ubuntu/ maverick/main automake all 1:1.11.1-1 [608kB]
Get:5 http://us.archive.ubuntu.com/ubuntu/ maverick/main bison i386 1:2.4.1.dfsg-3 [468kB]
Get:6 http://us.archive.ubuntu.com/ubuntu/ maverick-updates/main libssl-dev i386 0.9.8o-1ubuntu4.4 [2,013kB]
Get:7 http://us.archive.ubuntu.com/ubuntu/ maverick/main libyaml-0-2 i386 0.1.3-1 [53.8kB]
Get:8 http://us.archive.ubuntu.com/ubuntu/ maverick/main libyaml-dev i386 0.1.3-1 [63.3kB]
Fetched 4,122kB in 3s (1,227kB/s)   
Selecting previously deselected package m4.
(Reading database ... 169680 files and directories currently installed.)
Unpacking m4 (from .../archives/m4_1.4.14-3_i386.deb) ...
Selecting previously deselected package autoconf.
Unpacking autoconf (from .../autoconf_2.67-2ubuntu1_all.deb) ...
Selecting previously deselected package autotools-dev.
Unpacking autotools-dev (from .../autotools-dev_20100122.1_all.deb) ...
Selecting previously deselected package automake.
Unpacking automake (from .../automake_1%3a1.11.1-1_all.deb) ...
Selecting previously deselected package bison.
Unpacking bison (from .../bison_1%3a2.4.1.dfsg-3_i386.deb) ...
Selecting previously deselected package libssl-dev.
Unpacking libssl-dev (from .../libssl-dev_0.9.8o-1ubuntu4.4_i386.deb) ...
Selecting previously deselected package libyaml-0-2.
Unpacking libyaml-0-2 (from .../libyaml-0-2_0.1.3-1_i386.deb) ...
Selecting previously deselected package libyaml-dev.
Unpacking libyaml-dev (from .../libyaml-dev_0.1.3-1_i386.deb) ...
Processing triggers for install-info ...
Processing triggers for man-db ...
Processing triggers for doc-base ...
Processing 1 added doc-base file(s)...
Registering documents with scrollkeeper...
Setting up m4 (1.4.14-3) ...
Setting up autoconf (2.67-2ubuntu1) ...
Setting up autotools-dev (20100122.1) ...
Setting up automake (1:1.11.1-1) ...
update-alternatives: using /usr/bin/automake-1.11 to provide /usr/bin/automake (automake) in auto mode.
Setting up bison (1:2.4.1.dfsg-3) ...
update-alternatives: using /usr/bin/bison.yacc to provide /usr/bin/yacc (yacc) in auto mode.
Setting up libssl-dev (0.9.8o-1ubuntu4.4) ...
Setting up libyaml-0-2 (0.1.3-1) ...
Setting up libyaml-dev (0.1.3-1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
As you can see, most of the packages were already installed, but it did find and install some new ones:
  • autoconf
  • automake
  • autotools-dev
  • bison
  • libssl-dev
  • libyaml-0-2
  • libyaml-dev
  • m4
I have no idea what those are, but who cares. They're free. [Edit: Wayne tells me they're for installing head versions of Ruby 1.9]

That's it for setting up RVM. Now we can start using it.

Install a new version of Ruby using RVM

I already had Ruby 1.8.7 installed on my laptop. Now I want to use RVM to install 1.9.2:
$ rvm install 1.9.2
Installing Ruby from source to: /home/andy/.rvm/rubies/ruby-1.9.2-p180, this may take a while depending on your cpu(s)...

ruby-1.9.2-p180 - #fetching 
ruby-1.9.2-p180 - #downloading ruby-1.9.2-p180, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 8609k  100 8609k    0     0   891k      0  0:00:09  0:00:09 --:--:-- 1642k
100 8609k  100 8609k    0     0   871k      0  0:00:09  0:00:09 --:--:--  871kruby-1.9.2-p180 - #extracting ruby-1.9.2-p180 to /home/andy/.rvm/src/ruby-1.9.2-p180
ruby-1.9.2-p180 - #extracted to /home/andy/.rvm/src/ruby-1.9.2-p180
ruby-1.9.2-p180 - #configuring 
ruby-1.9.2-p180 - #compiling 
ruby-1.9.2-p180 - #installing 
Removing old Rubygems files...
Installing rubygems dedicated to ruby-1.9.2-p180...
Retrieving rubygems-1.6.2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  236k  100  236k    0     0  1629k      0 --:--:-- --:--:-- --:--:-- 1957k
Extracting rubygems-1.6.2 ...
Installing rubygems for /home/andy/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
Installation of rubygems completed successfully.
ruby-1.9.2-p180 - adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
ruby-1.9.2-p180 - #importing default gemsets (/home/andy/.rvm/gemsets/)
Install of ruby-1.9.2-p180 - #complete 
So now I have installed Ruby 1.9.2. Next we have to tell RVM to switch to that version of Ruby:
$ which ruby
/usr/bin/ruby
andy@andy-laptop:~$ ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]
andy@andy-laptop:~$ rvm use 1.9.2
Using /home/andy/.rvm/gems/ruby-1.9.2-p180
andy@andy-laptop:~$ which ruby
/home/andy/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
andy@andy-laptop:~$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux] 

That's it. From here, head over the RVM page and start reading the RVM documentation.

Conclusion and summary for power users

On Ubuntu 10.10 running Ruby 1.8.7 and git, we installed and configured RVM, then used RVM to download and run Ruby 1.9.2. We can switch between Ruby versions by using the command rvm use. We followed the installation instructions exactly, except we added a line to the file .bashrc, instead of to the file .bash_profile, as was instructed. We also had to unset the RUBYOPT variable to remove an installation warning.

Summary of commands for power uses:

$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bashrc
$ source .bashrc
$ type rvm | head -1
rvm is a function
$ rvm notes
# For Ruby (MRI & ree)  you should install the following OS dependencies:
  ruby: /usr/bin/apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev
sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev
$ rvm install 1.9.2
$ rvm use 1.9.2
$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
Special thanks go to Wayne Seguin for RVM, and all who contribute code to it. Throw some money Wayne's way if you find RVM useful.
~fin~

2 comments:

  1. Thanks to Wayne for his review of this post.

    ReplyDelete
  2. fantastic yaar
    following command done my work..

    $ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bashrc

    Thanking you

    ReplyDelete