How to install Ruby on Rails on Ubuntu 5.10 In RubyOnRails, 902 days ago 

I’ve just finished setting up Rails on a freshly installed Ubuntu. This setup uses Mysql, Apache2 and fcgid as the fastcgi ‘driver’. I’d like to share all the steps and packages needed:

1. Install ruby1.8 and the such

sudo apt-get install ruby1.8 ruby1.8-dev ruby irb rdoc libyaml-ruby libzlib-ruby

2. Install the gem package manager

wget rubyforge.org/frs/download.php/5207/rubygems-0.8.11.tgz
tar xzvf rubygems-0.8.11.tgz
cd rubygems-0.8.11
sudo ruby setup.rb
sudo gem update --system

3. Install rails and all its dependencies

sudo gem install rails -y

4. Install the Mysql development libraries

sudo apt-get install libmysqlclient12-dev

If you’re working with MySQL 5, you need to install libmysqlclient14-dev instead.

5. Install the Ruby Mysql C bindings

wget http://tmtm.org/downloads/mysql/ruby/mysql-ruby-2.7.tar.gz
tar xvzf mysql-ruby-2.7.tar.gz
cd mysql-ruby-2.7
ruby extconf.rb
make
sudo make install

(Note: this step could perhaps be simply substituted with sudo gem install mysql)

UPDATE: This step could be “apt-get’ed” with: apt-get install libmysql-ruby but I prefer the former because it ensures to have the most recent bindings installed.

6. Install mod_fcgid

sudo apt-get install libapache2-mod-fcgid

7. Activate fcgid and rewrite modules

cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/fcgid.conf
sudo ln -s ../mods-available/fcgid.load
sudo ln -s ../mods-available/rewrite.load

Update: a2enmod fcgid is the “ubuntu way” to activate the fcgid module (and rewrite as well, of course).

8. Configure fcgid

You can tweak fcgid editing /etc/apache2/mods-available/fcgid.conf

Add this line to specify (globally) a rails environment:

DefaultInitEnv RAILS_ENV production

Other parameters of interest could be

  IdleTimeout 60
  ProcessLifeTime 6000
  MaxProcessCount 32
  DefaultMaxClassProcessCount 2
  IPCConnectTimeout 6
  IPCCommTimeout 6

Refer to the fcgid documentation for the meaning of each parameter.

9. Install ruby support for fcgi

sudo apt-get install libfcgi-ruby1.8

10. Create one rails application (optional)

cd ~/public_html
rails testapp

11. Create a named virtual host (optional)

Create a named virtual host a point its document root to the public directory of the rails application. Hint: you can use something like testapp.localdomain, for test purposes; just add testapp.localdomain to your /etc/hosts. Delete index.html inside /public as well.

12. Modify .htaccess

Now we need to modify a line inside .htaccess. AddHandler fastcgi-script .fcgi becomes AddHandler fcgid-script .fcgi Also, be sure to have this line: RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

13. Is database.yaml ok?

Verify that into /config/database.yaml no sockets are used

14. Configure fcgid for development purposes

It seems that fcgid is much more aggressive in its default setting for “caching” connections than fastcgi. This can be a problem while developing (the same page keeps being served even if it is actually modified). You can tune this behaviour using the IdelTimeout and IdleScanInterval directives. They have to be put in your httpd.conf, not in any VirtualHost. fcgid documentation is very poor (stating a binary compatibility with the much better documented mod_fastcgi, which is, imho, not perfect). I personally use “60” as the value for IdleTimeout and “10” as the value for IdleScanTimeout. This values were quite randomly choosen, based upon the “300” and “100” default ones.

<IfModule mod_fcgid.c>
    IdleTimeout 60
    IdleScanInterval 10
</IfModule>

Update: note that fastcgi are not intended to be used in a development environment. Use webrick, instead.

Update: As davidw noted (look at the first comment), if you get weird errors with fcgid not working, try adding

  IPCCommTimeout  20
  IPCConnectTimeout 20

to the fcgid configuration parameters.

15. Your log dir must be writeable by the web server

To have fastcgi write its error log (fastcgi.crash) be sure to have the /log directory owned by www-data.

