Installing RoR in Ubuntu

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.

Some Terminologies
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.

RVMRuby 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.

Installation

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

Creating Gemsets
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!

Potential Pitfalls
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

Moving Ahead

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

gems>pwd
/home/arun/.rvm/gems
gems>ls -l
total 24
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 $GEM_HOME.
Then you can give
gem install

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
gems>cd $GEM_HOME
ruby-1.9.2-p136@foo>ls
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
...........
rspec-mocks (2.3.0)
rspec-rails (2.3.0)
rubygems-update (1.4.2)
rubyrss (1.1)
.........................

TIP
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.

Great References
http://everydayrails.com/2010/06/28/rvm-gemsets-rails3.html
http://ryanbigg.com/2010/12/ubuntu-ruby-rvm-rails-and-you
http://rvm.beginrescueend.com/

Advertisements

7 thoughts on “Installing RoR in Ubuntu

  1. Good one, mate. You have covered most of the stuff, but I would have liked to have some introduction of why ruby or rails (greedy, ain’t it?). But even as it is , this will be useful for any one who is already on Ubuntu and wants to get started with Rails.

    Hope this would be the first of many posts to come about Ruby and Rails ecosystem.

  2. Why RoR can be a post onto itself. This is just Install part I had spent lot of time figuring this out. Esp. that zlib problem and all.

  3. Ah, ok, we the readers will wait for that. Since I am not on Ubuntu, I had a different share of problems to tackle on Windows :). Not sure whether I will get the time to blog about it or not. Plus, who is using Rails on Windows, anyways? πŸ™‚

  4. Thanks, mate. Made it easier for me to get Rails up and running on Ubuntu. Good work since I encoutered the zlib error despite doing Step 1, and since the workaround was handy, it saved me a lot of time. For Step# 2 I think you should mention that though the post talks about 3 sub-steps, we need to follow only the first 2 if we are following your post. Finally, it would be better if you can change this to bullet points so that it is sort of easier to follow (you can still have the explanations, but seeing Step 1/2/3 is a bit reassuring).

  5. Pingback: Installing RoR in Ubuntu Β« Something Bizzare | γ‚’γƒ—γƒͺγ‚±γƒΌγ‚·γƒ§γƒ³ι–‹η™Ίθ€…γ―γ€γƒ­γ‚°γ«θ¨˜ιŒ²

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s