Set up ruby-on-rails

How to install Ruby on Rails in Ubuntu 12.04 LTS

Ubuntu 12.04 LTS (Long Term Support) – Precise Pangolin has already released, and you might have upgraded from 11.10 or installed Ubuntu 12.04 LTS. In this post you will learn how to setup Ruby on Rails on a newly installed Ubuntu 12.04 LTS.

Ruby on Rails is a very popular web development framework, it is based on the principle of  “Convention over Configuration”. Although, application development using Rails is quite easy and fun but setting up rails development environment may be difficult and frustrating, specifically for beginners. I had already written a post about setting up rails on Ubuntu 11.10  but few things has changed and need to be updated for the new version of Rails, Ruby and Ubuntu.

“Hassle Free” Rails Installer for Ubuntu 12.04

Although I’ve explained the installation steps in detail but if already know how these things work then better save time by running the script – that will install Ruby on Rails on Ubuntu 12.04 along with the dependencies and RVM.

Download the Rails Installer Script (From Github)

First change some settings in Gnome Terminal. Go to Edit -> Profile Preferences -> Title and Command and check the “Run Command as login shell”  box.

Login Shell - Terminal

Then make the script executable and execute it (you may have to enter the login password once) –

sudo chmod +x rails-installer.sh
./rails-installer.sh

If anything goes wrong then let me know through comments.

Step by Step Instruction for Setting Up Rails on Ubuntu 12.04 LTS

Just follow these simple steps and withing a couple of minutes you would be creating some nice applications (and of course using Rails).

step 1.  Install git and cURL

First of all, update your package repository.

sudo apt-get update

git is a simple, fast and efficient version control system. It is easy to learn, so even if you don’t have any experience with git you can try it in your next rails project (or any other project). you will love it.

sudo apt-get install git

Curl is a simple command line utility for getting file over web protocols, based on libcurl. To install curl simply execute –

sudo apt-get install curl

step 2. Install RVM and Dependencies

RVM is not strictly required but it makes ruby management a lot easier. You can try different implementations of ruby, different versions of ruby and all without any pain. So it’s strongly recommended. but RVM requires the command to be executed as login shell, so open a terminal and go to Edit -> Profile Preferences -> Title and Command and check the box that says “Run Command as a login shell“. (look at the above snapshot)

curl -L get.rvm.io | bash -s stable

Now, you must load the RVM

source ~/.rvm/scripts/rvm

Then install additional dependencies specified by the RVM –

rvm requirements

sudo apt-get -y install build-essential openssl libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

Installing Javascript Runtime
In newer version of Rails, you also need a Javascript runtime. Although you could install it from the package repository but it’s very outdated. So I recommend installing it using the PPA. (and I’ll also update the script)

sudo apt-add-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

step 3. Install Ruby

Now, you got RVM running, installing and running multiple versions or just one version of Ruby is very simple. To install Ruby just pass the version number to rvm install command (or some other implementation of ruby if you want; RVM also supports rbx, ree, JRuby, IRonRuby other than the default MRI) –

rvm install 1.9.3

Then select the Ruby version you want to use, (or make it default so that you don’t have to select it again in new session)

rvm use 1.9.3 --default

Now, you can check the version of ruby, you’re running right now-

ruby -v

step 4. Install Rails

RVM installs ruby as well as the gem utility (managing ruby libraries). To install rails, simply install it using the gem. It will automatically install the latest version unless you specify the version explicitly.

gem install rails

Now You’re ready to go.

rails new app_name
cd app_name
rails server

Now, open a browser and go to http://localhost:3000. :)

Voila :)

You should also check out this Ruby on Rails Interactive Video Course on Treehouse.

Note : When your Rails Application is ready, you may want to deploy it to a real server, I recommend Digital Ocean (@$5/mo, you get 512 MB RAM, 20 GB SSD and 1TB Bandwidth, cool, isn’t it.) for that, also checkout the Digital Ocean review if you want to know more about them. If you need any help then read this article about Deploying Rails Application to VPS.

Update 1 : Added a video as a reference for Rails Installer Script.

