Apr 20, 2009

Install Ruby Rails on Ubuntu 9.04 Jaunty Jackalope

It's getting easier to install Ruby on Rails all the time. I thought about skipping actually writing this tutorial until I heard that Phusion Passenger 2.2.0 supported Nginx. I was pretty psyched and so ran off to do an install with that new stack. If you want a head start on it or need a step by step tutorial then by all means click through for instructions.

Update: Version for Karmic Koala now posted.

Step 1: As usual, the first thing we'll want to do is make sure your version of Ubuntu is up to date.:
sudo apt-get update
sudo apt-get dist-upgrade

Step 2: We'll be installing some software that needs to be built so we'll need to get packages required for compiling. Luckily, you can type this single command and get everything you need:
sudo apt-get install build-essential

Step 3: Once you've got the tools, it's time to install MySQL and Ruby. If you're using SQLite you may not need all this stuff. Otherwise, just copy and paste this command into your terminal if you're in a hurry.
sudo apt-get install ruby ri rdoc mysql-server libmysql-ruby ruby1.8-dev irb1.8 libdbd-mysql-perl libdbi-perl libmysql-ruby1.8 libmysqlclient15off libnet-daemon-perl libplrpc-perl libreadline-ruby1.8 libruby1.8 mysql-client-5.0 mysql-common mysql-server-5.0 rdoc1.8 ri1.8 ruby1.8 irb libopenssl-ruby libopenssl-ruby1.8 libhtml-template-perl mysql-server-core-5.0

If you hadn't previously installed MySQL you'll be asked to set a root password. You don't have to, of course (it will bug you no fewer than 3 times if you opt not to) but I strongly recommend it. You'll need this password when you populate your rails config/database.yml file so be sure not to forget it.

Step 4: Grab the latest ruby gems and install them. As always be sure to check rubyforge.org to make sure you're grabbing the latest one. As of this writing it's 1.3.4 but it never hurts to confirm.
wget http://rubyforge.org/frs/download.php/57643/rubygems-1.3.4.tgz
tar xvzf rubygems-1.3.4.tgz
cd rubygems-1.3.4
sudo ruby setup.rb

Once it's done you can remove the .tgz file and erase the rubygems-1.3.4 directory too.

Step 5: On the command line, type gem -v. if you get this message we need to make some symlinks:

The program 'gem' can be found in the following packages:
* rubygems1.8
* rubygems1.9
Try: sudo apt-get install
-bash: gem: command not found

Let's create those symlinks now:

sudo ln -s /usr/bin/gem1.8 /usr/local/bin/gem
sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby
sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc
sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri
sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb

Step 6: Install Ruby on Rails! You can leave off the --no-rdoc and --no-ri switches if you're on a machine with plenty of ram. But just in case you've got a more modest setup (say 256MB or less) I'd just use the following:
sudo gem install rails --no-rdoc --no-ri

If you're just doing local development then you are basically done. You may want to install additional gems (such as mongrel). If you want to deploy Ruby on Rails onto a server then it's time to setup Nginx and Phusion.

Step 7: The folks responsible for Phusion (Hongli Lai & Ninh Bui) have done a remarkable job. They truly understand what it means to make using your product easy for customers. It's one reason we love Phusion. Nginx doesn't support loadable modules like Apache does, so Phusion will download, compile, and install it for you. What could be easier? To get started, let's download the packages we'll need:

sudo apt-get install libc6 libpcre3 libpcre3-dev libpcrecpp0 libssl0.9.8 libssl-dev zlib1g zlib1g-dev lsb-base

Step 8: We're going to create a directory for your application. In anticipation of Capistrano, let's put it in /var/www/myapp/current. Swap out the myapp with anything you like.
sudo mkdir -p /var/www/myapp/current

If you've got an existing Rails application, it doesn't hurt to populate that directory now. Just make sure that the root to the public directory is /var/www/myapp/current/public. Don't forget to chown it for www-data:

sudo chown -R www-data:www-data /var/www/myapp/current/

Step 9: Now it's time to install Phusion Passenger and let it do it's magic. It's critical that you have Phusion Passenger 2.2.1 or greater installed. This may not work with 2.2.0.

sudo gem install passenger
sudo passenger-install-nginx-module

At this point you'll be greeted with two options. The first will allow Passenger to do all the work while the second, for advanced users, allows you to point Phusion to your pre-installed Nginx. We're going to make life easy on ourselves and choose option 1. I'd also recommend keeping the default path (/opt/nginx).

The Nginx configuration file (nginx.conf) file is in /opt/nginx/conf/ They've done you the favor of adding the lines you need to get going. Defining the configuration file is beyond the scope of this tutorial, but I've included a sample file which you can download. This file assumes your application is in /var/www/myapp/current.

Sample nginx.conf file for Ruby on Rails, Nginx, Phusion Passenger.

