The Pragmatic Studio

Installing Ruby and Rails on Mac OS X

September 23, 2010

Updated: January 17, 2016

All of our online courses start with comprehensive instructions for getting the required software installed and set up on your own computer. For the online Rails course, that means installing Ruby and Rails. During the course, you’ll then write, refactor, and test your code directly on your own computer. That way, after the course, you’ll already be familiar with the environment where you can then start writing your own Rails apps!

Setting up a stable Ruby and Rails environment on your Mac has never been easier. Here’s our recommended approach…

Install Ruby 2 and Rails 5

The best way we’ve found to install Ruby and Rails on a Mac is using the Ruby Version Manager (RVM). It’s a command-line tool that makes it easy to install and manage multiple independent Ruby environments on the same machine. That way, you’ll be able to easily switch back and forth between different Ruby and Rails versions.

Note that Mac OS X ships with a version of Ruby. However, it’s best not to mess around with the system-installed Ruby as it’s intended to be used by the operating system and apps installed by Apple. So we’ll use RVM to install a separate user-level Ruby environment, rather than mucking around with the system-installed Ruby.

  1. First, find the Terminal application (it’s under the Applications -> Utilities directory) and drag it onto your dock. You’ll end up using Terminal a lot as a Rails developer, so it’s good to have it handy. Then open a new Terminal session. You should see a new window with a cursor and a prompt that looks something like this:

    enoch:~ mike$

    The default prompt includes the computer name (enoch in my case), the current working directory (tilde represents your home directory), the current user name (mike), and a trailing $ which is the prompt for input.

    If this is the first time you’ve seen this side of a Mac, it may seem rather intimidating. Don’t let it throw you. It’s simply a way to interact with your computer by entering commands. In fact, here comes our first command…

  2. RVM compiles Ruby versions from source code. To do that, RVM uses the GCC compiler and other build tools. Unfortunately, GCC isn’t installed by default on Mac OS X. GCC is only available after you’ve installed Apple’s Command Line Tools for Xcode.

    Check to see if you already have GCC installed by typing the following at the command prompt:

    gcc --version

    If you see a version number, then you’re all set. If the gcc command isn’t found, you may get a dialog asking if you’d like to install Apple’s Command Line Tools for Xcode. Go ahead and click "Install" to automatically download and install the Command Line Tools.

    Otherwise, download the Command Line Tools for Xcode. To access the downloads you may need to register as an Apple developer if you haven’t already done so. Once you’re on the downloads page, search for "command line tools" (in the search field on the left) and then click on the appropriate version of "Command Line Tools for Xcode". Click on the .dmg link to download it. Once it has finished downloading, simply double-click the .dmg file and then double-click the enclosed .pkg file to start the installation process.

  3. Next, to install RVM from its GitHub repository (the recommended way), we need a working version of the git version control system. It may seem like overkill at this point to install a version control system. However, as you become more familiar with the Rails community you’ll find that having git installed will make things a lot easier. And it’s easy enough to install, so we may as well get it out of the way now.

    Check to see if you already have git installed by typing the following at the command prompt:

    git --version

    If you see a version number, then you’re good to go. (Git is included in Apple’s Command Line Tools.) If the git command isn’t found, then download the latest version of the graphical Git installer from the git-osx-installer downloads page. Once it has finished downloading, simply double-click the .dmg file to start the installation process.

  4. With that out of the way, install RVM by going back to your Terminal prompt and typing (or copying and pasting) the following:

    curl -L | bash -s stable
  5. When the RVM installation is complete (it’s fairly quick), close your Terminal session and open a new Terminal session. Then confirm that RVM is being properly loaded in the new Terminal session by typing

    rvm --version

    You should see a version number with no errors.

  6. Now that we have RVM installed, we’re ready to install Ruby 2. To do that, type

    rvm install 2.3.3

    This will download, compile, and install Ruby into a directory managed by RVM (it’s under the ~/.rvm directory). Initially you may see some help information displayed, followed by a prompt at the bottom of the screen. Press the spacebar to scroll through the help pages, or press ‘q’ to skip over the help and proceed with installation.

    Installing Ruby may take a while, so feel free to grab a refreshing beverage or a tasty snack.

  7. When the installation is done, close your Terminal session and open a new Terminal session. Then set Ruby 2 as the current Ruby version by typing

    rvm --default use 2.3.3

    This also sets Ruby 2 as the default version to be used whenever you open any new Terminal sessions.

  8. Then verify that Ruby 2 is the current Ruby version by typing

    ruby -v
  9. Now generate the core Ruby documentation by typing

    rvm docs generate-ri

    This takes a little while to complete, but in the end you’ll have all the Ruby documentation at your fingertips.

  10. Now we’re ready to install Rails! Rails is distributed via RubyGems: the standard Ruby package manager. When you installed Ruby, the RubyGems system came along for the ride. With RubyGems already installed, it’s easy to install Rails and its dependencies.

    Install Rails 5 by typing

    gem install rails --version 5.0.0 --no-ri --no-rdoc

    Then sit back and relax as RubyGems downloads all the Rails-related gems. After a minute or so, you should end up with a couple dozen gems installed.

  11. Finally, verify that Rails 5 was successfully installed by typing

    rails -v

Now you have the latest versions of Ruby and Rails installed!

Create An Example Rails App

Now that we have all the required software installed, let’s create your first Rails app to make sure everything is working in harmony. We’ll create a simple application for managing a list of todos.

  1. From a command prompt, navigate to a directory where you want the application code to live (~/work, for example).

  2. Start by creating an empty Rails application called todos:

    rails new todos
  3. Change into the todos directory that was created in the previous step:

    cd todos
  4. The application doesn’t know about todos yet, so we’ll use scaffolding to quickly generate all the code for managing a list of todos. Run the scaffold generator by typing

    rails g scaffold todo name:string due_on:date completed:boolean

    You’ll see Rails create a bunch of files, including a migration file for creating a database schema to store todo items in a database (SQLite3 in this case).

  5. Run the database migration by typing

    rake db:migrate
  6. Then start the Rails app by typing

    rails s
  7. Finally, point your web browser at http://localhost:3000 and you should see a page welcoming you to Rails. To start managing your todos, go to http://localhost:3000/todos.

  8. When you’re done, you can stop the Rails app by typing CTRL-C in the command prompt where you started the app.

Next Steps

That’s all there is to it! Now you have everything you need to start building your own Rails apps. And that’s exactly how we recommend you start learning Rails, by actually building something, whether it be for fun or profit.

Ruby and Rails Course Package

If you're ready to dive into developing with Ruby and Rails, there's no better set of courses to put you on a path to success than our popular Ruby and Rails Course Package. You'll come away with a solid understanding of the fundamentals of Rails, and how to put all the pieces together, so you can confidently create your first Rails app or jump right into an existing app!