How to set up your Mac for Rails Development

In this post I will guide you through everything necessary to get your freshly set up Mac — using Leopard or Snow Leopard — ready for Ruby on Rails Development.

Things we’ll install:

Xcode Tools

Install XCode Tools from your MacOS Install Disc’s “Optional Installs” Folder:

Install Xcode from your Mac OS X Install disc's 'Optional Installs' folder

Or, if you don’t have your Mac OS Install disc handy, you can download the latest version of Xcode Tools from Apple Developer Connection (free Registration required).

MacPorts

Get the latest version from the MacPorts site. Download the disk image for your version of Mac OS X and install MacPorts.

After the installation is complete, open a Terminal and make sure that the port command is available by reloading /etc/profile:

. /etc/profile

Update your ports database using

sudo port selfupdate

Then, install all the necessary ports:

sudo port install git-core +svn ruby rb-rubygems sqlite3 ImageMagick

PostgreSQL

While MacPorts is building and installing packages, download PostgreSQL for Mac and start the ‘PostgreSQL for Mac’ installer in the ‘Server’ directory.

Install PostgreSQL Server

After installing PostgreSQL Server, add a database role for your user account:

sudo -u postgres /Library/PostgreSQL8/bin/createuser

Enter your Mac OS X system user name as role name, and make it a superuser.

Add postgres user role

TextMate

Download and install the latest version from the TextMate site.

TextMate Bundles

To make sure that the bundle installation runs smoothly, make sure that the Bundles directory exists on your machine:

mkdir -p ~/Library/Application\ Support/TextMate/Bundles

Install Bundles for Git, Ruby on Rails, RSpec, Cucmber, Webrat, Sass and Haml:

git clone git://github.com/timcharper/git-tmbundle.git ~/Library/Application\ Support/TextMate/Bundles/Git.tmbundle
git clone git://github.com/drnic/ruby-on-rails-tmbundle.git ~/Library/Application\ Support/TextMate/Bundles/Ruby\ on\ Rails.tmbundle
git clone git://github.com/dchelimsky/rspec-tmbundle.git ~/Library/Application\ Support/TextMate/Bundles/RSpec.tmbundle
git clone git://github.com/bmabey/cucumber-tmbundle.git ~/Library/Application\ Support/TextMate/Bundles/Cucumber.tmbundle
git clone git://github.com/bmabey/webrat-tmbundle.git ~/Library/Application\ Support/TextMate/Bundles/Webrat.tmbundle
git clone git://github.com/aussiegeek/ruby-sass-tmbundle.git ~/Library/Application\ Support/TextMate/Bundles/Ruby\ Sass.tmbundle
git clone git://github.com/canadadrugs/ruby-haml-tmbundle.git ~/Library/Application\ Support/TextMate/Bundles/Ruby\ Haml.tmbundle

You can also install my TextMate bundle containing commands to restart Passenger, reformat Ruby code and update all your bundles via Git:

git clone git://github.com/inz/inz-tmbundle.git ~/Library/Application\ Support/TextMate/Bundles/inz.tmbundle

Finally, tell TextMate to reload all bundles:

osascript -e 'tell app "TextMate" to reload bundles'

Rubygems

Make sure you have the latest version of Rubygems:

sudo gem update --system

SQLite3

SQLite3 needs some special attention to install properly.

To install SQLite3 on Mac OS X 10.5 Leopard:

sudo env ARCHFLAGS="-arch i386" gem install sqlite3-ruby -- --with-sqlite3-dir=/opt/local

If you are using Mac OS X 10.6 Snow Leopard, install like so:

sudo gem install sqlite3-ruby -- --with-sqlite3-dir=/opt/local

PostgreSQL

The PostgreSQL gem will also not install without special treatment, install it using this command:

sudo env PATH=/Library/PostgreSQL8/bin:$PATH gem install pg

Rails and all the Rest

The other gems necessary for your development setup will happily install without any further issues:

sudo gem install rails haml rspec rspec-rails cucumber capistrano capistrano-ext

Phusion Passenger and Passenger PrefPane

To keep your development environment one step closer to your deployment setup, you can use Phusion Passenger for development. And with the Passenger PrefPane you even get a pretty interface for that!

Install passenger:

sudo gem install passenger

Then, install the Apache module using

sudo passenger-install-apache2-module

After the installation of the module, follow the directions and load the passenger module in your /etc/apache2/httpd.conf.

To make future updates a little easier you can create a config file in /etc/apache2/other, and it will be loaded automatically.

mate /etc/apache2/other/passenger.conf

and copy the directives from the Passenger installation into the file.

Or, if you like the command line, you can also add the file like so:

sudo sh -c "cat >/etc/apache2/other/passenger.conf <<EOT
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
PassengerRoot /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.5
PassengerRuby /opt/local/bin/ruby
EOT"

The command above can then be reused whenever you update the passenger gem, replacing the updated version number.

Passenger PrefPane

To simplify development using passenger you can download and install the Passenger PrefPane. It will automatically add the necessary directives to the Apache configuration, so you should be able to use Passenger right away.

SSH Keys

If you’re developing and/or deploying web applications, chances are you need a pair of SSH keys for public key-based login to your servers.

Generate a pair of keys using:

ssh-keygen

Install the keys into the default location and follow the instructions.

To use the key to log in to your servers, copy it to the clipboard using:

cat ~/.ssh/id_rsa.pub | pbcopy

Then, log in to your servers and append the key to ~/.ssh/authorized_keys for every account you want to log in with.

If you have a set of SSH keys backed up from previous installations, you can of course use these as well. Just be sure to set the correct permissions on the copied files (be sure to use your Mac OS username in the command below):

chown -R inz:staff ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

If you’ve followed the instructions you should now have a Mac fully set up for all your Rails development needs.

Let me know in the comments what worked for you what did not, so I can keep this guide up to date.

Thanks, have fun
C.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

6 Responses to “How to set up your Mac for Rails Development”


  • Danke danke danke :-) … Der Post war nach meinen UPGRADE auf Snow Leopard wirklich überlebenswichtig :-)

    Jetzt funktioniert Postgres und die ganze Rails Umgebung sogar wieder *hehe*

  • Thanks, your tip to use postgresqlformac.com bundle solved my problem with RDBMS.

    I had EnterpriseDB Postgres 8.3 package since Leopard, and it wouldn't work with ruby-pg on Snow Leopard. Neither EnterpriseDB PG 8.4 nor MacPorts PG 8.3 worked for me. The most I could get trying to compile ruby-pg were "expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’" errors.

  • Thanks so much for this guide!

    After several hours and trying many different tutorials this is the first one that worked for me. I always came unstuck at creating a potgresql user or installing the pg rubygem. Great stuff!

  • Since restarting I'm getting a

    “/Library/StartupItems/PostgreSQL” has not been started because it does not have the proper security settings.

    error. But PostgreSQL starts ok. Any ideas on how to fix this?

Leave a Reply