This guide will show you how to setup your Mac environment for optimal Ruby on Rails development.
Step 1: Package Manager
Homebrew is the best package manager for Mac OS X. You will need it to get the rest of the packages in this guide. Follow the simple instructions here to install it on your system.
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
Edit your /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.
This tutorial will show you how to install Ruby 1.9 with rbenv, but you can alternatively use RVM. Run the following:
$ 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
Running 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 memcached
:
$ 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
TotalTerminal is “a system-wide terminal available on a hot-key”. It is highly customizable and works with the default Terminal.app.
Bash Prompt
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 ~/.bash_profile
:
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.
Colors
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 ls
):
export CLICOLOR=1
The following command will add color to your git output:
$ git config --global color.ui true
Auto-Completion
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.
Zsh
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
Flamewars can start over the choice of an editor so we are going to avoid one here. If you like IDEs, try RubyMine, and if you like GUI-based text editors, try Sublime Text. They are both fantastic.
I like Vim, so I will go through my preferred Vim configuration for Rails development.
MacVim
MacVim is a gVim compatible vim frontend for Mac. Install it with Homebrew:
$ brew install macvim
Janus
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.
:Rake
to 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
Conclusion
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 Author