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 install Ruby 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 3.
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.
-
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...
-
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:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
If you're asked about installing Apple's Command Line Tools for Xcode, go ahead and answer "yes".
-
Now we can use Homebrew to install rbenv. To do that, run the following command in your Terminal session:
brew install rbenv
-
Next, set up rbenv in your Terminal's shell by running the following command and following the printed instructions:
rbenv init
-
Then close your Terminal session and open a fresh new Terminal session so that the changes take effect.
-
Now that we have rbenv installed and the Terminal's shell set up properly, we're ready to install Ruby 3.2.2. To do that, type
rbenv install 3.2.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...
-
When installation is complete and you're back at a command prompt, set Ruby 3.2.2 as the global Ruby version by typing:
rbenv global 3.2.2
This sets Ruby 3.2.2 as the default version to be used whenever you open any new Terminal sessions.
-
Then verify that Ruby 3.2.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 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
Great - now you have Ruby installed!
Choose A Code 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 code editor to create and edit these files. The editor doesn't need to have a ton of features. In fact, a basic code editor that has Ruby syntax highlighting and a file/directory browser works best.
In the videos we use Visual Studio Code which runs equally well on Mac, Windows, or Linux. And it's free! We also use the following VS Code extensions:
-
Code Runner extension for running Ruby program files, with the following VS Code settings:
"code-runner.clearPreviousOutput": true, "code-runner.showExecutionMessage": false, "code-runner.ignoreSelection": true,
You can add these settings by opening your user settings in VS Code using the Preferences: Open Settings (JSON) command.
-
Material Theme extension for the VS Code color theme.
You're now ready to start writing Ruby programs!
Windows 11
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.
-
Navigate to the RubyInstaller downloads page and click the Ruby+Devkit 3.2.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 use Windows Explorer to navigate to where you saved the executable file and double-click it to start the installation process.
-
After accepting the license agreement, the installer will ask you to select components to install. Make sure the checkmarks labeled "Ruby RI and HTML documentation" and "MSYS2 development toolchain" are checked. MSYS2 is required to install Ruby gems with C extensions. Then click "Next" and Ruby will be installed in the
C:\Ruby32-x64
directory as promised.Wait! Do not click "Finish" on the last installer screen until you've made sure the checkmark labeled "Run 'ridk install' to set up MSYS2 and development toolchain" is checked. With that checkbox checked, when you click "Finish", a new command window will open asking you which components of MSYS2 you want to install. Choose "1" which is the base MSYS2 installation. It will proceed to install a bunch of stuff, and then return back to the prompt asking which components you want to install. You should see the message "MSYS2 seems to be properly installed". Go ahead and close that command window.
-
Next, open a new command prompt by selecting the Start menu, typing
cmd
into the search box, and pressingEnter
. You should see a new window with a blinking cursor and a command 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...
-
Verify that Ruby 3.2.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 3.2.2 (2023-03-30 revision e51014f9c0) [x64-mingw-ucrt]
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 theC:\Ruby32-x64\bin
directory (see step #2).
And that's all there is to it!
Choose A Code 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 code editor to create and edit these files. The editor doesn't need to have a ton of features. In fact, a basic code editor that has Ruby syntax highlighting and a file/directory browser works best.
In the videos we use Visual Studio Code which runs equally well on Mac, Windows, or Linux. And it's free! We also use the following VS Code extensions:
-
Code Runner extension for running Ruby program files, with the following VS Code settings:
"code-runner.clearPreviousOutput": true, "code-runner.showExecutionMessage": false, "code-runner.ignoreSelection": true,
You can add these settings by opening your user settings in VS Code using the Preferences: Open Settings (JSON) command.
-
Material Theme extension for the VS Code color theme.
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 3.2.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.
-
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.
-
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 Code 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 code editor to create and edit these files. The editor doesn't need to have a ton of features. In fact, a basic code editor that has Ruby syntax highlighting and a file/directory browser works best.
In the videos we use Visual Studio Code which runs equally well on Mac, Windows, or Linux. And it's free! We also use the following VS Code extensions:
-
Code Runner extension for running Ruby program files, with the following VS Code settings:
"code-runner.clearPreviousOutput": true, "code-runner.showExecutionMessage": false, "code-runner.ignoreSelection": true,
You can add these settings by opening your user settings in VS Code using the Preferences: Open Settings (JSON) command.
-
Material Theme extension for the VS Code color theme.
You're now ready to start writing Ruby programs!