Install Ruby on Rails on Ubuntu Feisty Fawn
Posted by Vince Wadhwani on May 09, 2007
Update: Nov 16, 2007 - See this article for instructions for Gutsy Gibbon
This is a follow-up post the previous articles I wrote on installing Ruby on Rails for Ubuntu Dapper Drake and Edgy Eft. This time around, I've moved away from Lighttpd and am deploying using Mongrel and Nginx. Why? I had a ton of problems with Lighttpd, from unexpected crashes to unexpected behavior. I learned an important lesson which I'll share: When possible, develop and deploy using the same tools. Because Mongrel is so great, we can do just that. So let's get started!
As usual, the first thing you want to do before starting any installation on Ubuntu is enable the repositories you'll need and then update your system. You can either uncomment the repositories in /etc/apt/sources.list or use Synaptic to enable UNIVERSE.
deb http://us.archive.ubuntu.com/ubuntu feisty universe
deb-src http://us.archive.ubuntu.com/ubuntu feisty universe
sudo apt-get update
sudo apt-get dist-upgrade
2. We'll be installing some software that needs to be built this time around (don't worry it won't be scary) so we'll need to get packages required for compiling. In one swoop, you can type this command and get everything you need:
sudo apt-get install build-essential
3. Once you've got the tools, it's time to grab MySQL, my database of choice, and Ruby. Just copy and paste this command into your terminal if you're shady on spelling:
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
Update: Install Ruby 1.8.6 manually to get Mongrel Cluster 1.0.2 working!
4. We next need to grab the latest ruby gems from rubyforge. You may need to modify this command if a version after 0.9.4 is available.
sudo wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
tar xvzf rubygems-0.9.4.tgz
cd rubygems-0.9.4
sudo ruby setup.rb
5. Now let's install Ruby on Rails!
sudo gem install rails --include-dependencies
The first time I ran that, I got an error that said:
Bulk updating Gem source index for: http://gems.rubyforge.org
ERROR: While executing gem ... (Gem::GemNotFoundException)
Could not find rails (> 0) in any repository
Solution? Run it again. No idea why it always craps out, but it's happened to me on two different servers. Maybe it's a bug
6. Now, let's get nginx and fcgi:
sudo apt-get install libpcre3 nginx libfcgi-dev libfcgi-ruby1.8 libfcgi0c2
7. Like the previous HowTo's I wrote, this one will use phpmyadmin to manage the database manipulation. I still haven't gotten around to learning MySQL on the command line so I use phpymyadmin as a crutch. If you don't need it you can skip this step. Otherwise let's grab PHP. Note that we're not installing Apache2 because we already have Nginx.
sudo apt-get install libxml2 ucf php5-common php5-cgi php5-mysql phpmyadmin
8. We're almost done.. now it's time to get Mongrel.
sudo gem install mongrel
Select which gem to install for your platform (i486-linux)
1. mongrel 1.0.1 (mswin32)
2. mongrel 1.0.1 (ruby)
3. mongrel 1.0 (mswin32)
4. mongrel 1.0 (ruby)
5. Skip this gem
6. Cancel installation
> 2
Choose 2 unless a more recent (ruby) version of Mongrel is available. We'll say Y to all the dependencies.
Install required dependency daemons? [Yn] Y
Install required dependency fastthread? [Yn] Y
Now, if you skipped the part about getting the build-essential, you'll get an error that says: ERROR: Failed to build gem native extension. Go back and grab the build tools (sudo apt-get install build-essential) and try this step again.
sudo gem install mongrel_cluster
copy the init file over to /etc/init.d/ by typing this all on one line:
sudo /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster /etc/init.d/mongrel_cluster
Update: you may need to change the above command if a version of Mongrel Cluster > 1.0.2 exists
Next, add a path statement to mongrel_cluster file just above the CONF_DIR variable:
vi /etc init.d/mongrel_cluster
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local:/usr/local/sbin:/usr/local/bin
Thanks to Russ Brooks for the above tip. You may also want to change the USER=mongrel to USER=www-data.
Finally, let's modify permissions and make sure we boot mongrel on startup:
sudo chmod +x /etc/init.d/mongrel_cluster
sudo update-rc.d mongrel_cluster defaults
9. Congratulations, you've got everything installed! It's time to deploy. If you have an existing rails application on another server, let's move it over to our root at /var/www/
sudo mv myrailsapp /var/www/
10. Modify your permissions of your app and phpmyadmin:
sudo chown -R www-data:www-data myrailsapp
sudo chown -R www-data:www-data phpmyadmin
11. Setup the Mongrel Cluster (source). This will get us a group of 3 mongrel clusters running on port 8000. From within your myrailsapp/config folder, type: sudo mongrel_rails cluster::configure -e production \ -p 8000 -N 3 -c /var/www/apps/myrailsapp -a 127.0.0.1 \ --user mongrel --group mongrel
Now let's create a symlink to that file from within /etc where all our configs live:
sudo mkdir /etc/mongrel_cluster
cd /etc/mongrel_cluster/
sudo ln -s /var/www/myrailsapp/config/mongrel_cluster.yml
You can download a sample mongrel_cluster file HERE. In any case, it's a good idea to download it and cross reference it to what the above command produced.
12. Next we're going to put a script into /var/www/phpmyadmin folder to spawn fastcgi on port 8888. Make sure you give it execute permissions using sudo chmod +x fastcgi_script
Download the script here. Inspiration for this script came from Alexey N. Kovyrin. It has been modified only for Ubuntu's PHP path. Don't forget to put it into /var/www/phpmyadmin
13. We're *almost* done. Next step is to configure Nginx. Here's a sample nginx.conf file for your /etc/nginx/ folder. It's set up to handle one rails app and phpmyadmin. Adding additional servers just means more server blocks.
14. Now that we've got everything set up, let's turn this sucker on!
Step 1: PHP: . /var/www/phpmyadmin/fastcgi_scriptStep 2: Mongrel: /etc/init.d/mongrel_cluster start
Step 3: Nginx: /etc/init.d/nginx/ start
References:
hi Vince, thanks for your great howto, but I got a error: sudo apt-get install libpcre3 nginx libfcgi-dev libfcgi-ruby1.8 libfcgi0c2 Reading package lists… Done Building dependency tree… Done E: Couldn’t find package nginx
and I have uncomment the universe lines in source.list, a “apt-cache search nginx” returns nothing, did I make something wrong?
thank you!
hui
Hi Hui,
Do you have all your repositories enabled? Check packages.ubuntu.com to find out where the nginx package is..
-Vince
vince
sorry, my ubuntu is not feisty but dapper :( upgrading… thank you!
hui
deb http://us.archive.ubuntu.com/ubuntu dapper universe deb-src http://us.archive.ubuntu.com/ubuntu dapper universe
Shouldn’t this be feisty?
geira
Also note this:
http://www.mail-archive.com/debian-bugs-closed@lists.debian.org/msg114782.html
geira
Hi Geira,
It should be feisty, you’re right. I had copied/pasted from my previous HowTo.. I fixed it. Thanks!
vince
I followed your instructions. Everything has worked for me except nginx.
When I try and run “sudo apt-get install nginx” the package downloads properly, but I get an error that nginx fails to start; error:
Starting nginx: invoke-rc.d: initscript nginx, action “start” failed. dpkg: error processing nginx (–configure): subprocess post-installation script returned error exit status 1
Has anyone else encountered this?
Walker
Hello Vince,
this HowTo is great.. the system is working and performances are great.. but I can’t login into my application.. it’s weird.. maybe sessions disabled?
ET
mek
Walker: check to see if anything is already running on the port that nginx is configured to. If you’ve got Apache or Lighttpd taking up port 80 when you install nginx you may have a problem
Mek: It sounds like an authentication problem. If you’re using AAA or Restful authentication then try erasing your remember tokens.
vince
here we go.. my app was trying to load RMagik, was showing no error, just did not work… the system is running super-fast! WOW!
:) thank you
mek
Before following the steps here, I was getting the dreaded “ERROR: Failed to build gem native extension” for fastthread. Now I’m getting it when I select #2 for mongrel install. I did get the build-essential and it installed without error.
Anyone have clue why I’m having difficulty installing
Brian
Brian: did all the previous packages install correctly? There might be a required dev package that you’re missing (e.g. ruby1.8-dev)
vince
Vince: Yes, everything else installed correctly, or at least I don’t remember seeing any errors.
I already have rails 1.2.3, ruby 1.8.5 and mysql (the latest version).
I did not install nginx or fcgi as I didn’t want them - I assume they’re not required for mongrel to work - is that correct?
Webrick runs just fine, but I’d rather use mongrel instead as that is what I’ve used for development on my windows box.
Brian
Brian, can you check to see if you have the ruby-dev package? sudo apt-get install ruby1.8-dev
vince
You should upgrade this post to download RubyGem 0.9.3, the download link is here
http://rubyforge.org/frs/download.php/20585/rubygems-0.9.3.tgz
scott hodson
Hi Scott,
Updated.. thanks for the heads up!
vince
Vince: That did it! Thanks. One last assist please: what does nginx do for you - do you use it for the IMAP/POP server?
Brian
Hi Brian, glad it worked! I use nginx to serve PHP and Rails. For a mail server I use Postfix w/ Courier IMAP. I wrote a HowTo for that earlier (for Edgy.. haven’t updated it for Feisty yet). You can check that article out at
http://www.urbanpuddle.com/articles/2006/12/20/setup-a-postfix-mail-server-on-ubuntu-edgy-eft
vince
Hi Vince, Great HOW-TO for a newbie like me. I followed it and found no problems in installation, but two issues at the end:
1) I found two paths to rails app in the sample configuration file for Nginx provided, and modified them, but how do I invoke the application? It looks like ‘phpmyadmin’ is mapped to /, so all searches are done from there…
2007/05/21 22:05:44 [error] 10713#0: *9 “/var/www/phpmyadmin/rails/Demo/index.html” is not found (2: No such file or directory), client: 127.0.0.1, server: localhost, URL: “/rails/Demo/”, host: “localhost” (when trying http://localhost/rails/Demo - I copied app ‘Demo’ to /var/www/rails)
2) I am also having a routing error with mongrel, but I am looking at other forums.
Choosing ‘About your application’s environment’ from http://localhost:8000, the trace message is: {no route found to match “/rails/info/properties” with {:method=>:get}}
Thanks a ton for your effort.
Murali
Hi Murali,
The app should be invoking itself automatically if you followed the instructions precisely.
If you’re still having trouble, you’re probably best off hitting the rails deploy mailing list for specific problems with your conf file: http://www.ruby-forum.com/forum/23
vince
Hi, very precise and well written, as a newbie I appreciate that thanks.
As I have been using Apache2 over the last few weeks to run Moodle I would like to use that rather than nginx is it as easy as missing out step 6?
Cheers
Dave
Dave
lee
Dave: I haven’t tried apache2 with rails yet but I’m sure there are some excellent howto’s for it out there.
Lee: Thanks, fixed.
vince