Setting up Ruby Environment in macOS

Photo by Jess Bailey on Unsplash

Setting up Ruby Environment in macOS

Introduction

In the previous article, I wrote about establishing a nodeJS environment in macOS. Given that I have also been learning about the Ruby on Rails framework, I thought that writing on Ruby environment setup is a worthy follow-up.

The meat of the installation process involves four main steps:

  • GnuPG and GMP packages installation.

  • Installing the Ruby Version Manager (RVM).

  • Ruby itself

  • Installing essential Ruby Gems

Important note:

Ensure that you have macOS Catalina or later installed. To check your macOS version, click on the apple icon at the top left of the screen, then click on About This Mac option. The pop-up displays the macOS name and version.

In short, your macOS version should be 10.15 or later. If that is not the case, please update your macOS by clicking on the software update button on the same pop-up.

Also before proceeding, check whether homebrew is already installed: Type the command brew --version on the Terminal and press <Enter>.

If homebrew is installed, the terminal should display the homebrew version, otherwise, if not already installed, an error arises: command not found: brew

To install homebrew, paste the command: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"on the Terminal and press <Enter>

The Process

1) GnuPG and GMP packages installation

GnuPG and GMP packages are prerequisites for Ruby Environment setup.

GnuPG stands for GNU privacy guard. GnuPG is a package that signs and encrypts data thus entrenching security. It's bemusing to know that GNU is itself a recursive acronym that reads Gnu's Not Unix in full.

Why is GnuPG necessary before installing Ruby as demanded by official docs? Simply, RVM employs GnuPG to verify that the Ruby installation package has integrity, and has been signed by the certified trusted source.

On its part, GMP means Gnu Multiprecision Library. A subtle hint of its purpose lies in its name - Multiprecision. It's responsible for precise arithmetic operations on floating point numbers, signed integers, and rational numbers. It powers these mathematical operations with high accuracy or precision.

Why GMP? GMP is especially critical for the Ruby BigDecimal library among other libraries.

Now, on with the installation. Search for the Terminal in applications and launch it.

  • Type in brew install gmp then press <Enter>.

  • Type in brew install gnupg the press <Enter>.

If an error or warning arises concerning GnuPG, solve it by typing in the command brew link gnupg on the terminal then press <Enter>

2) Ruby Version Manager (RVM) installation

RVM is a command-line tool for managing and working with multiple Ruby dev environments and allows easy switching between them. In other words, it allows a developer to download and install multiple Ruby versions.

For RVM installation, open the mac terminal and follow the following steps:

  • Type in curl -sSL https://rvm.io/mpapis.asc | gpg --import - then press <Enter>

  • Type in curl -sSL https://rvm.io/pkuczynski.asc | gpg --import - then press <Enter>

  • Type in \curl -sSL https://get.rvm.io | bash then press <Enter>

  • Once that's done, close and reopen the terminal application.

  • Now type in rvm then press <Enter> If a long message that includes the link rvm.io is printed on the screen, then the installation is successful.

3) Ruby Installation

At this juncture, we implement Ruby installation using RVM in a few commands:

  • Open Terminal.

  • Type in rvm install 2.7.4 --default then press <Enter> You can replace the version number 2.7.4 with one that fits your development needs - you can check Ruby versions here.

  • Type in rvm list then press <Enter> After a successful installation, you should see a message starting with Ruby version (In this case ruby-2.7.4).

4) Ruby Gems

Ruby gems are similar to node packages in Javascript - libraries that contain useful code nuggets to ease the coding process.

Here are the Gem installation steps:

  • Launch Terminal.

  • Type in gem update --system then press <Enter>

  • Type in gem install bundler then press <Enter>

  • Type in gem install pry then press <Enter>

  • Type in gem list | wc -l then press <Enter>

Conclusion

This article explains how to set up a Ruby environment in macOS concisely. As part of the prelude, it describes important details about checking the macOS version and how to determine whether homebrew is already installed.

The procedure is a detailed process that includes installing GnuPG and GMP packages, the Ruby Version Manager (RVM), Ruby itself, and crucial Ruby Gems.

I hope you have found this article helpful. Follow for more Ruby, Ruby on Rails, and Javascript articles.