The Pragmatic Studio
get this course sign in

Install Ruby

Exercises

Objective

Welcome to your first exercise! We're delighted to have you along for the course.

Before we can start writing Ruby code, we need to get Ruby running on your computer. Now, depending on your operating system, you may already have a version of Ruby installed. But for this course you'll need to be running Ruby 2.

So just follow the steps below for your favorite operating system—Mac, Windows, or Linux— and we'll be on our way!

Mac OS X

The best way we've found to install Ruby and Rails on a Mac is using rbenv. 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 Ruby versions if you like. It takes a little extra effort up front, but it's well worth it in the end.

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 rbenv to install a separate user-level Ruby environment, rather than changing 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 Ruby 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$

    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. 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.

    We'll use the command prompt throughout the course to run Ruby and use some related command-line tools. In fact, here comes our first command...

  2. To install rbenv, first we need to install Homebrew which makes it easy to install and compile software packages from source.

    To install Homebrew, copy the following command and paste it after your Terminal prompt:

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    If you're asked about installing Apple's Command Line Tools for Xcode, go ahead and answer "yes".

  3. Now we can use Homebrew to install rbenv. To do that, run the following command in your Terminal session:

    brew install rbenv
  4. Next, set up rbenv in your Terminal's shell by running the following command and following the printed instructions:

    rbenv init
  5. Then close your Terminal session and open a fresh new Terminal session so that the changes take effect.

  6. Now that we have rbenv installed and the Terminal's shell set up properly, we're ready to install Ruby 2.7.2. To do that, type

    rbenv install 2.7.2

    This will download, compile, and install Ruby into a directory managed by rbenv. (If you're curious, it's under the ~/.rbenv directory.)

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

  7. When installation is complete and you're back at a command prompt, set Ruby 2.7.2 as the global Ruby version by typing:

    rbenv global 2.7.2

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

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

    ruby -v

    You should see something like the following, though your exact patch number, date, and revision may be slightly different:

    ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]

Great - now you have Ruby installed!

Choose A Text Editor

Throughout the course we'll be creating a Ruby program by putting Ruby code in one or more files. You'll need to choose a text editor to create and edit these files. The editor doesn't need to have a boat-load of features. In fact, we find that a basic editor that has Ruby syntax highlighting and basic file/directory browser works best.

In the videos we use TextMate (Mac only). However, you could just as well use Visual Studio Code which runs equally well on Mac, Windows, or Linux. And it's free!

You're now ready to start writing Ruby programs!

Windows

The easiest way to install Ruby on Windows is using RubyInstaller. It's a self-contained Windows-based installer (an .exe file) that includes a Ruby interpreter, a few command-line tools, and some documentation.

  1. Navigate to the RubyInstaller downloads page and select the Ruby 2.7.2-1 (x64) executable installer. Save the file to your Desktop, for example, and then run the file once it has finished downloading. You may need to navigate to where you saved the .exe file and double-click it to start the installation process.

  2. After stepping through a couple standard installer screens, you'll be prompted for the installation destination. By default, Ruby will be installed in your C:\Ruby27-x64 directory. We'll assume you go with the default destination.

    Wait! Don't rush on to the next screen until you've made sure to check the box marked Add Ruby executables to your PATH. This will automatically set up your environment so you can use Ruby from any command prompt. (For the curious, it simply adds the C:\Ruby27-x64\bin directory to your PATH environment variable.)

  3. When the installer finishes, open a new command prompt by selecting Start -> Run... and typing cmd into the dialog box. You should see a new window with a blinking cursor and a prompt that looks something like this:

    C:\Users\mike>

    If this is the first time you've seen this command prompt it may seem rather intimidating, but don't let it throw you. It's simply a way to interact with your computer by entering commands. We'll use the command prompt throughout the course to run Ruby and use some related command-line tools. In fact, here comes our first command...

  4. Verify that Ruby 2.7.2 was successfully installed by typing the following at the command prompt:

    ruby -v

    You should see something like this, though your exact patch number, date, and revision may be slightly different:

    ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x64-mingw32]

    If instead you see "Command not found", then you either need to open a new command prompt and try again, or check that your PATH environment variable includes the C:\Ruby27-x64\bin directory (see step #2).

  5. Finally, we need to install a bit more Ruby documentation. RubyInstaller includes documentation in Microsoft's Compiled HTML Help format (.chm files) to minimize the download file size. However, in this course we'll show you how to access the documentation in Ruby's standard RDoc format.

    Download the RDoc documentation by running the following command:

    gem install rdoc-data
  6. Then install the RDoc documentation by typing:

    rdoc-data --install

    If for some reason this doesn't work, don't worry about it. You can always use the online documentation instead.

And that's all there is to it!

Choose A Text Editor

Throughout the course we'll be creating a Ruby program by putting Ruby code in one or more files. You'll need to choose a text editor to create and edit these files. The editor doesn't need to have a boat-load of features. In fact, we find that a basic editor that has Ruby syntax highlighting and basic file/directory browser works best.

In the videos we use TextMate (Mac only). However, you could just as well use Visual Studio Code which runs equally well on Mac, Windows, or Linux. And it's free!

You're now ready to start writing Ruby programs.

Linux

Folks who run Linux tend to already be comfortable with the command line, installing and building software, and configuring things just the way they like. We're not so bold as to tell you exactly how to tweak your Linux install. The important thing is that you get Ruby 2.7.2 installed to match the version we'll use throughout this course.

Unless you have a strong preference, we recommend installing Ruby on Linux using rbenv. 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 Ruby versions if you like. It takes a little extra effort up front, but it's well worth it in the end.

  1. Start by installing rbenv:

    sudo apt install rbenv

    If you prefer a manual approach, follow the steps in the Basic GitHub Checkout section of the official rbenv installation documentation.

  2. Once you have rbenv installed, it's then easy to get Ruby installed by picking up with step 6 in the Mac OS X instructions above.

Choose A Text Editor

Throughout the course we'll be creating a Ruby program by putting Ruby code in one or more files. You'll need to choose a text editor to create and edit these files. The editor doesn't need to have a boat-load of features. In fact, we find that a basic editor that has Ruby syntax highlighting and basic file/directory browser works best.

In the videos we use TextMate (Mac only). However, you could just as well use Visual Studio Code which runs equally well on Mac, Windows, or Linux. And it's free!

You're now ready to start writing Ruby programs!

All course material, including videos, slides, and source code, is copyrighted and licensed for individual use only. You may make copies for your own personal use (e.g. on your laptop, on your iPad, on your backup drive). However, you may not transfer ownership or share the material with other people. We make no guarantees that the source code is fit for any purpose. Course material may not be used to create training material, courses, books, and the like. Please support us by encouraging others to purchase their own copies. Thank you!

Copyright © 2005–2021, The Pragmatic Studio. All Rights Reserved.