Installing Ruby on Rails for Mac

This page provides the simplest instructions for installing Ruby on Rails 3.0.x on a Mac computer. It has been most recently tested on a Mac Mini running Lion (10.7). The instructions will probably work provided that the Mac already has Ruby 1.8.7 installed. If it doesn't, additional suggestions are provided below.

Install XCode

Xcode is a free download. You may have an XCode DVD that came with the computer, but it will be better to download XCode to ensure a recent version (recommended versions are 4.1 or later for Lion, 3.2 or later for Snow Leopard and 3.1 or later for Leopard). If you download XCode from the Apple Developer site, you will probably need to register (for free).

XCode takes a while to download, even on a reasonably fast network. Be prepared to do some waiting.

XCode has an install package that you can double-click to get started. When I ran XCode for Lion, the installation appeared to stall at the very end. Apparently this is a common problem. However, XCode seems to be installed anyway. Restarting the computer or force-quitting the installer gets you on your way.

Terminal Window and Testing for Ruby

You will need to use the Terminal Window for entering typed commands. The Terminal application is located in the Utilities subfolder inside the Applications folder. You can double-click the Terminal application to get it started, but you will probably want to drag it to the control panel at the bottom of your display for easy future access.

With the Terminal open, test for Ruby by typing this command, followed by the Return key:

	ruby -v
      

If 1.8.7 appears in the response, these instructions should work for you.

Setting up the Gem installer

Our text (Agile Web Dev with Rails) suggests the following steps for getting the gem installer set up (p. 5):

      sudo gem update --system
      sudo gem uninstall rubygems-update
    

I'm not quite sure why the second step is needed, but I did it for my installation and it worked fine.

Note that sudo is required before these commands. These commands install software in folders with restricted access. The sudo command tells the Mac OS that you have those privileges and you must authenticate with your Mac password.

Installing Rails

We will be using Rails version 3.0.9 in this class and that is what you should install. Here are the commands:

      sudo gem install rails --version 3.0.9
      sudo gem install sqlite3
    

Testing

To really test if your installation is working, you should test it with a database. The easiest and most gratifying way is to create a scaffolded Rails application. Here's how to do that.

First, go to a directory where you will want to create your application directory. For example, if you want your application directory (folder) to be in the documents directory (folder), enter the following command in the Terminal:

	cd Documents
      

The command cd stands for change directory. You are now in the Documents directory. The command ls will list all of the files in the current directory. You can verify the current directory with the pwd command.

Now create your rails application, giving it a name (e.g. travel):

      rails new travel
    

I suggest giving your rails app name a thematic name like travel, music, dining, sports, etc, which should match the goals of your application.

Then change directory to your new applicaton:

      cd travel
    

Create a scaffold:

      rails generate scaffold Airport city:string code:string
    

Create the database table:

      rake db:migrate
    

Start the server:

      rails server
    

You can now verify if you application is working in a browser at http://localhost:3000/airports where the last word should be the plural version of your scaffold (model) name.

Problems?

It is possible that your Mac doesn't have the most recent version of ruby. In that case, the installation process is a bit more complicated. You will need to install ruby yourself. The simplest approach is to use Mac Ports.

With MacPorts installed, you can then upgrade sqlite3 and install Rails and Gems:

      sudo port upgrade sqlite3
      sudo port install ruby
      sudo port install rb-rubygems
    

I haven't recently tested these commands, but they are the same as suggested in the Agile Web Dev book (p. 5). After these commands run, you can then proceed to install Rails as documented above.