Thursday, 6 March 2008

Rails on Mac OS X Leopard

Note: The RichText blog has moved to

We'll get to the instructions for setting up rails on Leopard in a moment, but first a bit of background...

A few days after it was released, I changed over to using Mac OS X 10.5 (Leopard) as my platform for developing rails apps. When I first installed Leopard it took me a little while to figure out the best way to do things, as Leopard comes with lots of rails development features pre-installed.

Recently, after reinstalling my OS, I had to figure some of it out again as I hadn't made enough notes. So this time, I thought I'd document it properly and share it on my blog for the benefit of others (and so I don't forget too!).

As you might remember from this post last year, I'm a fan of using Netbeans for my rails IDE. Since that post, it has come out of beta and is now a fully fledged release (currrently at v6.0.1).

However, I couldn't get its step-through debugger to work with Leopard's installation of rails. I think is is because Netbeans assumes certain relative paths between the ruby interpreter and your gems, which differ in the Leopard standard install.

After being inspired by this post by Robby Russell, I decided to use Macports as my solution. Macports gives you a self contained environment for installing stuff. Everything installed under Macports lives in /opt/local, so if you mess up your Macports environment, you can easily just trash this folder and start again.

Installation Procedure

Here goes... The following instructions walk you through how to set up a development environment with Ruby, Rails, MySQL, Netbeans and Subversion on OS X 10.5 "Leopard".

Before doing anything else, we need to install the Xcode tools from the Leopard DVD. Just double click on the installer and away it goes.

Download the installer from the Macports site, and run it.
(I used the dmg for v1.5.0 for OS X 10.5, but I think v1.6.0 is out now so you might want to try that one).

Now, for MySQL, I just downloaded the package from the MySQL site, and installed it.

The problem I've found with this using this package is that the preference pane and automatic startup item don't work quite right, but there are workarounds for this. In particular, this worked for me:

"[...] the working MySQL.prefPane is here

and to get the StartupItem to work (see bug#25008), copy the (if existent)
/usr/local/MySQLCOM directory to /Library/StartupItems/MySQLCOM "

There are also some great free GUI tools available on the MySQL site too. At the time of writing, there isn't a dedicated package for these available for OS X 10.5 -only 10.4, but it works fine.

Set paths
This step is important! Dont skip it.

edit ~/.bash_profile, for example using TextMate:
mate ~/.bash_profile

and add the following line:
export PATH="/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

(As I mentioned before, anything installed in Macports is installed under opt/local)

Save the file, and restart your terminal session to pick up the changes.

Install ruby and rubygems in Macports

sudo port install ruby
sudo port install rb-rubygems

Install all your required gems

For example:

sudo gem install rake
sudo gem install rails
sudo gem install capistrano
sudo gem install termios
sudo gem install mongrel
sudo gem install mongrel_cluster
sudo gem install ruby-debug
sudo gem install ruby-debug-ide

(The ruby-debug and ruby-debug-ide gems above are required if you want step-through debugging to work in Netbeans).

I just use the subversion installation that comes with Leopard, as this works fine.

Download Netbeans from here, and run the installer. (For developing rails apps, you just need the 'ruby' bundle).

Debgging in Netbeans
In Netbeans, under Preferences->Ruby, set the ruby interpreter to /opt/local/bin/ruby
The fast debugger radio button should become available - select it.

That's pretty much it. You should now have a fully functioning setup for developing on rails.

Note: I use svn over ssh to access my subversion code repository. Below are some instructions for getting this to work with Netbeans. You can safely ignore all that follows if this doesn't apply to you - Netbeans works fine 'out of the box' with svn over http...

svn+ssh in Netbeans
In order to get the svn+ssh protocol to work with Netbeans, you need to set up a ssh key, and set your mac up to remember it. Here's how to do this

1. Create a pair of keys on your local machine.
From your home directory on your mac, run the following command:
ssh-keygen -t dsa

This will prompt you for a secret passphrase - make sure to use a good passphrase. When this is done you will get two files called id_dsa and in your ~/.ssh dir.

2. On your server, ensure that the .ssh directory exists for the user
Log in to your server via ssh

make a .ssh hidden directory
mkdir ~/.ssh

3. Place your public key on your server
On your mac, run
ssh 'cat >>.ssh/authorized_keys' <~/.ssh/

4. Try to log in to the server

You will be asked for the passphrase

In OS X Leopard, there is a user interface for ssh-agent which will ask you if you want to remember the passphrase, and will store it in your keychain. From then on, you wont need to enter a password or a passphrase for this user.

5. Edit the subversion config
This step is required because, although when you check out from an svn repository in Netbeans, the dialog asks you for a tunnel command, it forgets that when you restart the IDE.

On your mac, edit the ~/.subversion/config file (note: this file wont exist unless you've previously run Netbeans)
mate ~/.subversion/config

Add an entry under the [tunnels] section like this:
ssh = $SVN_SSH ssh -l my_ssh_username

Save the file.

Now, Netbeans will be able to access your svn repository over ssh without needing a password from you.

6. Checkout from your repository
In Netbeans, If you go to the Versioning->Checkout dialog, you will see that the tunnel command is pre-populated for you. Just enter your repository location eg:


and everything should work fine.

Digg Technorati Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy

Please also visit the Swirrl blog

1 comment:

Anonymous said...

Sounds good! have u heard Navicat before? it is a powerful gui and offer lite version too.