How to install Ruby in a macOS for local development

Written by:
wordpress-sync/blog-feature-ruby-security

September 27, 2022

0 mins read

If you’re starting out with Ruby or Ruby on Rails development, you’ll want to know:

  1. How to properly install Ruby on Mac.

  2. How to setup a local development environment that doesn’t conflict with brew or an existing install of Ruby 2.7 Mac

In this tutorial, you’ll learn how to do both! If you have the new macOS M1 laptops, don’t worry, this tutorial also works for installing Ruby on Mac M1.

Never run brew install ruby

Some guides and blog articles will instruct you to install Ruby by running brew install ruby or updating your existing Ruby versions on a macOS by running the update ruby mac command. I highly recommend you avoid that completely.

That said, Reddit user postmodern shared on a Ruby thread that you could probably instruct the brew installation to be explicitly installed onto a different directory. Brew by default installs to /usr/local and according to postmodern, this doesn’t interfere with the Apple system Ruby scripts because their interpreter location is hardcoded to /usr/bin/ruby. They also point out that Apple has plans for deprecating their pre-installed versions of Python, Ruby, and Perl.

Why can't you use system Ruby on Mac?

If you run commands that change your native Ruby version, such as brew install ruby or brew uninstall ruby, then you risk breaking features and capabilities in your macOS (this also applies to Mac M1).

Also, it’s important to point out that the preinstalled version of Ruby on the macOS is outdated, which, on a new 2022 M1 laptop, resolves to ruby 2.6.8p205. The latest version, Ruby 3.1.2, was released in April 2022, and is a major version upgrade from the one that is preinstalled on the macOS.

You’ll likely want to use Ruby 3 for a local development environment — which is a very good reason to move away from pre-installed Ruby versions. It also gives you the freedom to choose which version you want to work with. To make that magic happen we turn to Ruby virtual environments.

How do I check which version of Ruby is installed on my Mac?

To check which version of the Ruby interpreter you have natively installed, run the command:

1/usr/bin/ruby --version

If you haven’t upgraded, then the macOS Monterey version ships with Ruby 2.6.8. If you’ve upgraded, or manage Ruby versions using rbenv, rvm, or other Ruby version managers, then you might see a different version. Sometimes, you may also find Ruby in the file path /usr/local/bin/ruby when it is installed with Homebrew package manager for macOS.

Where is the Ruby folder on Mac?

The Ruby interpreter is often found natively on the macOS in the file paths /usr/bin/ruby or /usr/local/bin/ruby, depending on where it was installed. Ruby gems are installed and managed natively on macOS by the operating system, and using sudo gem install or making changes in those Ruby gems is discouraged. Instead, consider installing a Ruby virtual environment with a tool like rbenv.

The Ruby virtual environment

A great concept that is familiar with other programming languages and their tooling is the Ruby virtual environment. If you’ve used Python’s virtualenv or Node.js’s nvm, then Ruby’s virutalenv should be an easy switch. All you need to get started are a few Ruby ecosystem tools, such as rbenv or rvm.

These tools avoid conflicting with the native Ruby Mac version by fetching the Ruby source code from the internet, compiling it locally, and making it available under a path. They then instruct you to add this new directory path onto your shell of choice, so the new Ruby version is available to use when you open new terminal interfaces.

They are essentially Ruby version manager that allows you to quickly switch between different Ruby runtime versions as needed, depending on the Ruby install directory that was set.

Should you use rbenv or rvm?

Two popular Ruby virtual environments tools are rbenv and rvm. Since specific environment tools and versions tend to become outdated as the industry progresses, I won’t go into a detailed feature matrix comparison of rbenv vs rvm1. Instead, I asked some Ruby-savvy developer friends for their opinion, and went with rbenv since the majority of them recommended it.

Other Ruby virtual environment managers that I haven’t explored, yet are worth noting, are chruby and ruby-install.

Let’s get started with rbenv

The open source rbenv project is hosted on GitHub, and a great place for working through issues and getting up-to-date documentation from the rbenv README.

How to install Ruby rbenv

