Installing Rails on Mac OS X Tiger

2/23/07 update: some things have changed since I wrote this page, either in MacOS itself, or the components referenced. So before you start, be sure to also check a recent update for 10.4.8 and the comments of both pages that contain some valuable information.
If something still does not work, please leave a comment and someone will help you.

I recently Switched back to a Mac, while other long time Mac users are switching to Linux. And I love every minute of it! Once I got the basic stuff going, it was time to install a fitting Ruby on Rails environment on my new iMac.

There are a number of approaches you can use (see the reference section at the bottom). I was after installing an environment as close as one you can use in production, but right here on my desk.

If you are only looking for the basics, Locomotive is probably what you need, if you want a grown-up rails setup on Mac OS Tiger, read on.

Choosing an install method

There are 3 approaches to installing Ruby on Rails on Mac OS Tiger.

You can use the one click approach, and Locomotive is probably the simplest. My main issue is that it is a bit too opaque and if you want to patch anything, upgrade one one the component, you might be out of luck.

You can use the “compile everything from source approach”, and Hivelogic covers this well. This is an approach you might prefer except that you may run into Mac specific issues not covered by typical Unix/Linux code

A third option is to rely on Darwin ports as covered by Evan with “a better way of building ruby, rails, lighttpd, mysql, and postgres on OS X tiger

This third method gets my vote: less opaque, but a large number of smart and dedicated people have already worked out all the problems.

So with that covered, let’s get on with that “grow-up setup” (thanks Coda!). Hmm OK, just one last word of warning before you begin. I’ve installed more than the bare minimum. If you follow these directions, you’ll be all set for standalone lighttpd , standalone mongrel, apache, rails with mongrel and a few more combinations. I haven’t got to using pound just yet, so we’ll keep this for some other time.

Installation on Tiger

If you have not installed Darwin port, now is the time.

Then update port itself

sudo port -d selfupdate

Fix the root path and set the PATH variable in /etc/profile. You may not need this is you are not going to run things as root. Although this will simplify a few things for you.

mate /etc/profile    #fill free to use vi or any other editor you’d like
PATH=”/opt/local/bin:/opt/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin”

Install Apache 2

sudo port install apache2

Create a default configuration for apache

cd /opt/local/apache2/conf
cp httpd.conf.sample httpd.conf

Start Apache

sudo launchctl load -w /Library/LaunchDaemons/org.darwinports.apache2.plist

Rebooting would do the trick too. At this point you should have a “It works!” if you point a browser to http://localhost

Install fcgi (not really going to use it, so it is a just in case so I can benchmark it at some point)

sudo port install fcgi

Install lighttpd

sudo port install lighttpd +ssl

Install mysql 5

sudo port install mysql5 +server

start mysql 5 (you can also reboot)

sudo launchctl load -w /Library/LaunchDaemons/org.darwinports.mysql5.plist

Change the mysql root password (on localhost AND on your network card)

/opt/local/lib/mysql5/bin/mysqladmin -u root password ‘new-password’
/opt/local/lib/mysql5/bin/mysqladmin -u root -h [HOSTNAME] password ‘new-password’

Check it works (empty password if you have not set it)

mysql5 -u root -p

Install subversion

sudo port install subversion +mod_dav_svn +tools

Install ruby and a few goodies

#ruby
sudo port install ruby
sudo port install rb-rubygems
sudo port install rb-termios
sudo port install rb-fcgi
sudo port install rb-mysql5
sudo port install imagemagick

Install Apache mod_fcgi module

sudo port install mod_fastcgi

add the following line to /opt/local/apache2/conf/httpd.conf

LoadModule fastcgi_module modules/mod_fastcgi.so”

Install gems you can’t live without

sudo gem install -y rake
sudo gem install -y rails
sudo gem install -y capistrano
sudo gem install daemons gem_plugin mongrel mongrel_cluster –include-dependencies
sudo gem install rmagick

One good gem to have would be sendfile to avoid copying data between apache and mongrel for example, but the Tiger kernel does not support it despite having the function defined in the C header files. So since it is not really a production machine, we can live without it.

