Posted by Vince Wadhwani on Dec 07, 2006

Update March 05, 2007: Now using gems 0.9.2 by default in this HowTo. To upgrade your older gems see this article.

Update: May 09, 2007: There's now a version for Feisty Fawn!

A while back I wrote a HowTo for installing Ruby on Rails on Ubuntu's Dapper Drake edition. Well, some 6 months later Dapper is still the LTS release but many rails developers are longing to move on to Edgy Eft. While a simple apt-get update && apt-get dist-upgrade will take care of that for most people, it doesn't necessarily help those installing on Edgy Eft from scratch. That being said, the previous HowTo is still accurate. Still, I'm reposting so that people can post their experiences and recommendations (comments are disabled after 20 days to keep spam in check) below. Also, I've cleaned up a few things and removed some older package requirements. Note that this tutorial assumes you did not choose LAMP or DNS server during your install. Probably no harm if you did except we won't be using Apache to run Rails.. we'll be using Lighttpd.

1. Make sure the universe repository in /etc/apt/sources.list is uncommented:
deb http://us.archive.ubuntu.com/ubuntu/ edgy universe
deb-src http://us.archive.ubuntu.com/ubuntu/ edgy universe

Also uncomment the security lines:
deb http://security.ubuntu.com/ubuntu edgy-security universe
deb-src http://security.ubuntu.com/ubuntu edgy-security universe

2. Make sure you are up to date:
sudo apt-get update
sudo apt-get dist-upgrade

3. Install the ruby and mysql base packages:
sudo apt-get install ruby ri rdoc mysql-server libmysql-ruby

4. We need to grab the latest ruby gems from rubyforge. You may need to modify this command if a version after 0.9.2 is available.
sudo wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
tar -xvzf rubygems-0.9.2.tgz
cd rubygems-0.9.2
sudo ruby setup.rb

5. Now let's install Ruby on Rails!
sudo gem install rails --include-dependencies

6. We have Rails but we still need lighttpd. Like the last time, I've thrown in PHP too in case you want to manage your database with phpmyadmin like I do. Repeat after me, apt-get is your friend.. apt-get is your friend: (Thanks to Mitch Golden and Mike McKay for helping scrub this list.)

sudo apt-get install lighttpd php5-common php5-cgi php5-mysql php5-mysqli libfcgi-dev libfcgi-ruby1.8 phpmyadmin

I should mention that there are other popular choices for web server setups such as Apache2 with Mongrel and Pound. Personally, I'm not terribly worried about load balancing and massive scaling at this stage so I skipped it. My understanding is that once Apache2 hits v2.6 that it will no longer require pound in order to interface with Mongrel. At the same time, a bunch of issues with lighttpd are supposed to be solved at v1.5 (they are at 1.4.13 at the moment). I'm going to play wait and see before passing any judgment on my next setup.

7. Modify your /etc/lighttpd/lighttpd.conf file to set up your domains. Be careful because by default Ubuntu maps all the images so that they are taken from /usr/share/images instead of your/rails/root/public/images so if you're wondering why your rails app is working but your images aren't being displayed this is why. You can take out the offending code in your lighttpd.conf or just put your images in /usr/share/images. Here is a sample lighttpd.conf (rename the file and put it in /etc/lighttpd/) for you with the default behavior removed. I'm not a lighttpd expert so please post any corrections/improvements in the comments so I can incorporate them. Update: I would suggest you do not use this lighttpd.conf file until after step 10 else steps 8 & 9 will fail as your app is not set up yet! Thanks to Shinkan and xuehu for pointing this out.

8. Time to enable some modules for lighttpd. There are others that you may need but they should be enabled in your lighttpd.conf. I'm not a fan of enabling modules two different ways but I'm sure this will get cleaned up eventually.
sudo /usr/sbin/lighty-enable-mod fastcgi
sudo /usr/sbin/lighty-enable-mod proxy
sudo /etc/init.d/lighttpd force-reload

9. Let's make sure that there are no errors by stopping lighttpd and then starting it manually:
sudo /etc/init.d/lighttpd stop
sudo lighttpd -f /etc/lighttpd/lighttpd.conf

Update: I don't like it but if you get an error when you try to start lighttpd like: "(network.c.300) can't bind to port: 0.0.0.0 80 Address already in use" then use sudo killall lighttpd to stop lightty instead.

If you have errors there make sure you take a look and try to address them! The output you see will be your best bet in getting help from somebody on one of the forums.

10. Install some basic gems you might need to run your rails app:
sudo gem install actionmailer
sudo gem install activesupport
sudo gem install actionpack
sudo gem install actionwebservice

And now you should have a new, shiny, ready to go Ubuntu Edgy Eft server just waiting for your PHP or Ruby on Rails applications!

To test it out, go to your /var/www/ directory and type sudo rails myrailsapp It should now create a rails app for you in the /var/www/ directory. Next, sure that you have your web app permissions set correctly. You will need to run sudo chown -R www-data:www-data /var/www/myrailsapp to make sure lightty can read the webapp you've created.

Then stop and start lighttpd again to make sure we have no errors:
sudo /etc/init.d/lighttpd stop
sudo lighttpd -f /etc/lighttpd/lighttpd.conf

When you surf over to your server you should see the Welcome to Rails placeholder page waiting for you. Congratulations, that means you've done it right and are ready to roll! The only thing left to do before you get serious is set yourself a root password on MySQL (you can use phpmyadmin for that) and then configure your rails app (/var/www/myrailsapp/config/database.yml) with the correct permissions to use the correct database.

If you are serious about rails development, take a minute to register over at Working with Rails and recommend your favorite rails devs. Good luck and happy coding!