Tutorial

Install Ruby on Rails on Mac OSX 10.7 - 10.9


In this article you will learn how to install Ruby on Rails on a machine running Mac OSX 10.7 - 10.9. We first install homebrew followed by ruby, gemsets and finally rails. Finally we test our installation of rails.

In this tutorial, you will learn how to install Ruby on Rails on Macs running OS newer than OSX Lion. As you’d have guessed, Ruby on Rails requires installation of Ruby. It is available by default in OS X but we need to get the latest version to get Rails working. After installing Ruby, we install rails as a ruby-gem. We will find about that later.

If you want to quickly install Rails and don’t care to read this reference, just fire up your Terminal application and run the highlighted commands of this tutorial chronologically.


Step 1. Install OSX Command Line Tools


Many open source applications on Unix systems like OS X get installed by downloading source code and compiling it on the target machine. Ruby on Rails also requires some tools and dependencies that need to be compiled locally. The compilation tools are not installed by default in OS X. To install them we need to install the OS X Command Line Tools as follows:

Xcode 4.x and 5.0 :

• Search for Xcode in Mac App Store and install it. It’s free! (You will need to login with your Apple ID before installing)

• In Xcode, go to Preferences -> Downloads-> Components and click “Install Command Line Tools”.

Xcode 5.0.1 in OSX Mavericks:

• Install Xcode as above.

• Open Terminal and run the following command:

brew install git


Step 2. Install Homebrew


Homebrew, quoted as the “missing package manager of OS X”, makes installation of open source applications a breeze. Using homebrew, you can install applications simply by running commands like “brew install app-name”.

We need to install Homebrew because Ruby requires some packages, which need to be installed using homebrew. To install homebrew, run the following command in the Terminal:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

After installation, run the following command:

brew doctor

This commands checks for any errors or depreciations in the installation of homebrew. If any errors are reported follow the instructions to rectify them. Now to update homebrew with the latest “formulas”, run the following command:

brew update


Step 3. Install Ruby on Rails with RVM (Ruby Version Manager)


When working with Ruby, which version you are using plays an important role. There might be situations where you’d need to switch to a different version of ruby for running a particular app. RVM takes control of the Ruby interpreter installed on your system and provides on the fly Ruby version switching.

Another great feature of RVM is the ability to create gemsets. A gemset is a collection of ruby-gems. When RVM selects a particular gemset, only the gems listed in that gemset is made available to Ruby. This provides sandboxing of ruby-gems.

Now RVM provides three installation options:

1. RVM with Rails and Ruby :

\curl -L https://get.rvm.io | bash -s stable --rails --autolibs=enable

This single command would install RVM, with latest Rails and Ruby interpreter. This is the easiest way to get Ruby on Rails installed. You may be asked to enter root password during the installation process, which can take a long time depending on your internet connection.

If this command runs successfully and you see mostly green and less red on your terminal, congratulations, you have Ruby on Rails installed on your system. Jump to Step 4. Testing your Rails Installation.

2. RVM with Ruby :

\curl -L https://get.rvm.io | bash -s stable –ruby

This would install RVM with the latest version of Ruby. The next step would be to install RubyGems manager by running the following:

rvm rubygems current

Now that rubygems is installed, install rails as follows:

gem install rails

Again, this is going to take long time since it will download and install all the dependencies of rails but be patient, after this step, you will have Rails installed. Continue to Step 4. Testing your Rails Installation.

3. Standalone RVM:

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

It will only install RVM. After this you need to run the following series of commands:

source ~/.rvm/scripts/rvm

rvm requirements

rvm install ruby

rvm use ruby –default

rvm rubygems current

gem install rails

As you can see this is more tedious than previous methods.


Step 4. Testing your Rails installation


Now that you have successfully installed Ruby on Rails on your Mac, its time to create a Hello World Rails application to see if everything is in order.

Note: I highly recommend using iTerm instead of the default Terminal application.

In a new Terminal tab, run the following command:

rails new helloworld

You will see a list of commands running. After this, a new folder named ‘helloworld’ is created in the current directory. Go to that directory:

cd helloworld

Now run the following command to run a local rails server:

rails server

Open your favorite browser and open this link http://localhost:3000 . If everything goes OK you should see the welcome page of Ruby on Rails.


Rohan Raja

Recently graduated, majoring in Mathematics and Computing from IIT Kharagpur, Rohan is a technology enthusiast and passionate programmer. Likes to apply Mathematics and Artificial Intelligence to devise creative solutions to common problems.