GeekBits

How to Install Ruby on Rails on MacOS

In this tutorial, we will learn how to use the rbenv utility to install Ruby on Rails on a macOS machine.

3 min read
How to Install Ruby on Rails on MacOS

Ruby on Rails, commonly known as Rails, is a free and open-source web application framework that allows us to create web applications using the DRY principle.

Rails provides a model-view-controller framework by providing features such as database integration and migration, views, and models, which is critical in building full-stack web applications.

One of the best ways to have Ruby on Rails running on your machine using rbenv. Using rbenv provides a versatile and heavily customizable environment for developing Ruby on Rails applications. It also allows us to switch to different versions of the Rails framework as needed.

Requirements

To follow along with this tutorial, ensure you have the following:

  1. a device running macOS 13 and above.
  2. Administrative permissions to install tools and applications.
  3. Network connectivity.
  4. Homebrew package manager installed on your macOS machine.

Step 1 - Installing rbenv

The first step is to install the rbenv on our macOS system. If you are unfamiliar, rbenv is a version manager tool for the Ruby programming language on Unix-like systems.

To install rbenv on macOS, open the terminal and use the Homebrew command as shown:

brew install rbenv ruby-build

The command above will install your system's rbenv utility and the ruby-build plugin. The ruby-build plugin allows us to use the `rbenv installs command, which simplifies the installation of multiple versions of the Ruby interpreter.

The next step is to add the command eval "$(rbenv init - bash)" command in the .bashrc file. This ensures that rbenv will load automatically when we launch a new terminal instance.

Run the command:

nano .bashrc

Next, add the entry as shown:

eval "$(rbenv init - bash)"

Finally, save the file and close the editor.

You can use the echo command to append the entry above as shown:

echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bashrc

If you want to use the .bash_profile file instead, run the command:

echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bash_profile

For ZSH, run the command:

echo 'eval "$(~/.rbenv/bin/rbenv init - zsh)"' >> ~/.zshrc

Next, apply the changes by loading the .bashrc file as shown:

source .bashrc

To ensure that we have the rbenv utility configured in the system, run the command below:

type rbenv

Output:

rbenv is a shell function from /Users/csalem/.zshrc

Step 2 - Installing Ruby

Once we have the ruby-build plugin installed, we can install any version of the Ruby interpreter we wish to install.

Let us start by listing the available versions of Ruby that we can install using the ruby-build plugin. We can do this by using the -l parameter as shown:

rbenv install -l

Output:

3.0.6
3.1.4
3.2.2
jruby-9.4.3.0
mruby-3.2.0
picoruby-3.0.0
truffleruby-23.0.0
truffleruby+graalvm-23.0.0
...

This should return all the available Ruby versions, allowing you to choose which version you wish to install.

Once you have identified which version you wish to install, use the install command as shown below:

$ rbenv install 3.2.2

This should download and install Ruby version 3.2.2 as specified in the command above.

Once you have it installed, you can set the version as the default Ruby version using the global subcommand as shown:

rbenv global 3.2.2

Once installed, you can check the version as shown:

$ ruby -v

Output:

ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin23]

If you wish to install a different version of the Ruby interpreter, you can use the rbenv command followed by the version you wish to install.

Similarly, to set a given version as the default version, you can use the rbenv global command followed by the target version.

Installing Ruby Gems and Rails

Once we have everything set up, we can learn how to install Rails. Ruby provides tools and libraries using packages known as gems. We can use the gem command to manage these gems, including installing, updating, uninstalling, and more.

The first step is to install the Bundler gem. Bundler is a tool that provide a simple way to manage the installed gems and their dependencies with ease.

Run the gem command as shown to install Bunder:

sudo gem install bundler

Output:

Fetching bundler-2.4.19.gem
Successfully installed bundler-2.4.19
Parsing documentation for bundler-2.4.19
Installing ri documentation for bundler-2.4.19
Done installing documentation for bundler after 0 seconds
1 gem installed

This should download and install the Bundler gem on your machine.

Once complete, run the command below to install the Rails gem:

sudo gem install rails

Once completed, you should have the Ruby and Ruby on Rails framework installed on your machine.

Conclusion

In this tutorial, you learned how to install Ruby on Rails using rbenv on macOS. You can then learn how to create web applications using Ruby on Rails. You can also learn how to connect Ruby on Rails with a database such as PostgreSQL, MySQL, Redis, and more.

Stay tuned for more tutorials on Ruby on Rails.

Sign up for our newsletter

Don't miss anything. Get all the latest posts delivered to your inbox. No spam!