Just like many other tools, we turn to the popular Homebrew macOS package manager. Run the following command in your terminal:

1brew install rbenv ruby-build

Once everything is installed, we need to instruct rbenv to setup our shell, so we’re using the Ruby runtime version that we’ve customly installed with rbenv whenever we open a new terminal window to type commands in the prompt (as opposed to the preinstalled Ruby 2.6.8 version on the macOS).

To setup rbenv with your shell run the following command:

1rbenv init

Follow the on-screen printed instructions if any extra configuration is required on your part. You might be asked to paste a command in your shell’s configuration. If you’re on a macOS and haven’t changed from the default Z Shell (ZSH), then you’ll want to make sure you add the following line of code to the top of your ~/.zshrc file:

1eval "$(rbenv init - zsh)"

The rbenv install process is now complete. You might want to run some diagnostic checks to ensure that everything is set up correctly. If so, you can run the rbenv-doctor tool that the rbenv GitHub project provides:

  1. Open a new terminal window so that you have the new rbenv environment loaded

  2. Run the command:

    curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | zsh

Warning: This command shouldn’t be taken lightly, as you are piping commands into your development environment’s shell that originate from a public and open source GitHub repository. It is possible that a malicious actor had tampered with the source code, so you are highly encouraged to review the code properly before you run this command.

How do you use rbenv to install a new Ruby version?

Congrats on building your own Ruby virtual environment! Now that you have a functional rbenv setup we can use it to completely manage your Ruby versions.

To begin, you want to list which Ruby versions are available for installation so you can choose the latest Ruby release, or perhaps a specific version that you’re looking for. To use rbenv to list all Ruby versions, run the following command:

1curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | zsh

The output of which was:

12.6.10
22.7.6
33.0.4
43.1.2
5jruby-9.3.6.0
6mruby-3.1.0
7picoruby-3.0.0
8rbx-5.0
9truffleruby-22.1.0
10truffleruby+graalvm-22.1.0
11
12Only latest stable releases for each Ruby implementation are shown.
13Use 'rbenv install --list-all / -L' to show all local versions.

You can then proceed to installing one of these Ruby versions. Let’s use rbenv to install a Ruby version of 3.1.2:

1rbenv install 3.1.2

The command will then start fetching the relevant source code files to extract, compile, and then install the Ruby 3.1.2 runtime version available to your shell.

When you’ve finished installing it, you should set it to be the chosen version anytime your shell starts. To do that, you need to run:

1rbenv global 3.1.2

Finally, you can verify that this is working as expected by running the ruby --version in your shell, which should output a similar result:

1ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]

A note about rbenv global before we wrap up. Due to the naming of the command (global), I was concerned that running this command might somehow interfere with the pre-installed macOS Ruby version. This isn’t the case. The command refers to the global user configuration, which is the rbenv configuration file at ~/.rbenv/version, and its meaning is that it isn’t tied to a specific Ruby project / directory.

So, what’s next?

Ruby on Rails vulnerabilities and Ruby security vulnerabilities

Before you rush off to your Ruby development activities, consider the following resources to make sure your following secure coding conventions and keeping your Ruby gems safe from vulnerabilities. They’re great for Ruby on Rails vulnerabilities that manifest as part of the framework’s Ruby gems, as well as Ruby security vulnerabilities in general.

I highly recommend the following Ruby development and Ruby security related resources:

Lastly, don’t forget to stay alert of malicious remote code execution and other security risks in Ruby gems due to supply chain attacks that we’ve witnessed happening across various ecosystems, Ruby included.

Try our free Ruby code checker tool now, to see how the Snyk code engine analyses your code for security and quality issues.

Patch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo SegmentPatch Logo Segment

Snyk is a developer security platform. Integrating directly into development tools, workflows, and automation pipelines, Snyk makes it easy for teams to find, prioritize, and fix security vulnerabilities in code, dependencies, containers, and infrastructure as code. Supported by industry-leading application and security intelligence, Snyk puts security expertise in any developer’s toolkit.

Start freeBook a live demo