tutorial

Install Rails 4 correctly on OS X Mountain Lion (& Mavericks)
2013, July 17

UPDATE: Since this article was written, I received a lot of feedback and I decided to give a shot at creating an online course on web development. From novice to employable. Have a look here: HowToCode.io.

UPDATE 2: The following steps are also working perfectly on Mac OS X Mavericks

This is my way of setting up a Rails 4 development environment on Mac OS X Mountain Lion. By correctly setting up RVM, the relevant Ruby version and Rails 4. I'll assume you have a fresh install of OS X.

What is RVM and why you need it

In order to develop using the Rails framework, you need to have a Ruby interpreter installed on your machine. The good news is that OS X comes pre-installed with one. The bad news is that it is an older version than the one we need. OS X comes with 1.8.x and for Rails 4, we need at least version 1.9.3 and in this guide, we will install 2.0.0. We can't simply update the pre-installed version of Ruby as Mountain Lion has components that depends on the 1.8 version. Enters RVM.

Ruby Version Manager or RVM for short is a very useful piece of software that will allow us to have multiple versions of Ruby (and Rails and any other Gem) on our machine. Think of RVM as a virtual environments manager which will house specified versions of Ruby. You can then switch between versions at will and even force a specific version when you cd into a directory. We'll see how to do that.

RVM will also allow us to have one version of Rails per project. If, like me, you work on multiple projects at the same time, this will save you a lot of troubles.

Unfortunately again, the current RVM version will not install directly on the stock release of OS X Mountain Lion. But worry not and read on.

Install RVM

This way of installing RVM is based on Mark's gist. First of all, you will need to install MacPorts, a Mac OS X package manager. Get the latest version at www.macports.org. Once installed, you can install a new version of cURL (which we'll use to download and install RVM). To do so, fire up a terminal and issue:

 sudo port -v selfupdate sudo port install curl 

Note that OS X will ask for your password. Then, check your version of cURL. It should be higher or at least equal to 7.26:

 /opt/local/bin/curl -V 

Now, we can install RVM. Do so by issuing these commands, one by one:

 sudo port -v selfupdate sudo port install apple-gcc42 sudo port install gmake sudo port install gpatch sudo ln -s /opt/local/bin/gcc-apple-4.2 /opt/local/bin/gcc sudo ln -s /opt/local/bin/gmake /opt/local/bin/make sudo ln -s /opt/local/bin/gpatch /opt/local/bin/patch curl -L get.rvm.io | bash -s stable 

Once done, you have RVM on your machine. Source it and add it to your path:

 source ~/.rvm/scripts/'rvm' export PATH=/opt/local/bin:$PATH 

Install the correct version of Ruby

Now that we have RVM installed, we can proceed with installing Ruby 2.0.0. In your terminal window, issue these:

 rvm install 2.0.0 rvm use 2.0.0 --default 

We now have Ruby 2 installed on our system. Try issuing ruby -v in your terminal and it should tell you that you have 2.0.0 installed.

Create a Gemset and install Rails 4

RVM allows us not only to have different versions of Ruby but also different versions of Rails. I recommend creating one environment per project your are working on with its own installation of Rails. Create and move to a directory you want to use for storing our demo project. You will redo this step each time you start a new Rails project. Here, I use 'demo' as a name for my environment and project.

First, we create the environment and make sure that each time we cd into the directory, the correct environment (with its gems) gets loaded:

 mkdir demo cd demo rvm use 2.0.0@demo --create rvm --rvmrc 2.0.0@demo 

You can cd out of the directory and cd back in. You will see a message that informs you that your RVM environment will change to 'demo'. Accept with (y)es:

 cd .. cd demo 

Now, install Rails 4:

 gem install rails 

...and create the demo app:

 rails new demo_app 

Test your Rails install

From within your 'demo_app' directory, start a Rails web server with the following command:

 rails server 

You can now lauch your web browser and navigate to:

 localhost:3000 

This will open the default Rails 4 welcome page. You see it? That means Rails 4 is correctly installed in its own sandbox on your Mac OS X Mountain Lion machine.

If you have any comment or question, feel free to contact me or tweet.

back to top | share this on Twitter: