How To Install Ruby on Rails with rbenv on macOS – DigitalOcean

Introduction

Ruby on Rails is a popular application stack for developers looking to create websites and web applications. The Ruby programming language, combined with the Rails development framework, makes application development fast and efficient.

One way to install Ruby and Rails is with the rbenv command-line tool. Using rbenv will provide you with a robust and well-controlled environment for developing your Ruby on Rails applications, allowing you to easily change your working version of Ruby as needed.

rbenv provides support for specifying specific versions of the Ruby application, allows you to change the global Ruby for each user, and allows you to use an environment variable to override the Ruby version.

In this tutorial, you will use rbenv to install and configure Ruby on Rails on your local macOS machine.

Prerequisites

To follow this tutorial, you’ll need:

  • A computer or virtual machine with macOS installed, with administrative access to that machine and an Internet connection. This tutorial has been tested on macOS 12.4 Monterey.
  • Node.js installed on your macOS machine, as explained in How to install Node.js and Create a local development environment on macOS. Some Rails features, such as the asset pipeline, depend on a JavaScript runtime. Node.js provides this functionality.
  • The Homebrew package manager installed on your macOS machine.

Step 1 — Installing

rbenv

In this step, you will install rbenv and make sure that it starts automatically at boot. To do this on macOS, this tutorial will use the Homebrew package manager.

To download the rbenv package

with Homebrew, run the following command:

brew install rbenv This will install

  1. rbenv

and the [ruby-build] plugin (https://github.com/rbenv/ruby-build). This plugin adds the therbenv installation command, which streamlines the installation process for new versions of Ruby. This can install several other dependencies and take some time.

Next, you will add the eval “$(rbenv init -)” command to your ~/.bash_profile file so that rbenv loads automatically when you open Terminal. To do this, open

your .bash_profile in nano or your favorite text editor:

  1. nano

.bash_profile

Add the following line

to the file: eval “$(rbenv init -)”

Save and exit

the file.

Next, apply the changes made to your ~/.bash_profile file to your current shell session:

  1. source ~/.bash_profile

To verify that rbenv is configured correctly, use the type command, which will display more information about

the rbenv command: type rbenv

The terminal window will display the following

: Outputrbenv is an rbenv() function { local command; command=”${1:-}”; if [ “$#” -gt 0 ]; then shift; fi; case “$command” in rehash | shell) eval “$(

  1. rbenv

“sh-$command” “$@”)” ;; *) rbenv command “$command” “$@” ;; esac }

At this point, you have rbenv and ruby-build installed on your machine. This will allow you to install Ruby from the command line in the next step.

Step 2 — Installing

Ruby

With the ruby-build plugin now installed, you can install any version of Ruby you might need through a single command. In this step, you’ll choose a version of Ruby, install it on your machine, and then verify the installation.

First, use the -l flag to list the available versions of Ruby. Please note that recent and unlisted minor versions may also be available. For example, if the output of this command shows Ruby 3.0.5, you can probably install 3.0.4 as well.

  1. rbenv install -l

Output2.6.10 2.7.6 3.0.4 3.1.2 jruby-9.3.6.0 mruby-3.1.0 picoruby-3.0.0 rbx-5.0 truffleruby-22.1.0 truffleruby+graalvm-22.1.0

The result of that command will be a long list of versions that you can choose to install

. For this tutorial, we will install Ruby 3.1.2: rbenv install 3.1.2 Installing Ruby

can be a lengthy process, so be prepared for the installation to take some time to complete

.

Once it has finished installing, set it as your default version of Ruby with the global subcommand

:

  1. rbenv global 3.1.2

Verify that Ruby was installed correctly by checking its version number

:

ruby -v

Your output will look like this:

Outputruby

  1. 3.1.2p20
  2. (2022-04-12

revision 4491bb740a) [

  1. x86_64-darwin21

] To install and use a different version of Ruby, run the rbenv commands with a different version number, such as rbenv install 2.7.6 and

rbenv global 2.7.6

. You now have a version of Ruby

installed and have set your default version of Ruby. It will then be configured to work with Ruby packages and libraries, or gems, which will then allow you to install Rails.

Step 3 — Working with

Gems

