Updated 5th December 2012 with latest package versions.

After recently updating to OS X 10.8 Mountain Lion, I decided to document the process of getting set up for Ruby on Rails development with MongoDB. Fortunately the process hasn't changed too much from previous versions of Mac OS X.

1. Install Command Line Tools 10.8 for Xcode

In order to compile Ruby with RVM, as well as many Homebrew packages, you'll need a compilation toolchain. If you are doing Mac or iOS development, you may want to install Xcode in its entirety. However if you don't want the hefty Xcode (1.8GB) on your system, the Command Line Tools are a good lightweight (110MB) alternative.

The Command Line Tools installer can be downloaded here (you'll need an Apple ID). After downloading, mount the disk image and run the installer.

2. Install Ruby and Rails using RVM

Note: the following commands should be run as a non-root user (i.e. no sudo).

To install the latest stable version of RVM, run the following from a terminal window:

$ curl -L https://get.rvm.io | bash -s stable

Close and reopen the terminal window. Then install the latest Ruby version with:

$ rvm install 1.9.3

You can confirm this has installed correctly by running:

$ ruby -v
ruby 1.9.3p327 (2012-11-10) [x86_64-darwin12.2.0]

Before installing any gems, I generally disable the slow and unnecessary RDoc and RI generation by creating a ~/.gemrc file with the following:

install: --no-ri --no-rdoc
update: --no-ri --no-rdoc

Then we can install and verify Rails with the following commands:

$ gem install rails
$ rails -v
Rails 3.2.9

3. Install Homebrew & XQuartz (X11)

Homebrew is my preferred way of managing packages in OS X. Homebrew compiles most packages from source and keeps everything nicely managed within /usr/local. It can be installed by running:

$ ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"

X11, which is required for packages such as ImageMagick, was previously bundled with Mac OS X. It is still maintained, but no longer ships with the OS, instead available as XQuartz.

Download XQuartz 2.7.4, mount the disk image and run the installer, as with the Command Line Tools.

4. Install MongoDB

Finally, we can install MongoDB using Homebrew:

$ brew install mongodb
$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/mongodb/2.2.2-x86_64/homebrew.mxcl.mongodb.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

This will load MongoDB automatically on login, and start it for the first time. We can verify MongoDB is installed correctly by loading the console:

$ mongo
MongoDB shell version: 2.2.2
connecting to: test
>