Step 10: Time to make some Nginx tweaks for Ubuntu/Debian. If you're used to running nginx as installed by Debian or Ubuntu then you might notice that you've lost the familiar way to start and stop nginx. Gone is /etc/init.d/nginx stop and /etc/init.d/nginx/start. To get it back, copy (or download) this into a file called nginx in /etc/init.d/ (as root)
#! /bin/sh

# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon


test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx

set -e

case "$1" in
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \
echo "$NAME."
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/opt/nginx/logs/$NAME.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
/opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
exit 1

exit 0

Next, let's set the permissions and make nginx load on a reboot:

sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults

Now you should be able to start and stop nginx using the command you're used to:

sudo /etc/init.d/nginx start

Bonus step In case it's not already installed, let's grab the perquisites and then install the mysql gem. This will improve performance on your webserver:
sudo apt-get install libmysqlclient-dev
sudo gem install mysql --no-rdoc --no-ri

Troubleshooting If things didn't go smoothly, here are some things to check:

1. There's an error log for nginx in /opt/nginx/logs/ Try looking to see if you get any hints there.

2. If you see a 403 forbidden error, make sure you have permissions set correctly. I'm running Nginx as www-data and I've set the group/user owners of my rails app to match using the chown -R www-data:www-data command. It doesn't hurt to confirm it.

3. Try running your rails app in development mode using the standard script/server. If it doesn't work there then it obviously won't work in production mode either