At this point, you’ve got more that the basic setup for Ruby on Rails. That was no too bad, wasn’t it? Although arguably, this could be easier!

Deploying a rails application: Mephisto

For good measure, just to check that our setup is all good, let’s install Mephisto. The latest Rails blog engine.

Create the databases

mysqladmin5 -u root create mephisto_development
mysqladmin5 -u root create mephisto_test
mysqladmin5 -u root create mephisto_production

Checkout everything

svn co http://svn.techno-weenie.net/projects/mephisto/trunk mephisto

Configure the database (should be the right default)

cd mephisto/config
cp database.example.yml database.yml
mate database.example #to check that the database name are what we created

Populate the database

rake RAILS_ENV=production db:bootstrap

And start. Since lighttpd is installed, that’s what it is using

script/server

Point your browser to http://localhost:3000 and yes! It works :D

Or you can go to http://localhost:3000/admin (using admin/test for user/password)

Now let’s finish our “grown up setup” and configure mongrel cluster

mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N3 -c [RAILS_ROOT of mephisto]

a few note on Capistrano

My initial Capistano Cheat Sheet should be enough with one exception. SSH is not enabled by default on Tiger. To enable, use the “Sharing” panel under System Preferences and enable “Remote Login”.

And second, if you have trouble when running via ssh, you may need to fix ssh PATH. To do that, create a file under ~/.ssh/environment with:

PATH=/opt/local/bin:/opt/local/sbin:/opt/local/apache2/bin:/bin:/sbin:/usr/bin:/usr/sbin

Run Mongrel Cluster as a service

Now let’s configure Mongrel Cluster to start at boot time

sudo mkdir /etc/mongrel_cluster
ln -s [YOUR RAILS_ROOT]/config/mongrel_cluster.yml /etc/mongrel_cluster/[your application].yml

Create a file ~/Library/LaunchAgents/mongrel_cluster.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Debug</key>
        <true/>
        <key>Label</key>
        <string>org.rails.mongrel.cluster</string>
        <key>OnDemand</key>
        <false/>
        <key>Program</key>
        <string>/opt/local/bin/daemondo</string>
        <key>ProgramArguments</key>
        <array>
                <string>&#8211;label=mongrel_cluster</string>
                <string>&#8211;start-cmd</string>
                <string>/opt/local/bin/mongrel_cluster_ctl</string>
                <string>start</string>
                <string>-v</string>
                <string>-c</string>
                <string>/etc/mongrel_cluster/</string>
                <string>;</string>
                <string>&#8211;stop-cmd</string>
                <string>/opt/local/bin/mongrel_cluster_ctl</string>
                <string>stop</string>
                <string>-v</string>
                <string>-c</string>
                <string>/etc/mongrel_cluster/</string>
                <string>;</string>
                <string>&#8211;restart-cmd</string>
                <string>/opt/local/bin/mongrel_cluster_ctl</string>
                <string>restart</string>
                <string>-v</string>
                <string>-c</string>
                <string>/etc/mongrel_cluster/</string>
                <string>;</string>
                <string>&#8211;pid=none</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>/tmp/mongrel.log</string>
        <key>StandardOutPath</key>
        <string>/tmp/mongrel.log</string>
</dict>
</plist>

Additionally, if you did not want to modify /etc/profile, you can add:

    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key><string>/opt/local/bin:/opt/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin</string>
    </dict>

And finally, you can start mongrel cluster. or logging in will work:

sudo launchctl load -w ~/Library/LaunchAgents/mongrel_cluster.plist

To have it work at boot time, you will need to put mongrel_cluster.plist under /Library/LaunchDaemons instead.

References

lighttpd

http://duncandavidson.com/essay/2005/12/railsonlighty

http://duncandavidson.com/essay/2006/01/railsvhostingonlighty

Rails installation

rails with postgres on tiger using Darwin Ports

Lighttpd with rails on tiger

Time For A Grown-Up Server: Rails, Mongrel, Apache, Capistrano and You

Rails installation from single script

Locomotive, one click rails installer for Mac OS X

pound

pound + lighttpd + mongrel

10/17/06 update: I’ve reinstalled on a brand new system with Mac OS 10.4.8. See my notes on what has changed.