Install Ruby and Rails on a Mac
This is a step-by-step guide for installing Ruby and Rails on a MAC. Then it shows you how to create a first Rails project. Finally, it shows how to run the Rails project.
Installation steps
Step 1 — Install Homebrew (brew.sh)
Homebrew is a free open-source software package manager mainly used to easily help on the software installation on macOS and Linux operating systems.
To install the Homebrew, we need to open a terminal window and execute the following command.
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Note: For more information about Homebrew there is the official website: https://brew.sh/ .

Step 2 — Install chruby
Sometimes, when working with many Ruby projects, we may need to have different versions of Ruby (maybe because a specific component was developed only for a specific version of Ruby, or maybe we want to run the newest version, etc,). In order to manage the used version, there is a version manager called chruby.
To install the chruby, we need to open a terminal window and execute the following command.
brew install chruby
At the end of the installation add the following command to the terminal configuration file
a) If the terminal is bash, execute the following command.
sudo nano /etc/bashrc
And then, paste the line
source /usr/local/opt/chruby/share/chruby/chruby.sh
a) If you use zsh, execute the following command.
sudo nano ~/.zshrc
And then paste the line
source /usr/local/opt/chruby/share/chruby/chruby.sh
For these changes to take effect it is necessary to open a new terminal window.
Note: For more information on how to install chruby there is the Homebrew website with a dedicated page: https://formulae.brew.sh/formula/chruby

Step 3 — Install ruby-install
In a terminal window and execute the following command.
brew install ruby-install
At the end of the installation run the following command to verify that it has been installed.
ruby-install –version
Note: For more information about ruby-install, access the website https://www.ruby-lang.org/en/documentation/installation/

Step 4 — Run brew update
In a terminal window and execute the following command.
brew update
Step 5 — Install Openssl
In a terminal window and run the following command.
brew install openssl
Step 6 — Add the path to openssl
a) If you use bash, execute the following command in a terminal window, replacing USERNAME with the name of the system user.
echo ‘export PATH=”/usr/local/opt/openssl@1.1/bin:$PATH”’ >> /Users/USERNAME/.bash_profile
b) If you use zsh, execute the following command in a terminal window, replacing USERNAME with the name of the system user.
echo ‘export PATH=”/usr/local/opt/openssl@1.1/bin:$PATH”’ >> /Users/USERNAME/.zprofile
Note: For more information about installing openssl, you can access the site https://formulae.brew.sh/formula/openssl@1.1

Step 7 — Identify openssl for system compilers
In order to make the compilers be able to identify the openssl in the system, we will need to do the following configuration:
a) If you use bash, execute the following command in a terminal window to edit the .bash_profile file
nano /Users/USERNAME/.bash_profile
Add the following lines to the file
export LDFLAGS=”-L/usr/local/opt/openssl@1.1/lib”export CPPFLAGS=-I/usr/local/opt/openssl@1.1/include
b) If you use zsh, execute the following command in a terminal window to edit .zprofile file
nano /Users/USERNAME/.zprofile
Add the following lines to the file
export LDFLAGS=”-L/usr/local/opt/openssl@1.1/lib”export CPPFLAGS=-I/usr/local/opt/openssl@1.1/include
For these settings to take effect it is necessary to open a new terminal window.
Step 8 — Get the latest ruby version
In a terminal window, execute the following command.
ruby-install –latest
The most recent ruby version will be shown
Step 9 — Install the latest version of ruby
In this case, version 2.7.2 will be installed.
In a terminal window, execute the following command.
ruby-install ruby 2.7.2 — — with-openssl-dir=/usr/local/opt/openssl
For these settings to take effect it is necessary to open a new terminal window.
Step 10 — Switch ruby versions
In this step, the ruby versions switch to the latest version (in this case, 2.7.2)
a) If you use bash, execute the following command in a terminal window to edit the .bash_profile file
nano /Users/USERNAME/.bash_profile
Add the command to the file
chruby 2.7.2
b) If you use zsh, execute the following command in a terminal window to edit .zprofile file
nano /Users/USERNAME/.zprofile
Add the command to the file
chruby 2.7.2
Step 11 — Install GEM Rails
This GEM corresponds to the Ruby on Rails framework, a web framework (full-stack).
In a terminal window run the following command:
gem install rails
Note: To find out more information about GEM rails, we can access the official website: https://rubygems.org/gems/rails/

Step 12 — Install nvm
Since there are many versions of NodeJs and we may have the need to change between NodeJS versions (maybe because a component works on a version, but not on another, or we want to have the most recent version of NodeJs, etc.) there is a Node Version Management tool (nvm) that will make NodeJs version management more easy. In order to have nvm installed we need to open a terminal window and execute the following command.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
a) If you use bash, execute the following command in a terminal window, replacing USERNAME with the name of the system user.
nano /Users/USERNAME /.bash_profile
Add the following command to the file
. ~/.nvm/nvm.sh
b) If you use zsh, execute the following command in a terminal window, replacing USERNAME with the name of the system user.
nano /Users/USERNAME /.zprofile
Add the following command to the file
. ~/.nvm/nvm.sh
For these settings to take effect it is necessary to open a new terminal window.
Step 13 — Install NodeJs
In a terminal window, execute the following command.
nvm install node
Step 14 — Install Yarn
In a terminal window, execute the following command.
npm install -g yarn
How to create your first Ruby on Rails project?
In this example we will be using the nano text editor to write the code on the files. We could use other text editors or IDEs, like Emacs or Visual Studio Code.
Step 1 — Create the project
On a terminal window, we should open the folder where we want to create the project and execute the following command, taking in attention that project-name is the name we want to give to the project:
rails new project-name
The command will create a folder named project-name with the essential files to put a site to run.
Step 2 — Acces the project
Run the following command to enter the project folder
cd project-name
Step 3 — Run the project
Run the following command
rails s
This will start a process for running the site.
Step 4 — Open the project site on a browser
On a webbrowser window, access the following url: http://localhost:3000
It will show a web site similar to the following.

Usually, to have webpages using Ruby on rails we should know that they use the MVC pattern. Also, to access the webpage, we must create a route.
Step 5 — Create our first route
a) Create a controller
On a terminal window, inside the project folder, run the following command.
nano ./app/controllers/home_controller.rb
Put the following code on the file.
class HomeController < ApplicationControllerdef indexendend
b) Create a view
On a terminal window, inside the project folder, run the following command.
nano views/index.html.erb
Put the following code on the file.
<h1>Hello World</h1><hr /><p>It’s <%= Time.now.strftime(“%H:%M”) %> on <%= Time.now.strftime(“%d/%m/%Y”) %>. Lets start coding! </p>
c) Create a route
On a terminal window, inside the project folder, run the following command.
nano /config/routes.rb
Put the following code on the file.
Rails.application.routes.draw do# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.htmlroot ‘home#index’end
Step 6 — Refresh the webpage and check the update
Your page should be similar to the following one, a simple page with the current time and date, inviting you to code.

Now we can continue creating new controllers, views, routes, etc.
So, what have we learned?
These are just the first small steps to start on the Ruby on Rails world. I advise you do them and continue creating more controllers, views and routes.
I also share with you some useful useful websites:
Getting Started with Rails: https://guides.rubyonrails.org/getting_started.html
- QuickStart: https://www.ruby-lang.org/en/documentation/quickstart/
- Ruby on Rails official site: https://rubyonrails.org/
- Ruby on Rails (wikipedia): https://en.wikipedia.org/wiki/Ruby_on_Rails