Setup Ruby and Rails on OSX
Welcome to the first of many posts in the OpenRain Tips and Notes series. We’re going to use this space to share some of the tips, tricks, howto guides, and notes, that we use internally to keep us productive and make us happier programmers.
Today, we’re going to learn how to get your brand new shiny Mac OS X machine setup for Ruby and Ruby on Rails development. When we’re done, the full software stack will include Ruby 1.8.7, Rails 2.1.0, MySQL 5.0.51, and Mongrel 1.1.5.
And, who might you be? A programmer, comfortable at the command line, running on Mac (Leopard, 10.5.4), and interested in Ruby and Rails.
Preamble
Setting up a complete Ruby on Rails environment for local development can be overwhelming, especially if you’re new to it, but even if you’re done it before. Depending on your level of comfortability, there several ways you can setup Ruby and Rails on OS X from scratch, so please keep in mind that this guide is one of several “right” ways to do this.
The most “right” way to setup Ruby, Rails, MySQL, and Mongrel on your Mac
Xcode and MacPorts

Download and install a copy of the Xcode Developer Tools DVD image from Apple’s Apple Developer Connection. Membership to ADC is free and highly encouraged if you’re doing any sort of software development for the Mac. It’s a good source for all your Apple programming activities. [Download > Developer tools > Xcode 3.1 Developer DVD (Disk Image)].
Download and install a copy of MacPorts. Since Mac OS X is a descendent of Unix, you can download, compile, and install all sorts of existing Unix software on your Mac. However, doing this manually becomes tiring almost immediately since you have to manage versions, dependencies, and conflicts all by yourself. The MacPort system is a convenient tool for managing the life-cycle of source-based installations. Any software that MacPorts installs, is kept separate from the software that comes with your Mac.
Update your environment variables
Once MacPort has been properly installed, modify your $PATH and $MANPATH environment variables with the location that MacPorts installs all it’s software in.
For example, in ~/.bash_profile:
export PATH=~/bin:/opt/local/bin:/opt/local/sbin:$PATH
export MANPATH=/opt/local/share/man:$MANPATH
Ruby, ruby, ruby

The good news is that, on Leopard (OS.X 10.5.4), Ruby is an officially supported language. Out of the box, this includes:
- the warm fuzzy feeling of Ruby running on OS X
- the ability to debug Ruby processes with dtrace
- integration with Cocoa via the RubyCocoa bindings, and
- using Ruby to control and message OS X applications via the AppleEvent infrastructure
The bad news is that updates don’t flow as fast from Apple so, for instance, running the latest version of Ruby, (1.8.7 as of this writing) isn’t possible unless you roll your own Ruby and RubyGems environment.
To install Ruby and RubyGems from the command line:
$ sudo port install ruby
$ sudo port install rb-rubygems
RubyGems for Rails and everything else

Once you’ve got RubyGems installed, setting up the rest of your system is easy to do from the command line:
$ sudo gem install rails rake capistrano capistrano-ext libxml-ruby mongrel hpricot

This installs Rails, as well as a bunch of other useful gems into:
/opt/local/lib/ruby/gems/1.8/gems
Install MySQL (Option 1: via MacPort)
There are two options for installing MySQL. Option 1 continues to use MacPort while option 2, a little further down the page, uses the binary installer. Either option works well.

$ sudo port install mysql5 +server
$ sudo -u mysql mysql_install_db5
$ ln -s /opt/local/share/mysql5/mysql/mysql.server ~/bin/mysql.server
The first and second command installs and setups MySQL5 via MacPort. The third command sets up a convenient helper script which you can use to start and stop MySQL server from the command line:
$ sudo mysql.server {start|stop|restart|reload|force-reload|status}
Optionally, you can configure MySQL to start up automatically when OS X boots.
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
Install the native MySQL driver
Out of the box, Rails comes with a pure-Ruby MySQL adapter, though all production Rails environments running on MySQL will use the native binary drivers since they are considerable faster. To get the native MySQL drivers from the command line:
sudo gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config5
Install MySQL (Option 2: via the binary distribution from mysql.com)
One alternative to installing MySQL from MacPort is to install the binary distribution from mysql.com.
Download and install the DMG.
Optionally, you can install the System Preferences panel and a startup script which starts MySQL up along with OS X.
Install the native MySQL driver
Similarly, you’ll also need to configure the MySQL RubyGem driver
$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-include=/usr/local/mysql/include/ --with-mysql-lib=/usr/local/mysql/lib/ --with-mysql-config=/usr/local/mysql/bin/mysql_config
Fin

That wasn’t too bad. If you followed the directions correctly, your shiny new Mac should be properly setup with Ruby 1.8.7 and Rails 2.1.0 running on MySQL 5.0. For performance purposes we also installed Mongrel and the MySQL native drivers which are automatically used by Rails during development.
A super secret cheat sheet:
Install Xcode 3.1
Install MacPort 1.600
sudo port install ruby
sudo port install rb-rubygems
sudo gem install rake rails capistrano capistrano-ext libxml-ruby mongrel hpricot
sudo port install mysql5 +server
sudo -u mysql mysql_install_db5
sudo gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config5
Recent Comments