This guide will show you how to setup your Mac environment for optimal Ruby on Rails development.
Step 1: Package Manager
Once you’ve installed Homebrew, run the following command to make sure it is working correctly:
$ brew doctor
The first time you run it, you will get an error like the following:
Error: /usr/bin occurs before /usr/local/bin
/etc/paths file to look like this:
/usr/local/bin /usr/bin /bin /usr/sbin /sbin
Restart your shell and run
brew doctor again. It should say:
Your system is raring to brew.
You are now ready to install packages on your Mac:
$ brew install git mercurial
Step 2: Ruby
The default Ruby on Mac OS, version 1.8.7, is outdated for most Rails projects. The best way to upgrade is to install Ruby via a package manager.
$ brew install rbenv ruby-build
Note: In the output of the previous command, you will see a message like this:
==> Caveats To enable shims and autocompletion, add rbenv init to your profile: if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
Pay close attention to these caveats! Homebrew uses these messages to give you further instructions for configuring your package. In this case, add the specified line to your
~/.bash_profile (create the file if it does not exist) and restart your shell.
Once you’ve installed rbenv, you are ready to install Ruby 1.9. Run
rbenv install to see a list of rubies available. Install the latest version you see:
$ rbenv install 1.9.3-p194
Set this version as your default:
$ rbenv global 1.9.3-p194
ruby -v should give you the version you just installed. Install some useful gems:
$ gem install bundler pry
Step 3: Databases
This command will install some common databases:
$ brew install postgres mysql mongodb redis memcached
You only need to install the packages you need. Remember to follow the instructions in the caveats! For example, you will need to run the following for
$ mkdir -p ~/Library/LaunchAgents $ cp /usr/local/Cellar/memcached/1.4.13/homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/ $ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist
If you missed the caveats for any of the packages, eg. mysql, run
brew info mysql to see it again.
Step 4: Shell
As a Rails developer, you will spend a lot of time in the shell. The following tools and tweaks will make your life easier. The final product should look similar to this:
TotalTerminal is “a system-wide terminal available on a hot-key”. It is highly customizable and works with the default Terminal.app.
Having an informative prompt is extremely useful for keeping track of your development. In my prompt, I list my current directory, ruby version and source control branch.
In order to display information about the version control in the current directory, you will need vcprompt:
$ brew install mercurial # if you didn't get this from Step 1 $ brew install --HEAD vcprompt
Add the following lines to your
C_DEFAULT="[\033[m]" C_WHITE="[\033[1m]" C_BLACK="[\033[30m]" C_RED="[\033[31m]" C_GREEN="[\033[32m]" C_YELLOW="[\033[33m]" C_BLUE="[\033[34m]" C_PURPLE="[\033[35m]" C_CYAN="[\033[36m]" C_LIGHTGRAY="[\033[37m]" C_DARKGRAY="[\033[1;30m]" C_LIGHTRED="[\033[1;31m]" C_LIGHTGREEN="[\033[1;32m]" C_LIGHTYELLOW="[\033[1;33m]" C_LIGHTBLUE="[\033[1;34m]" C_LIGHTPURPLE="[\033[1;35m]" C_LIGHTCYAN="[\033[1;36m]" C_BG_BLACK="[\033[40m]" C_BG_RED="[\033[41m]" C_BG_GREEN="[\033[42m]" C_BG_YELLOW="[\033[43m]" C_BG_BLUE="[\033[44m]" C_BG_PURPLE="[\033[45m]" C_BG_CYAN="[\033[46m]" C_BG_LIGHTGRAY="[\033[47m]" export PS1="n$C_DARKGRAY[$C_RED$(rbenv version-name)$C_DARKGRAY] $(vcprompt -f '$C_DARKGRAY[$C_GREEN%n:%b%m%u$C_DARKGRAY] ')$C_PURPLEu$C_DARKGRAY @ $C_BLUEh $C_DARKGRAY: $C_LIGHTYELLOWwn$C_DARKGRAY$$C_DEFAULT "
Feel free to to tweak it to your liking. Note that if you are using RVM instead of rbenv, your prompt will need a different command.
I find tomorrow-theme‘s color scheme very pleasing. To install, just clone the repository and open
Tomorrow Night.terminal in Finder.
Add the following line to your
~/.bash_profile to add color output to some default commands (like
The following command will add color to your git output:
$ git config --global color.ui true
Bash, the default shell, has very good autocompletion support. To enable it, install bash-completion:
$ brew install bash-completion
Add the following snippet (mentioned in the caveats) to your
~/.bash_profile to enable homebrew’s built in auto-completion:
if [ -f `brew --prefix`/etc/bash_completion ]; then . `brew --prefix`/etc/bash_completion fi
You can now auto-complete most packages installed by homebrew. For example, type
git checkout<tab> to have it prompt you with a list of checkout targets.
If you want your shell to be even more configurable, you may want to look into zsh. Oh-my-zsh is a good starter configuration for this extremely powerful and configurable bash alternative.
Step 5: Editor
I like Vim, so I will go through my preferred Vim configuration for Rails development.
MacVim is a gVim compatible vim frontend for Mac. Install it with Homebrew:
$ brew install macvim
Janus is a curated list of useful vim plugins. It is highly configurable, so you can easily add your own configurations and add/remove other plugins. Installation instructions can be found on its Github page. A small list of plugins it includes by default:
- Rails.vim, for navigation, syntax highlighting and a lot more (eg.
:Raketo run tests on the current file)
- Fugitive.vim, awesome vim integration
- NerdTree, file explorer
- Ack.vim, fast file search
- ctrl-p, fuzzy file finder
You can find the full list of features here. A few additional plugins:
$ mkdir ~/.janus && cd ~/.janus $ git clone https://github.com/bbommarito/vim-slim # Syntax highlighting for Slim templates $ git clone https://github.com/godlygeek/tabular # Easy formatting for tables, useful for Cucumber features
And a beautiful color scheme:
$ cd tomorrow-theme # wherever you cloned it from Step 4 $ cp vim ~/.janus/tomorrow-theme $ echo color tomorrow-night > ~/.vimrc.after
That’s it! You are now ready to develop Ruby on Rails applications on your Mac:
$ git clone https://github.com/railstutorial/sample_app && mvim sample_app
About the AuthorMore Content by Tanzeeb Khalili