Done.

Comments

  1. francesco said:

    step 7 can be substituted by `a2enmod fcgid`

    really useful post, by the way :)

    Posted on 11/27/05 01:39 AM    #

  2. riffraff said:

    instead of step 5 I let apt-get taking care of it:
    apt-get install libmysql-ruby

    Posted on 11/30/05 09:34 AM    #

  3. Claudio said:

    Nice, the post has been updated

    Posted on 11/30/05 09:59 AM    #

  4. brett said:

    For step 5 to work for me, I had to sudo apt-get install ruby1.8-dev

    Everything else worked like a charm. Thanks for the excellent walkthru

    Posted on 11/30/05 05:31 PM    #

  5. Claudio said:

    Hello brett, for step 5 which way? :) The apt one, the gem one or the scratch?

    Posted on 12/01/05 09:08 AM    #

  6. jon said:

    Apologies for my noviceness but I’m a total newcomer to Linux and Ruby and I haven’t even understood how to install programs yet. The first task I have set myself is to install Ruby because I want to learn it.

    This is the best site I have found, but I don’t understand any of the commands or how the syntax operates or what the switches do.

    In short, I would have loved an explanation, in particular a detailed one, of each command before trying it out.

    If anybody can point me in the right direction I am on jon4fun4836@hotmail.com

    Posted on 12/09/05 04:26 PM    #

  7. Laran Evans said:

    Great post! You included what was for me the one detail that a number of other installation guides left out.

    The point about changing to `AddHandler fcgid-script .fcgi` I found pretty tough to debug. Most of the installation guides still leave that out, defaulting to the older `AddHandler fastcgi-script .fcgi`. For me that meant that my Ruby scripts simply showed up as text.

    Thanks again!

    Posted on 12/14/05 05:35 AM    #

  8. Phil said:

    In step 4, if you’re using MySQL 5 (or any MySQL with the new-style authentication, I think), you should run

    sudo apt-get install libmysqlclient14-dev

    instead

    Posted on 12/24/05 03:03 AM    #

  9. David Hersey said:

    It seems there’s a couple details missing:

    1) in .htaccess it is also necessary to replace

    RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

    with

    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

    Maybe this is obvious, but being a Rails n00b I missed it the first time.

    2) I also found that you need to ensure your /log directory is writable by www-data, which is the uid/gid used by apache to write the fastcgi.crash log.

    Posted on 12/28/05 06:06 PM    #

  10. Claudio said:

    Thanks, the how to has been updated accordingly :)

    Posted on 12/28/05 07:28 PM    #

  11. Lars Hansen said:

    Nice guide… Thanks alot! :o)

    Posted on 01/08/06 10:14 AM    #

  12. Bryan said:

    regarding step 10, I also had to change the file
    /etc/apache2/mods-available/userdir.conf - i ended up adding the .htaccess ExecCGI and AllowOverride All commands in here. I’m not an apache admin, so this might be half-assing it, but otherwise I was getting an Interal Server 500 error, and was not authorized to execute the CGI.

    Here is my .htaccess:
    —————————————————
    AddHandler fcgid-script .fcgi
    AddHandler cgi-script .cgi

    RewriteEngine On

    RewriteRule ^$ index.html [QSA]
    RewriteRule ^([^.]+)$ $1.html [QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

    ErrorDocument 500 “Application errorRails application failed to start properly…!”
    —————————————————-

    And here is my /etc/apache2/mods-available/userdir.conf
    (of course, replace the brackets as necessary)
    ——————————————————
    [IfModule mod_userdir.c]
    UserDir public_html
    UserDir disabled root
    [Directory /home/*/public_html]
    Options FollowSymLinks ExecCGI
    AllowOverride All
    [/Directory]
    [/IfModule]
    ——————————————————

    I hope this helps someone. Keep in mind I have no idea about security - but it will get you up and running.. :)

    Posted on 01/11/06 04:51 PM    #

commenting closed for this article

This site

Browse by category

Contacts

Feed from feedburner Subscribe to this feed with Bloglines

Creative Commons

XFN Friendly