There are many reasons that I have always wanted to learn Rails. The first is that it’s written in Ruby, a language with elegant syntax that can bring joy to developers. Second, Rails is a convention over configuration framework, it forces developers to follow best practice, agile development flow to be able to get the most out of it. Despite all that, I’ve never been able to be determined enough to learn it fully and use it in my work. But now the chance has come, let’s get started with an OSX environment ready for Rails.

First, we downloading and installing some dependencies so that ruby can be compiled in our OSX system. Of course, you can use the system ruby come with OSX, but I can guarantee that it will bring to you in the future. We will need to install the Xcode Command Line Tools. Either download it directly from Apple website, or open Xcode, go to Preferences->Downloads, download it from here or if you are using OSX 10.9 type the following command in the terminal

$ xcode-select --install

Now, installing the needed compiler to build ruby from source using Homebrew (In case you don’t know what Homebrew is and you are using Mac OSX, I suggest you go immediately to its website and read the docs to install it)

$ brew install apple-gcc42

The second thing we must do is installing a ruby version manager to manage and install ruby. In Ruby world, RVM, rbenv, chruby are the 3 most popular version manager. I used rvm, but it’s just too heavy so I switch to rbenv and have been happy with it so far (I haven’t tried chruby yet but have heard some good things about it). Installing rbenv is really easy with Homebrew

$ brew install apple-gcc42
$ brew install rbenv

In case, brew does not add the rbenv bootstrap command to your .bashrc or .zshrc, do it yourself

# .zshrc or .bashrc

if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

Default, rbenv will create a .rbenv folder in your home folder and use it to hold ruby installations and gems but I prefer to use Homebrew’s directories so I do as Homebrew’s instruction and add the following to my .zprofile (.bashrc, .zshrc… in your case)

export RBENV_ROOT=/usr/local/var/rbenv

We also must install ruby-build so that rbenv can download and install ruby

$ brew install ruby-build

Yay, we are ready to install ruby with rbenv

$ rbenv install 2.0.0.p353 # The latest ruby version
$ rbenv global 2.0.0.p353 # Make it the default ruby version instead of system one

$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin13.0.0]
$ which ruby
$ which gem

Next is installing some necessary gems (for me)

$ rbenv install 2.0.0.p353 # The latest ruby version
$ gem install bundler rails pry cocoapods awesome_print
# Rehash so that shim executables are available for newly installed gems
$ rbenv rehash

I also installed some useful rbenv plugins using brew (the main reason that I use brew’s directories for rbenv: rbenv plugins installed with brew work nicely with brew’s directories).

# You can google, and find out the functions of each plugin (They are pretty useful)
$ brew install rbenv-binstubs rbenv-default-gems rbenv-gem-rehash rbenv-vars

Congratulation, we are all set now. Let create our first rails app.

$ rails new hello_world
$ cd hello_world
# Want to know what the following command do, go to rbenv docs or I will cover it in a later blog post
$ bundle install —binstub .bundle/bin