Setting up a development environment with Ruby on Rails and the
PostgreSQL database on Leopard is straightforward. In an interesting
twist, the hard part of setting up Rails and PostgreSQL on OS X
10.5 "Leopard" is easy with Linux, and the easy part is hard with
Linux.
Rails, the Easy Part
If you haven't heard, Ruby on
Rails is a web development framework created by David Heinemeier
Hansson that uses the Model-View-Controller (MVC) design pattern for
separating business logic from the presentation details. The MVC
approach is very popular in enterprise class systems.
Apple recognized the momentum behind Rails and includes a basic
configuration by default. The Ruby language (1.8.6) is installed along
with the Rails (1.2.3) framework. In addition to the Ruby language and
Rails, OS X Leopard includes a number of useful supporting
packages (called Ruby Gems). This saves you the setup time for the
basic infrastructure that is required on most Linux distributions.
PostgreSQL, the Hard Part
In order to get the PostgreSQL database up and running on Leopard,
use MacPorts to install it. To use
MacPorts, you first need to install the X-code tools from the
Leopard DVD. It's an optional install, but you need the compiler and
supporting libraries to compile and install PostgreSQL. (Most Linux
distributions have precompiled PostgreSQL packages ready to install
with one command, making this part of the set up easier on Linux.)
Once MacPorts is installed, install PostgreSQL with:
sudo port install postgresql83
It takes quite a while for MacPorts to download and compile
PostgreSQL. It took about 90 minutes for me. After it's installed,
there are a couple of post install commands that need to be run so that
it starts automatically at boot time. The commands are described in
detail once MacPorts finishes the basic installation.
Install the Ruby PostgreSQL Driver
In a production environment, you need the latest native PostgreSQL
binary driver. I tried a couple of times to install the binary driver
on my iBook, but I kept getting compile errors. I got around the
problem by installing the "pure ruby" driver (indicated by the
-pr suffix):
sudo gem install postgres-pr
I haven't noticed a performance issue during development, but I
would not feel comfortable using it for a production application.
Telling Rails to Use PostgreSQL
The final step after starting a new Rails application is to modify
the "config/database.yml" file to tell Rails to use PostgreSQL. Here is
an example:
development:
adapter: postgresql
database: keithw
username: keithw
Having Ruby and the basic Rails environment available out of the box
is a huge plus for OS X Leopard.
Keith Winston is a recent Mac convert after five years of Linux on the desktop. He also writes for Linux.com and created CommandLineMac to focus on the Unix-y power of the Mac. If you find Keith's articles helpful, please consider making a donation to his tip jar.