Nov 4, 2009

Fast install: Ruby on Rails on Ubuntu

This is the way I did it on my Jaunty server and my Karmic desktops/laptops (if I had that many).
It's entirely based on this guide.

First of all, I update my system the same way:
sudo apt-get update
sudo apt-get dist-upgrade


Then I install the application building essentials, like compilers and such, by merging (gentoo...) the build-essential meta-package:
sudo apt-get install build-essential


Here's where I deviate a little. In my last particular case, I didn't want to support mysql, because I consider it too much for my small and frail triple-E. You can choose to run the complete command like on the original guide or run then same one as I did if you don't care about mysql while you develop your Ruby on Rails application. You can use sqlite3 instead.
sudo apt-get install ruby ri rdoc ruby1.8-dev irb1.8 libdbi-perl libnet-daemon-perl libplrpc-perl libreadline-ruby1.8 libruby1.8 rdoc1.8 ri1.8 ruby1.8 irb libopenssl-ruby libopenssl-ruby1.8 libhtml-template-perl


Then I get rubygems from rubyforge.org. Extract it, go in and run the install script.
sudo ruby setup.rb


You absolutely have to create some symlinks because the install script doesn't do it for you like the packaged version from apt.
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


Time to use gem to install the latest rails framework:
sudo gem install rails


Don't use the nginx package from apt. Remove that if you have it installed. Phusion Passenger will beautifully install it in /opt and configure it for you. First, some dependencies we need:
sudo apt-get install libc6 libpcre3 libpcre3-dev libpcrecpp0 libssl0.9.8 libssl-dev zlib1g zlib1g-dev lsb-base


Then comes setting up some places...
sudo mkdir -p /var/www/myapp
sudo ln -s /var/www/myapp/current /var/www/myapp/myapp-0304092333

I hope you understood what that does, if not, then it's a simple way of changing you running application. You have your app folder "myapp", subfolders as actuall app folders (that you create by running "rails myapp-datehere") but with dates and a symlink to any of them you want, called "current", which will be used in the nginx config to tell it the root of the rails app, ie. "current/public". All you have to do to change your running version of the app, is to update a symlink now.

Careful about this one, you might encounter problems with your app if the user the interpreter is running under, can't read files. Well, you could change the user it runs under but that's different.
sudo chown -R www-data:www-data /var/www/myapp/current/


Finally, installing the great Passenger and nginx:
sudo gem install passenger
sudo passenger-install-nginx-module


You're going to let Passenger install nginx for you, so you'll choose 1 when it asks you. Leave the rest at the default values (just press Enter when asked).
All that's left, really, is to configure nginx by editing /opt/nginx/conf/nginx.conf, as root of course. Most importantly, change the user directive to www-data and you might want to change the default rails environment, in case you don't develop using rails' built in web server. You can still follow the remaining of the original guide and I suggest you do. There's an example nginx.conf provided too. Beware not to change the passenger_root directive since yours is probably newer than the one used in that example.

OK. I hope it worked for you. If not, don't blame me.
But if you need help, I'm here. There's still a lot more damage I can do ;).

POSTEDIT: I forgot that you need a package to use sqlite3, like I said that this guide lets you. So be sure to do this before you rake db:create or rake db:migrate:
sudo aptitude install libsqlite3-dev
sudo gem install sqlite3-ruby

And you can have my /opt/nginx/conf/nginx.conf file too. It's not too long.

user  www-data www-data;
# or you could use your username and usergroup and freely edit files
# as yourself inside /var/www.

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5;
    passenger_ruby /usr/bin/ruby1.8;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nodelay on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
    # change this to your own path:
    root /var/www/acclabs/current/public;
    passenger_enabled on;
    # if you want to see debug output on errors:
        rails_env development;
    index index.html index.htm;
    client_max_body_size 50M;
    if (-f $document_root/system/maintenance.html) {
          rewrite ^(.*)$ /system/maintenance.html break;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}