I have a tutoring listing on Tutorspree. A potential student asked me about Rails tutoring but he was still having trouble getting a nice Rails environment running on Mac OSX. I don’t want to charge for time getting Rails set up. Setup should be easy and fairly painless. I’m going to describe how I setup a new OSX install for Rails development.

The goal setup is:

  • Mac OSX 10.6.x
  • Ruby 1.9.2-p180 (using RVM)
  • Rails 3.0.x (with a new Rails project on its own RVM gemset)

Install Git

Git is a distributed version control system. I use it for all of my projects. I even use it at work via git-svn.

We want to install git so that installation of other programs we want will be easier. My favorite way to install Git is through Homebrew.

Run the following to install Homebrew:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

Then install Xcode. See more at the Homebrew git repo.

Install git with the following:

$ brew install git

Install RVM

We want to install the Ruby Version Manager a.k.a RVM. RVM allows us to manage various versions of Ruby in a straight-forward way. It also allows us to create a sandbox of gems for each Rails project (more on gemsets later).

Installing RVM is easy since we have git installed. Run:

$ bash <<(curl -s https://rvm.beginrescueend.com/install/rvm)

Make sure to follow the instructions from the RVM installation script (including adding the source‘ing line to our .bashrc)

Install Ruby

After having RVM installed, we can install our desired version of Ruby. Mac OSX comes with ruby 1.8.7 by default. I prefer to start fresh with Ruby 1.9.2.

$ rvm install ruby-1.9.2-p180

Note that we run rvm as our current user. We don’t have to run it as root.

After RVM finishes compiling and installing Ruby. I like to make 1.9.2 my default ruby version.

$ rvm --default 1.9.2-p180
$ rvm reload

The reload just reloads rvm with our new default ruby. We can also just restart our current shell.

Create a New Rails Project

When creating a new Rails project, I like to have all of its gems contained in one set. This allows me to ensure that have I complete control over gems that the project will have at runtime.

To do this, we can create a RVM gemset for our soon-to-be-new Rails project. Let’s call the project, woot. We’re going to create a gemset for it, install the Rails gem (and its dependencies) into the gemset and create the Rails project.

$ rvm gemset create woot
$ rvm gemset use woot
$ gem install rails
$ rails new woot

Note that the gem and its dependencies are only installed in the woot gemset. To see the difference, take a look at the output of:

$ rvm gemset use woot
$ gem list --local

*** LOCAL GEMS ***

abstract (1.0.0)
actionmailer (3.0.9)
actionpack (3.0.9)
activemodel (3.0.9)
activerecord (3.0.9)
activeresource (3.0.9)
activesupport (3.0.9)
arel (2.0.10)
builder (2.1.2)
bundler (1.0.15)
erubis (2.6.6)
i18n (0.5.0)
mail (2.2.19)
mime-types (1.16)
polyglot (0.3.1)
rack (1.2.3)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (3.0.9)
railties (3.0.9)
rake (0.8.7)
rdoc (3.8)
thor (0.14.6)
treetop (1.4.9)
tzinfo (0.3.29)

$ rvm gemset clear

$ gem list --local

*** LOCAL GEMS ***

bundler (1.0.15)
rake (0.8.7)

Okay, we’re almost ready to go. There’s one slight problem. Every time we launch our Rails app, we have to remember to switch the gemset for it.

~/woot$ rvm gemset use woot
# and when we're not in the project
$ rvm gemset clear

This is annoying. Use a .rvmrc file in the woot project so that RVM automatically switch to the correct gemset when we cd into the directory.

$ echo "rvm [email protected]" > woot/.rvmrc

When entering the directory, if asked by RVM to accept the .rvmrc, type y.

Now, we’re ready to go.

~/woot$ bundle install
~/woot$ rails s