Gems are packages of Ruby libraries and programs that can be distributed throughout the Ruby ecosystem. Use the gem command to manage these gems. In this step, you configure the gem command to prepare for the Rails installation.

When you install a gem, the installation process generates local documentation. This can add a significant amount of time to the installation process of each gem, so you can disable local documentation generation by creating a file called

~/.gemrc that contains a configuration option to disable this feature: echo “gem

  1. : -no-document” > ~/.gemrc

Once done, use the gem command to install Bundler, A tool that manages gem dependencies for projects. This is necessary for Rails to work

properly:

  1. Gem installation packager

You

will see output like this

: OutputFetching: bundler-2.3.18.gem Successfully installed bundler-2.3.18 1 gem installed

You can use the gem env command to learn more about the environment and configuration of gems. To view the location of the installed

gems, use the home argument, like this: gem

  1. env home

You will see output similar to the following

: /Users/sammy/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0

Now that you have set up and explored your gem workflow, you can

install Rails.

Step 4 — Installing

Rails To install Rails, use the gem install command:

  1. Gem installation rails

The gem command installs the specified gem as well as each dependency. Rails is a complex web development framework and has many dependencies, so the process will take some time to complete. You will eventually see a message stating that Rails is installed, along with its dependencies:

Output… Successfully installed lanes-7.0.3.1 35 installed gems

RBENV works by creating a directory of compatibility fixes or libraries that intercept calls and change or redirect them. In this case, the compatibility fixes point the Ruby commands to the files used by the version of Ruby that is currently enabled. Through the rehash subcommand, rbenv maintains compatibility fixes in that directory to match all Ruby commands in every installed version of Ruby on your server. Whenever you install a new version of Ruby or a gem that provides commands, such as Rails, you should use rehash.

To repeat the fix directory, run the following

command:

  1. rbenv rehash

Verify your Rails installation

by printing its version with this command

:

  1. rails -v

You will see the version of Rails that was installed:

OutputRails 7.0.3.1 With Rails

installed correctly, you can start testing your Ruby on Rails installation and start developing web applications. In the next step, you will learn how to update and uninstall rbenv and Ruby.

Step 5 — Updating and uninstalling rbenv and Ruby

When maintaining projects, it helps to know how to update and uninstall when the need arises. In this step, you will update rbenv, then uninstall Ruby and rbenv from your machine.

You can upgrade rbenv and ruby-build using Homebrew

by running the following command:

brew upgrade rbenv ruby-build If

  1. rbenv or ruby-build

needs to be updated, Homebrew will do it for you automatically. If your configuration is already up to date, you will get output similar

to the following: OutputWarning: rbenv 1.2.0 already installed Warning: ruby-build 20220713 already installed

This will ensure that we are using the most up-to-date version

of rbenv available.

As you download additional versions of Ruby, you may accumulate more versions than you would like in your ~/.rbenv/versions directory. Using the ruby-build plugin uninstall subcommand, you can remove these older versions.

for example run the following to uninstall

Ruby version 2.1.3: rbenv uninstall

  1. 2.1.3

With the rbenv uninstall command you can clean up older versions of Ruby so that you don’t have more installed than you are currently using

.

If you have decided that you no longer want to use rbenv, you can remove it from your system.

To do this, first

open your ~/.bash_profile file in your editor:

  1. nano ~/.bash_profile

Locate and delete the following line from the file to prevent rbenv from starting when you open Terminal:

… eval “$(rbenv init -)”

Once you have deleted this line, save the file and exit the editor.

Run the following command to

apply the changes to the shell

:

  1. source ~/.bash_profile

Next, remove rbenv

and all Ruby versions installed with this command

:

  1. rm -rf ‘root

rbenv’ Finally, remove the rbenv

package with Homebrew

: brew uninstall rbenv Check the version of

  1. rbenv

To make sure it has been uninstalled

: rbenv -v

You will get the following output

: Output-bash: /usr/local/bin/rbenv: No such file or directory

This means that you have successfully removed rbenv from your machine

.

Conclusion

In this tutorial you installed Ruby on Rails with rbenv on macOS. From here, you can learn more about coding in Ruby with our How to Code in Ruby series. You can also explore how to use Ruby on Rails with PostgreSQL instead of your default sqlite3 database, which provides more scalability, centralization, and stability for your applications.