The real strength of Ubuntu/Debian system is its package manager which makes the upgrade and installs in the system a breeze. In this post I am going to talk about ways to install Ruby and Rails in Ubuntu system.
Perhaps the easiest way to install Ruby is to use the apt package manager. Well you can do
sudo apt-get install ruby
But it turns out that its a bad idea to use apt for installing Ruby in Ubuntu. Primary reason being that Ruby development goes real fast and Ubuntu package managers are bit slow in catching up with it. As of today Ruby 1.9.1 is available through apt-get, but Ruby currently is 1.9.2. Ok, that is one of the reasons for not using apt, the other major reason is that we need to have a mechanism to install and switch between multiple versions of Ruby and Rails and perhaps switch back and forth between them.
Say you have to work with Ruby 1.8.7 with Rails2.3 in one project and Ruby 1.9.2 with Rails3 in another one. Using apt to install various versions of the language makes this difficult. Enter RVM (Ruby Version Manager)
Ok, if you want to further convince yourself about reasons of not going with debian package manager read this.
A newbee in Ruby Land can be overwhelmed with the terminologies in this world, so let me first clear them before diving into the how-to’s of upping the RoR in Ubuntu machine
Gem – aka RubyGem is a package manager for Ruby. As apt is the package manager for Ubuntu you can think Gem is package manager for just Ruby Ecosystem. In apt you have .deb file, here we have .gem file which is called the gem file, gem is nothing but a packaged Ruby program or library. Analogous to Java World .gem file can be equated to .jar file. But gem provides command like gem install which you can use to install gems remotely (like apt-get install). Similar commands are there for remove.
RVM – Ruby Version Manager is a manager which helps in installing multiple versions of ruby language and gems. You can easily mix and match the language version with various gems and use them independently. It provides a neat way of separating the versions without any clashes. RVM does this through gemsets. Essentially it provides a compartment or work-area where you can start your work with Ruby.
Gemset – Gemset is like an environment provided by RVM. In a Gemset first you choose which version of Ruby to use and then you can add as many gems within that gemset. You can create as many gemsets as you want. For each gemset you choose the ruby version and add gems into the gemset.
Ok, I hope now you are comfortable with jargons, its time to dive deep into the installation steps.
Pre-reqs To get started make sure you have git and curl in your system. Assume all those into programming will already have that ready.
Install RVM Look through the steps for RVM Install here. Its pretty straightforward. Once we have the RVM, we can install Ruby and start creating gemsets.
To install ruby you have give command
rvm install 1.9.2
Here 1.9.2 denotes the Ruby Version.
Suppose you want to have 1.8.7 Ruby also in same system do
rvm install 1.8.7
Now that you have multiple versions of Ruby in your system, you have to decide which version to use, you can give
rvm use 1.9.2
or say to make 1.9.2 default
rvm --default use 1.9.2
Now that you have Ruby ready, you have option to create gemset or you can even ignore this and use the default (the global gemset). I feel its always better to start by creating a gemset, so that later on you can easily switch to different version if needed.
To create your own gemset give command
rvm gemset create foo
So foo is the name of your gemset. Next step would be to associate a Ruby Version to the gemset and start using it. For that start using gemset foo give
rvm use 1.9.2@foo
So you are saying that you want to use Ruby 1.9.2 with gemset foo. Note above command also switches you to the foo gemset. So from now on if you do anything it will be within the gemset foo. That is to say suppose you install any gem like rails it will be within the gemset foo.
To check the gems within gemset give
gem list --local
Ok! now to the crux, to install Rails just have to give
gem install rails
Yup! that is it, you are up and running with Ruby on Rails!
When you install Ruby using the rvm install command it pulls out the source code and compiles it and then installs it. So you have make sure that relevant libraries are there in the system, especially the zlib.
In one of the system, where I was trying out, didn’t have zlib so after doing all the steps when I tried to install gems with gem install command it was giving error
$ gem install rails
ERROR: Loading command: install (LoadError)
no such file to load -- zlib
ERROR: While executing gem ... (NameError)
uninitialized constant Gem::Commands::InstallCommand
This was quite vexing issue I tried to install zlib with apt-get still couldn’t resolve it. So if you face this problem you can do the following steps
$ rvm pkg install zlib
$ rvm remove 1.9.2
$ rvm install 1.9.2 --with-zlib-dir=$rvm_path/usr
Essentially what it does is installs zlib with rvm. Then we remove Ruby and then compile and install Ruby again.
The best approach to tackle this will be to do this step before start of the installation
$sudo apt-get install build-essential bison openssl libreadline5 libreadline5-dev curl git zlib1g zlib1g-dev libssl-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf
RVM installs and maintains all the gems and gemsets in your home directory under .rvm/ folder.
Here under ~/.rvm/gems folder you can find info about all your gemsets
drwxr-xr-x 2 arun arun 4096 2011-01-30 00:29 cache
drwxr-xr-x 7 arun arun 4096 2011-01-31 18:07 ruby-1.9.2-p136@foo
drwxr-xr-x 2 arun arun 4096 2011-02-01 05:32 ruby-1.9.2-p136@foo1
drwxr-xr-x 7 arun arun 4096 2011-01-30 00:29 ruby-1.9.2-p136@global
drwxr-xr-x 2 arun arun 4096 2011-01-31 17:39 ruby-1.9.2p136@rails3tutorial
So there you can see the gemsets created by me. global is the default gemset created by rvm.
Suppose you want to download a gem and install it. Copy the downloaded gem file to
Then you can give
Suppose you want to have Ruby RSS gem. Download it and copy it to $GEM_HOME. Then give gem install. The steps are listed below
gems>cp /media/harddisk/downloads/rubyrss-1.1.gem $GEM_HOME
bin cache doc gems rubyrss-1.1.gem specifications
ruby-1.9.2-p136@foo>gem install rubyrss-1.1
Successfully installed rubyrss-1.1
1 gem installed
Installing ri documentation for rubyrss-1.1...
Installing RDoc documentation for rubyrss-1.1...
ruby-1.9.2-p136@foo>gem list --local
While installing gems it’s better to avoid rdoc and ri doc which gets installed with the gem. These documentation consume space and increases time to install. Anyway most of us will be googling or consulting online docs while development so if needed we can avoid docs totally while installing any gem. For that give
gem install rubyrss-1.1 -d --no-rdoc --no-ri
Hope a new-bee would find this post useful and easily get on with RoR in Ubuntu.