Configuring OS X for Your First Rails Project

This post outlines a procedure to setup a new Mac for developing Rails projects. There are other ways to setup a Mac for developing your Rails app, but this method ensures that your Rails project is deployed using best practices.

The tools that will be used in this setup:

Setup Xcode

The easiest method to install Xcode in newer versions of OS X is to use the Mac App Store. Search for Xcode in the App Store, and then click on Install.

Accept the Xcode license

The Xcode license can then be accepted by opening Terminal.app and typing:

sudo xcodebuild -license

Scroll through the license to the end and press enter to accept.

Other ways to accept the license

You can also accept the license by opening Xcode and accepting the license when it first opens.

Install Xcode Tools

Now you can install Xcode tools, our main purpose in installing Xcode. Use the following command in your terminal:

xcode-select --install

This will guide you through the installation of the xcode-tools library, which is a requirement for some of the other tools we will be installing.

Alternative methods to install Xcode tools

Xcode tools can also be installed through the Xcode GUI, by using Homebrew, or through TextWrangler (requires downloading TextWrangler).

Homebrew

Homebrew is a package manager for OS X. It can be installed with a single command in the Terminal. To install Homebrew, follow the simple command on their website:

Homebrew - install instructions

As of the time of this post, the command to install Homebrew is

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

mysql

Although OS X ships with a default mysql installation, you will probably want to use Homebrew to manage your mysql installation.

The reason for doing it this way is to have more control of the version of mysql that you are using. If your application(s) require a specific version of mysql that isn’t the version that comes with OS X, you can ensure that the version you need is being used.

Alternatively, if you want to be sure that you are using the most recent version of mysql, Homebrew makes it easy to manage your mysql versions.

To install mysql using Homebrew, invoke the following command in the terminal:

brew install mysql

To install a specific version of mysql, use

brew install mysql -v version

Where version is the version number you need to install.

Configure mysql

First, setup mysql to start at login via launchd:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

Then, mysql can be launched using launchctl:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

To override mysql settings, you can create a configuration file in /etc/ called my.cnf.

Ruby and rvm

One of the best ways to ensure that the right environment is used for each of your ruby projects is using a ruby environment manager, such as rvm.

To install rvm, use the following terminal command (available on the rvm homepage):

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

Install and set ruby version

Once rvm is finished installing, you can install the version of ruby needed for your project using it.

For example, to install ruby 2.1.5 and set it as the default version (overriding the system ruby), use

rvm install 2.1.5 --default

You can check which version of ruby is currently being used by using

rvm list

Bundler

By default, rvm will install the dependency manager Bundler when it is installed.

Clone your project (which should already have a Gemfile) and navigate to the root folder of the project in terminal. If your project doesn’t already have a Gemfile set up, you can read more about setting one up in the Gemfile section of the Bundler guide.

For more about cloning a repository, read the Clone section of the Fetching a Remote page in the GitHub help pages.

From the project’s root folder in terminal, use bundler to install the project’s gemset:

bundle install

The project gemset will be installed.

Setup the Rails Project’s Database

Now you will need to setup the database for your Rails project.

Configure your project’s database.yml file with the right database information, then run

rake db:setup

Once the database is done being setup, run the database migrations using

rake db:migrate

Start your Rails server, navigate to the project in your browser, and your project should be there.