For help, you should check out the great railsforum or Mailing list front-end.


  1. cool. nice job. one thing -- i think your nginx config is slightly out of date -- passenger gem is now version 2.2.2 not 2.2.1

  2. Awesome tutorial. Worked perfectly. Thanks for the help.

  3. super-easy install, and working!

  4. Great tutorial! It's been so easy for me as a beginer with Ubuntu 9.04, to handle with it. But after that I've install Aptana RadRails plugin to my Eclipse(PDT), and now I have a problem because rails is not detected into Window/Preferences/Rails. Manual setting up of it path also failed. And now I am unable to create new RadRails project. View/RI is also empty. Any idea?


  5. Everything work fine, but after a reboot if i try to connect to one of my applications i get 502 Error bad gateway... but if i manually restart nginx, then everything start to work again... so i need to manually restart nginx after every reboot. Someone have a similar problem with 9.04 x64? I'm testing it in local.

  6. Great job, really appreciated this quality tutorial!

  7. Sorry for the noobs question... when i install ubuntu 9.04 i dont' have a www-data user... should i create one?

  8. STEP 3, getting an error:

    sudo apt-get install ruby ri rdoc mysql-server libmysql-ruby ruby1.8-dev irb1.8 libdbd-mysql-perl libdbi-perl libmysql-ruby1.8 libmysqlclient15off libnet-daemon-perl libplrpc-perl libreadline-ruby1.8 libruby1.8 mysql-client-5.0 mysql-common mysql-server-5.0 rdoc1.8 ri1.8 ruby1.8 irb libopenssl-ruby libopenssl-ruby1.8 libhtml-template-perl mysql-server-core-5.0

    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    E: Couldn't find package ri

    I removed "ri" but then I get
    "E: Couldn't find package rdoc"
    Which is the next item in the list.

    Any ideas?

  9. Ok, I got it.
    I'm using Linode and I edited my source list
    and uncommented a bunch of lines. Then I ran
    apt-get update
    I went back to step 3 and everything installed

  10. Awesome. Do you have any idea?

  11. Any idea how to make capistrano's deploy:web:disable task work with nginx/passenger?

  12. Thanks for posting...It's now working here in my newly upgrade Jaunty Jackalope..

    I can now enjoy using Ruby Rails. As my first step in programing.

    More power!!


  13. Hi,

    thanks for the cool guide.
    I am at step #9,

    issuing :
    wget http://vince71.googlepages.com/nginx.conf
    nginx.conf .1: Permission denied

    i can open the file by, but on a read only
    vi nginx.conf

    what is the command on removing readonly attributes to the file?

    is Copy and Paste will work on command shell?

  14. Terrific post. Thank you!

  15. Thanks for this, I'm new to Ubuntu, trying it out and wanted to see what Rails is like as a non-softie. You made that pretty headache-free.

  16. Great instructions - latest ruby gems at time of writing this is now 1.3.4 found here: http://rubyforge.org/frs/download.php/57643/rubygems-1.3.4.tgz

  17. Very helpful. I know you might think this is a pretty straightforward procedure but for those of us new to the environment this sort of tutorial is tremendously helpful. I appreciate you taking the time to document it. Kudos.

  18. very good although I got:

    E: Package libmysqlclient-dev has no installation candidate

    had to use: libmysqlclient15-dev

    then the gem install worked
    Very good article!!!

  19. Great walkthrough, saved me tons of headache!

  20. Will this version of nginx (installed this way) run php?

  21. Thanks for the post.
    Worked perfectly.

  22. I get -->

    E: Couldn't find package ri

    Same for rdoc, libmysql-ruby, etc..

    I uncommented all the required lines in the sources.list and did apt-get update.. Can you share your sources.list with me ?

  23. Excellent post, Nginx is working perfectly well, just as i was looking for, thanks a lot!!!

  24. Thanks for the tutorial to answer the question about whether php still works, I had php installed previously.

    PHP seems to work, but it can no longer connect to MySQL. I get the following error:

    Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /var/www/tmb08/php/class/CDB.php on line 34
    Err:Conn Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

  25. How do you deal with gem upgrade, for example say from 1.3.4 to 1.3.5?


  26. To nobody:

    For permission denied errors, you(as a regular user) are trying to overwrite a protected system config file.

    With the vi command, same thing. You can see it, but don't even try to play with the contents.

    If you want to do either command and have it run with the expected results, just put a sudo in front of the command. The system will ask for your password again and do whatever command followed as the superuser. This means no protection from any results. So make sure the command is what you really want to do, there are no do-overs here.

    Have Fun


  27. Installed smoothly. Had one issue that Rick Windham and his posted fixed it for me.(Thanks Rick). And thank you very much for putting this walk-though togeather. I wouldn't have tired it with out.


  28. Arg, took me a lot longer, but I had trouble with Passenger. I already had nginx 0.8.13 installed and used the sources when asked. After much fluffing around, I found I needed to change passenger_ruby in nginx/conf/nginx.conf to "passenger_ruby /usr/local/my_ruby_wrapper.sh;" which is a script that loads the GEM_PATH before ruby...




  29. I have install rails/mysql5.1 on karmic by following your
    Blog. Would you be interested in that changes?

  30. Rails/Nginx newbie here :). Thanks for this awesome tutorial. Followed the instructions exactly and set up everything on my Ubuntu 9.04 machine. Created a rails test app...and I get the "Welcone aboard" message when going to http://localhost. Problem is that if I click the "About your application's environment" I get the error "The page you are looking for is temporarily unavailable. Please try again later."

    However, when I run the same test app in development mode "ruby script/server"...and go to http://localhost:3000 everything works fine.

    Any ideas? What am I doing wrong?


  31. Raveendran VadakkootNovember 6, 2009 at 2:17 PM

    I still get this error for rdoc and ri. Hence I am not able to proceed with the installation of "rubygems". Any idea what the problem is?

    ~$ sudo apt-get install rdoc
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    E: Couldn't find package rdoc

  32. Hi, try apt-get install rdoc1.8 ri1.8

  33. Wow! Everything's working fine! Thanks a lot for this tutorial!

  34. Very helpful sharing!!!

  35. Hi, Thanks for this document!
    But, I am getting this error while installing Passenger-2.2.11:

    $sudo passenger-install-apache2-module
    gcc -Iext -Iext/common -fPIC -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -I/usr/include/apr-1.0 -I/usr/include/apr-1.0 -I/usr/include/postgresql -DLINUX=2 -D_GNU_SOURCE -D_REENTRANT -I/usr/include/apr-1.0 -I/usr/include/openssl -I/usr/include/postgresql -I/usr/include/xmltok -pthread -I/usr/include/apache2 -D_REENTRANT -I/usr/local/include -Wall -g -DPASSENGER_DEBUG -DBOOST_DISABLE_ASSERTS -o ext/apache2/mod_passenger.o -c ext/apache2/mod_passenger.c
    rake aborted!
    Command failed with status (127): [gcc -Iext -Iext/common -fPIC -DLINUX=2 -D_...]
    (See full trace by running task with --trace)


    It looks like something went wrong

  36. Dude when it's time you'll go straight to heaven!

  37. Man, this is so incredibly helpful! Thanks so much! With your help I got it running! I am ready to move to my VPS. Cheers!

  38. normally you would want to use 'sudo apt-get upgrade' rather than 'sudo apt-get dist-upgrade'. This will get you the latest updates to any packages without upgrading the entire OS to the latest version.


  39. I'm a Windows user trying to find my footings on Ubuntu...thanks for this step-by-step instruction. It helped me setup RoR and nginx on my ubuntu 10.04. To be honest with you, I followed through the instruction blindly not knowing what I was doing. Now I can't get phpMyAdmin to work again on my computer and all my php projects seem not to be working. Can someone please rescue this poor Windows user?

  40. Very well written. You got every point explained. Loved the whole article! Many thanks!

  41. can you post the next step, btw i failed installing mysql tried sqlite hope it works

  42. thanks for giving instruction. could you also post a video for this?

    dickies scrubs

  43. hello. i thought centos has more easy step to install ruby. i think i need to change my os to centos after this .