I’m often told that people have difficulty installing Ruby on their Macs. While OS X does ship with Ruby (located in /usr/bin/ruby), this version is outdated and requires root permissions to install gems, so it’s not exactly ideal. The correct approach is to install a Ruby version manager, which allows you to install multiple versions of Ruby, storing them in your home directory so you never need root permissions. Here, I show how to use a Ruby version manager to install the latest version of Ruby and set it as the default.

Install Homebrew if you have not already. You will be asked to enter your password and may also be required to install the Xcode Command Line tools. For the most part, Homebrew is able to sort all of this out by itself, requiring minimal interaction from you. bash ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

At this point it is a good idea to run brew doctor and fix any problems that Homebrew detects. If you see “Your system is ready to brew.” you can proceed to the next step.

Use Homebrew to install rbenv and ruby-build. bash brew install rbenv ruby-build

Set rbenv to initialize when you launch a shell. ZSH users should replace “.bash_profile” with “.zshrc” in the following command. Users of alternative shells, such as fish may have to use a different command here. bash echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

At this point, you’ll want to restart your Terminal so that your profile changes take effect.

List the available versions of Ruby to find the latest one. At the time of writing, the latest version is 2.1.2. bash rbenv install --list

Install the desired version of Ruby. This could take a while because rbenv has to compile Ruby from source. bash rbenv install 2.1.2

Set the newly installed version of Ruby to be the default version. bash rbenv global 2.1.2

Now that you’re using rbenv to use Ruby, there are some slight differences you should be aware of:

  • You do not need to, and should not, use sudo for installing gems. If you find that you do need to use sudo, rbenv is probably not working properly and you should ensure that it is initialized in the profile for your current shell.
  • Installed gems are specific to the Ruby version that installed them. If you install a gem under one Ruby version, then switch to another version, you will have to reinstall the gems.
  • If you install a gem that provides a command-line utility, you may have to run rbenv rehash before it is available for use.