Setting Up a Ruby Programming Environment on macOS
Ruby is a dynamic programming language you can use to write anything from simple scripts to games and web applications. It was first released in Japan in 1993, but gained popularity in 2005 as a language for server-side web development. Ruby is designed to be easy to use and fun for beginners, but powerful enough to create complex systems. It’s a great choice for beginners and experienced developers alike.
Ruby is already included in a default macOS installation, although it won’t be the most recent version. You may run into compatibility issues when following tutorials or attempting to use other projects if you use it.
In this tutorial, you’ll set up a Ruby programming environment on your local macOS machine using Homebrew, and you’ll test your environment out by writing a simple Ruby program.
Prerequisites
You will need a macOS computer running El Capitan or higher with administrative access and an internet connection.
Step 1 — Using the macOS Terminal
You’ll use the command line interface (CLI) to install Ruby and run various commands related to developing Ruby applications. The command line is a non-graphical way to interact with your computer. Instead of clicking buttons with your mouse, you’ll type commands as text and receive text-based feedback. The command line, also known as a shell, lets you automate many tasks you do on your computer daily, and is an essential tool for software developers.
To access the CLI, you’ll use the Terminal application provided by macOS. Like any other application, you can find it by going into Finder, navigating to the Applications folder, and then into the Utilities folder. From here, double-click the Terminal application to open it up. Alternatively, you can use Spotlight by holding down the COMMAND key and pressing SPACE to find Terminal by typing it out in the box that appears.
macOS Terminal
If you’d like to get comfortable using the command line, take a look at An Introduction to the Linux Terminal. The CLI on macOS is very similar, and the concepts in that tutorial are directly applicable.
Now that you have the Terminal running, let’s install some prerequisites you’ll need for Ruby.
Step 2 — Installing Xcode’s Command Line Tools
Xcode is an integrated development environment (IDE) that is comprised of software development tools for macOS. You won’t need Xcode to write Ruby programs, but Ruby and some of its components will rely on Xcode’s Command Line Tools package.
Execute this command in the Terminal to download and install these components:
xcode-select --install
You’ll be prompted to start the installation, and then prompted again to accept a software license. Then the tools will download and install automatically.
Step 3 — Installing and Setting Up Homebrew
While the CLI on macOS has a lot of the functionality you’d find in Linux and other Unix systems, it does not ship with a good package manager. A package manager is a collection of software tools that work to automate software installations, configurations, and upgrades.
To install Homebrew, type this command into your Terminal window:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Step 4 — Installing Ruby
With Homebrew installed, install Ruby with the following command:
brew install ruby
To check the version of Ruby that you installed, type:
ruby -v
Step 5 — Creating a Program
Create a file called hello.rb
:
nano hello.rb
Type the following code into the file:
puts "Hello, World!"
Run the program:
ruby hello.rb
Conclusion
This small program proves that you have a working development environment. You can use this environment to continue exploring Ruby and build larger, more interesting projects.