Tips and Tricks: Setting Up Ruby On OS X

And learn how to use the Mac terminal along the way.

In this article, we are going to look at how the CLI works on your Mac and install the official Ruby interpreter, the MRI. Let’s get started!

Note: Did you know that Apple computers come with Ruby pre-installed? It’s pretty sweet for running and testing quick scripts. The bad news is that it’s an older version of Ruby that doesn’t contain the latest updates and security patches added to the language. So we’re going to install our own up-to-date version of Ruby that will work side-by-side with the Apple version (which OS X uses for some of its own tasks).

The Terminal

On OS X, the CLI is called the terminal. To launch it, press CMD + Space to bring up Spotlight and type in terminal.

Starting Location

When you launch a new terminal on a Mac, you’ll always be located in your home directory — /Users/your_username. If you want to open it in the Finder, you can just type open . in the CLI. Don’t forget the dot, it means the directory I’m currently in.

Listing Directories And Files

To see what directories and files are inside your current location, type in ls. Much like Finder, you’ll see a list of directories and files. If you want to see more information about these files, you can use the -lah options like so: ls -lah. l means list, a means all (so it also shows hidden files) and h means human-readable, which will display file sizes in Kilobytes, Megabytes and Gigabytes.

Navigating The Disk

After viewing the directories in your current location using ls, you can open one of them — Documents, for example — by using the cd command like so: cd Documents. This will bring you to /Users/your_username/Documents.

cd stands for (c)hange (d)irectory. It’s the terminal way of entering a folder, just like double-clicking that folder in Finder.

To know what directory you’re currently in, type in pwd. It stands for (p)rint (w)orking (d)irectory and will show your location in the terminal.

To navigate one level higher in the hierarchy — meaning to leave the current directory and go inside its parent directory — type in cd … If you do it now, your command prompt will show that you’ve gone back to /Users/your_username.

If you want to go back to your user directory from any location, you can type cd ~. The tilde sign (~) is an alias for /Users/your_username.

Creating Directories

The mkdir command is used to (m)a(k)e a new (dir)ectory.

Start by navigating to your home directory. To do this, type in cd ~. Once you’re there, we’re going to make two new directories: testing and workspace.

To create testing, type in mkdir testing. To create workspace, type in mkdir workspace. It’s that easy.

To confirm that the two new directories were created, type in the ls -lah command — you should now see them on the list.

You can also open your home directory in Finder with open . to check that these directories were created.

Deleting Directories

Deleting a directory is as easy as creating one. The rm command stands for (r)e(m)ove, and we’ll use it to delete the testing directory that we just created. Note that to delete a directory, the -r option must be used. It stands for recursive. Also note that deleting a directory will delete all its content.

Type in rm -r testing and do a ls again — testing is gone!

Warning: Deleting directories and files from the terminal bypasses the trash can! You will not be able to retrieve a directory or file deleted from the terminal — so be careful!

Let This Installation Party Started

Apple uses the default, pre-installed version of Ruby for important system items, so we’ll be installing an updated version of Ruby that will run side-by-side with the default. Don’t worry, it’s pretty simple and you only need to do it once.

Xcode

Xcode is Apple’s IDE, or integrated development environment. You’re likely not going to use it for Ruby programming but it comes with several helpful tools that you’ll need to develop Ruby programs. Xcode is free for Mac users and is available from the Mac App Store. Open up the App store, search for Xcode, and install it.

Once Xcode is done installing, run it and agree to the Terms of Service. Xcode may ask you to enter your password and install some dependencies — this is normal. If it does, let it do it’s thing, then close Xcode — that’s all we needed from it there.

After you’ve installed and run Xcode once, open a new terminal and type in the following:

xcode-select --install

This will download and install some more tools — namely, the Apple Command Line Developer Tools. You may need to re-enter your user password and click accept and/or install on a message box. If so, go ahead and do so.

Homebrew

Homebrew is a package manager. Think of it like the Mac App Store: you choose software and install it from Homebrew. The twist is that Homebrew only works from the terminal! You won’t find Candy Crush on Homebrew — its catalogue only lists apps for developers and sysadmins. You’ll see in a minute why we need it. For now, we’ll just install it. Copy and paste the following line in the terminal:

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

At some point, it will ask for your Mac user password, which is normal. Type it in and press enter. Don’t be surprised if you don’t see your password being typed in the terminal — that’s just how terminal handles password prompts.

GPG

GNU Privacy Guard is an open-source data encryption software that provides privacy and authentication for data communication — basically, it keeps messages safe. The version of Ruby that we’ll be using will be installed through another app called RVM. RVM is signed with GPG, so we need to have GPG installed on the computer before we can install RVM.

Homebrew provides the GPG software in its catalog, so let’s install it now — it’s very easy! All you have to do is to type the following in your terminal, enter your Mac user password when prompted and that’s it:

brew install gpg

GPG is now installed on your Mac! I told you it was easy!

RVM

From the official RVM website:

RVM is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems.

Put another way, RVM allows us to install our own version of Ruby while letting OS X continue to use the one provided by Apple.

To install RVM, we first need to get its GPG key. Copy the following line and paste it in a terminal window:

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

Then, copy and paste the following (and be prepared to enter your Mac password once more):

RVM is now installed on your Mac, but there’s an important step you need to do next: close all terminal windows that may still be open. This forces OS X to load the RVM configuration the next time a terminal is open.

Ruby

Now we have everything needed to install our own version of Ruby using RVM. At the time of this writing, the latest Ruby version is 2.3. Open a terminal and type the following:

rvm install 2.3 --default

It will take some time to download and compile the MRI sources on your computer, so be patient. It will also ask you for your Mac password!

When the installation is finished, type the following in your terminal:

ruby -v

It should output something like the following:

ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]

Check for the version number in this line. It should say 2.3.0. If this is correct, your Mac is ready for Ruby!

If you are interested in learning how to code and get certified by one of the world’s top universities, visit exts.epfl.ch.