Update 2 : checkout my new rails application: Railyo – Rails freelancing jobs for cool developers!

  • Kleber Shimabuku

    Works nice! Thank you!

  • Aris Turner

    I am getting this error when installing rails. I can’t seem to figure out what’s wrong

    ……….Installing Rails gem…………….
    ERROR: Loading command: install (LoadError)
    cannot load such file — zlib
    ERROR: While executing gem … (NameError)
    uninitialized constant Gem::Commands::InstallCommand

    • Anil

      How did you get this resolved. It keeps complaining even after reinstalling .

    • http://tylerzika.com/ Tyler Zika

      I’m getting this error too. Any was to resolve it?

    • Saju

      try
      sudo apt-get install zlib1g-dev
      rvm reinstall 1.9.3

    • aaroncp

      After you run ‘rvm requirements’ copy the results of each of the entries and paste them as a command to be run. That should solve your problem.

      • aaroncp

        ahh actually not quite. You have to say
        $ rvm pkg install zlib
        $ rvm reinstall 1.9.3
        and it should then work. But do make sure to do the first thing I said as well.

  • guy

    great script thank you

  • Diego

    Thank you. I’m followed the tutorial and seems to be ok.

  • Shobit

    Thanks for the script. But it did not seem to install rails. After the “Installation Complete” when I typed in rails -v, it said rails is not currently installed. Then I manually installed it using sudo apt-get install rails. What could be the reason?

    • rkjha

      may be because you’re not running command as a login shell. (it’s required, because the script installs ruby through the RVM).

      • aldo

        well i DO am and still get this error.. same for gem and ruby, nothing is getting installed

  • Josh

    Thank you!

  • Horozco

    Thanks it’s Good XD

  • Romuald TISSERAND

    Hi, I’ve got some troubles with the script, but the step by step works well with 12.10 too. Helps a lot, thanks.

  • Charlie

    Worked perfectly. Thank you!

  • Charlie

    By the way, for all the people in the comments claiming that its not getting installed, tell them to restart their bash shell, so that the .bash_profile entry will take effect. Otherwise, it will say rvm is not installed.

  • Quan To

    Will the script work under proxy?
    Usually, I have to add –proxy option when running curl and bash

  • Dan

    Thanks! this was really helpful!

  • http://twitter.com/patscan1 Pat Scanlan

    Works great from Lubuntu 12.0.4. Couldn’t find out how to “run command as a login shell”, because I only had LXTerminal installed (not Gnome). So I did the step-by-step instructions and it works flawlessly.

  • Alkerzor

    Under Kubuntu (therefore KDE) you will be using Konsole by default. To make Konsole act as a login shell go to Settings > Configure Current Profile and change the command option from “/bin/bash” to “/bin/bash -l” (that’s a lowercase “L” as in “Login”).

    Also, “your nest rails project” should be “your next rails project” :-)

  • http://twitter.com/knavely knavely

    There is no checksum for ‘https://rvm.io/binaries/ubuntu/12.04/x86_64/ruby-1.9.3-p327.tar.bz2?rvm=1.16.20’ or ‘bin-ruby-1.9.3-p327.tar.bz2’, it’s not possible to validate it.

    If you wish to continue with unverified download add ‘–verify-downloads 1’ after the command.

    Downloading https://rvm.io/binaries/ubuntu/12.04/x86_64/ruby-1.9.3-p327.tar.bz2 failed.

    :-(

  • Mudit Sethia

    Man … this was the best tutorial I came across and really it was so easy to install ruby on mY Ubuntu 12.10,though with a couple of errors that google guided me through . Very Straight and no rubbish .

  • SY

    I really appreciate your work in this script and I’ve used this several times before and it ran smoothly, but I was stuck with an error this time installing rails gem. The following is the log:

    ……….Installing Rails gem…………….

    file ‘lib’ not found

    file ‘lib’ not found

    ………Done

    I’ve tried gem install rails, but it doesn’t seem to work.

    Cheers,

  • aaroncp

    One thing you should really include is the instruction to copy and paste the results you get from the ‘rvm requirements’ command and run them as a command. Currently your instructions say only to run ‘rvm requirements’ but not what to do after.

  • mikothas

    Très bon tutoriel; Merci beaucoup

    • rkjha

      accueil :)

  • Carla

    thanks a lot!