Friday, May 11, 2012

Installing Ruby on Rails on Ubuntu

So finally, after having waited in my to-do list for reasonably long time, Ruby and Rails is installed on my Ubuntu 12.04 (the proven, practical and precise OS). Yes, I did a fresh install of Ubuntu 12 after the upgrade from Ubuntu 11 rendered the OS respond slow (Ubuntu team, are you reading this?). But, I am more excited about Ruby on Rails. If you haven't heard of it yet, you need to catch up. Visit its homepage here.

This post is going to talk about installing Ruby on Rails (I will call it Rails henceforth going by the common term) which can be a real pain at times. And sadly, there are not many good documentation out there on it.

During my writing of this blog, the current versions I used are Ruby 1.9.3 and Rails 3.2.3. This post shows how to install Ruby, Rails and finally the Bundler using RVM (Ruby Version Manager). However, there is a simple way to install the entire package called the Railsready script that installs all the required packages at one go, and is quite simple. I wanted to get into the nitty-gritties! ;)

You need to do some checks first:

  1. You need to make sure you have sudo access to the system.
  2. We will not be using apt-get for the installations. Its outdated and can be quite problematic when installing Rails. Instead, we will use aptitude.
Step 1: Check if aptitude is installed in your system
Type the following command in the terminal.
$ sudo aptitude
in the terminal, and it should open the a visual interface. If you get errors, you have to install aptitude by executing the following commans.
$ sudo apt-get update
$ sudo apt-get upgrade
4 sudo apt-get install aptitude

Step 2: Update the libraries
You should update the libraries to make sure you have all the latest ones. This can save you from quite some trouble due to missing libraries during installation.
$ sudo aptitude update
$ sudo aptitude upgrade
Upgrade will take quite some time. Grab a coffee!

Step 3: Install Git and Curl
Since we will be using RVM (Ruby Version Manager), we need to install Git and Curl, if we do not already have them. Git is the source control repository (as you might know by the GitHub fame) nd Curl is a command line tool to get data from a URL.
$ sudo aptitude install build-essential git-core curl

Step 4: Install Ruby Version Manager
Ruby Version Manager (RVM) is very helpful to manage different versions of Ruby on a system. Using RVM is highly recommended (by me!).

To geth the RVM files to our system, we execute the following command at the bash shell.
$ bash < <(curl -s
Instead of the above command, you can also type:
$ curl -L  | bash -s stable

Step 5: Make RVM load with terminal
To make RVM load itself everytime we open the terminal, we need to edit the bash config file.
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
Now, close and open the terminal again. Or you can get the same effect by this command:
. ~/.bashrc
To check RVM is installed and to read the notes, you can execute:
$ rvm notes

Step 6: Install packages required for Ruby
After RVM is installed, its time to install Ruby. But you need to make sure we have all the packages that are required by Ruby (it's better to install all these packages now and avoid a headache later on!).

To check what the requirements are, you can execute:
$ rvm requirements
There should be quite some packages enlisted. Some of them, if they are already installed, like git and curl, will not be installed again.

To install the packages, execute:
$ sudo aptitude install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison

Step 7: Install Ruby (1.9.3 in this case)
With all preparations done now, we can go ahead to install Ruby. I used version 1.9.3.
$ rvm install 1.9.3
This should install Ruby just fine. If you have multiple versions of Ruby on your machine, you can make use this version by:
$ rvm use 1.9.3
, or even better, make this version as default:
$ rvm --default use 1.9.3
Check which version of Ruby you are using by,
$ rvm -v

Step 8: Install Rails
Time to install Rails now. This is the main application framework, one that changed the world to an extent! ;)

To install rails, we will use gems, the standard package manager for Ruby. You do not need to use sudo for installing gems.
$ gem install rails -v 3.2.3
Check the version of rails that was just installed.
$ rails -v
Congratulations, your installation is done. You can try out now creating a new Rails project.

Step 9: Create a new Rails project
Lets create a new Rails project. My site is called "hellosite".
$ rails new hellosite
This should start the bundler, that is responsible for managing application's dependencies. In case, the bundler doesn't start, you can go to the directory of the project (in our case "hellosite") and run the bundler.
$ cd hellosite

$bundle install
Go to the project's directory and you should see the project created with all different folders e.g. models and controllers and views....we will talk about them in some other post! ;)

Step 10: Start the Rails server
$ rails server
This command will start the default Rails server called WEBrick.

In case, you get an error like "ExecJS::RuntimeUnavailable", you need to get some packages installed:
$ gem 'execjs'

$ gem 'therubyracer'
Then go to the project directory and run the bundler again.
$ bundle install
and then start the Rails server using,
$rails server
The WEBrick server runs on port 3000 by default. Once the server is started, you can open your favorite browser, type "http://localhost:3000" and your site should open. There you go, you first Rails project is online! ;)

Eh, btw, the server is still running. To stop the server, press Ctrl+C! ;)

I will follow with more posts on this. But an important disclaimer before I close, trying out these commands on your machine should be done at your risk and do not hold me responsible if anything goes wrong. I had these two handly references that made my task easy, and these references have more comprehensive info. You can also refer to Ryan Bigg's installation guide or get the pdf from Mircea Goia's post for more.