Nov 7, 2009

If you're running Ubuntu 9.10 and want to install Ruby on Rails I've put together a quick tutorial for you. Not tremendously much has changed since the last tutorial for Jaunty Jackalope. Unicorn is out and while I think it's nifty, I'm going to wait a little while before playing with it. For now my money is still on Phusion Passenger as being the right tool for the job. If all that seems fine, let's get to the details.

Step 1: As usual, the first thing we'll want to do is make sure your version of Ubuntu is up to date.:

Step 2: We'll be installing some software that needs to be built so we'll need to get packages required for compiling. With Ubuntu, 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. As of Karmic Koala, we're using MySQL 5.1:

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.5 but it never hurts to confirm.

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. If you want to deploy Ruby on Rails onto a server then it's time to setup Nginx and Phusion.

Step 7: As I mentioned earlier, Unicorn may be the new hotness but I'm going to wait a while before I put my customers on that stack. The folks responsible for Phusion Passenger(Hongli Lai & Ninh Bui) are incredibly smart and I think they'll look at Unicorn and make some improvements to Phusion to eek out even more performance. The short of it is that I'm still a big believer in Phusion so I'm going to install it for production and recommend that you do too:

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.

sudo gem install passengersudo 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.

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)

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

I really like the deployment recipe of Brightbox. It uses a combination of svn and cap, pretty oldschool but works better than any other deployement recipes around, although brightbox hosting is a bit costlier than the other ones.It does have one advantage over other VPS and such services though, saves you an effort to configure your prod environment, although i agree it wont take much time, but getting something out of the box is quite helpful

Running Ubuntu 9.10 32-bit under Windows 7 with VirtualBox. Fails when trying to do the 'gem install rails' and in fact gem cannot install anything at all. Just doesn't do anything.

After MANY hours of hair-pulling and head-scratching that it was a matter of switching the VM from NAT to Bridged networking. No clue as to why this is or why it trips up just this part of the process.

This page has saved me soooo much time, I always just go straight here whenever I'm setting up a new dev environment (way more often than one would expect) in karmic (and for jaunty before that) and never have issues, thanks so much!!!!