In this article, I will show you how to install Ruby on Rails on a Mac using rbenv and ruby-build.
Ruby on Rails is a framework for web application development, offering efficiency, flexibility, and a large ecosystem of libraries and gems (packages).
When managing different versions of Ruby and Rails, developers may need to switch between versions. This is where rbenv comes into play. It is a lightweight tool that simplifies the installation and management of Ruby on Rails versions on your system.
ruby-build is a command-line tool that enables developers to install and manage different versions of Ruby effortlessly. It works in conjunction with a Ruby version manager like rbenv, RVM, or chruby. While rbenv primarily handles the management of Ruby versions within a project, ruby-build takes care of the actual installation process.
Step 1. Install rbenv and ruby-build
If you don't have Homebrew (brew) installed, follow the instructions here:
Before installing rbenv and ruby-build, it's a good idea to update brew first.
Open up a terminal window and run the following command:
You can also run
brew upgrade to update other packages. But that's not necessary right now and it may take a while.
Install rbenv and ruby-build
Once brew is installed and updated, you can install rbenv and ruby-build on a Mac with this command.
brew install rbenv ruby-build
Next, you have to print out instructions for how to run rbenv when you open a new terminal window, etc. To get the instructions run this command:
Because I am on a Mac, running zsh, I got this:
# Load rbenv automatically by appending # the following to ~/.zshrc: eval "$(rbenv init - zsh)"
So I edited my ~/.zshrc file and added that line.
Advanced users can also run a command to append it without an editor. But I like to make sure I don't end up with two entries.
Step 2. Install ruby
Before installing ruby you need to determine which version you want. To see the latest stable versions available run this command:
rbenv install -l
To figure out from the list which version is the latest, run this command:
rbenv install -l | grep -v - | tail -1
In my case, that came back with 3.2.2.
So to install that version I would run this command:
rbenv install 3.2.2
You could also combine the commands like this:
rbenv install $(rbenv install -l | grep -v - | tail -1)
But by breaking out the commands you get to verify which version is being installed ahead of time.
Override the Apple version
If you run this command you will see that you may already have an Apple version of ruby installed. For example, on my machine, I get this:
ruby -v ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin22]
Earlier we installed a later version of ruby, but we still need to use rbenv to set it as the global default.
To do that, use this command (substitute the version for the one that you want to install):
rbenv global 3.2.2
Now if you run this command again, you should see the rbenv installed version:
Step 3. Install rails
Before installing rails, open up a new terminal window to do it first. Otherwise, the install commands below may lead to a sudo request (you don't want that).
To install rails, use the gem tool (ruby package manager), by running this command (in a new terminal window):
gem install rails
To verify the installation and the version, run this command:
Step 4. Create a demo app
To create a new rails app:
- Open up a new terminal window
- Run the following commands to create a new project folder:
mkdir -p ~/projects/rails/rails-demo cd ~/projects/rails/rails-demo
- In that project folder, run this command to create a new app:
rails new myapp
- Change to the new folder that was just created for the app:
- Start the server:
- Browse to: http://localhost:3000/
In this article, you learned how to:
- Install rbenv and ruby-build on a Mac using Homebrew
- Manage the installation of ruby versions on a Mac
- Install rails
- Create a sample Ruby on Rails app
- Run the app as a server and view the results in the browser