Coresumo

How to install Ruby on Rails on mac

We will be setting up a Ruby on Rails development environment on macOS 11.

Older versions of OS are mostly compatible for followed along as far as you can and then Google search for any problems you run into. There are plenty of people who have documented solutions for them.

Introductory

In this article we will see installation of Ruby on Rails for newer versions of mac and older versions of mac.

How  to install Ruby on Rails on mac

This is the easiest method to install the ruby on rails on macOS. We are using homebrew for compilation. Install Ruby and configured the git also and then we install the Rails and then we installed the database. These database are MySQL for those people which having some experience in this. And for the new users we use the POSTGRESQL .

for both the new versions of macOS and the older version of macOS.

Let’s Start

Installing Homebrew

To install Ruby on macOS we need to install Homebrew. Homebrew is used to compile and install the software packages. Homebrew has some easy scripts to install. In this process it said to install the Xcode or command-line tools then says yes.

Open terminal and write the following commands:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing Ruby

Now in our machine ‘homebrew’ had installed and now lets us start the further process. In this phase, we install Ruby in our system. We are going to install the rbenv to manage the ruby version.

Rbenv:-  It is a tool which is used to install the ruby or control  the multiple versions in our system side by side. It is simple to understand  and lightweight to install. The best alternative to the RVM is this. This manages multiple versions of our ruby .

After the rbenv install we have to select the Ruby version to install.

For installation, run the following commands in your terminal.

brew install rbenv ruby-build

# Add rbenv to bash so that it loads every time you open a terminal
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.zshrc
source ~/.zshrc

# Install Ruby
rbenv install 3.0.2
rbenv global 3.0.2
ruby -v

Configuring git

Now we are going to use the version control system for this process. We have to login our github account details.

In this step we have to login or configure the github account to make sure you have a github account.

For configurg the git you are using the following commands:-

In the next step, we have to generate the ssh key and it to your github account.

git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t rsa -C "YOUR@EMAIL.com"

Ssh key:-  An SSH key is an access credential for the SSH (secure shell) network protocol. It is used for remote communication between machines and the unsecured open network.

cat ~/.ssh/id_rsa.pub

It is also used to transfer network management and also operating systems.

You have to paste your ssh key at here on your github account.

ssh -T git@github.com

If you have done all the above steps correctly, then you also check if the ssh is worked or not.

If the ssh key is not working then you have to check the all git configuring commands. After that, you have to checked the ssh key is working or not type the following command.

Hi excid3! You've successfully authenticated, but GitHub does not provide shell access.

You are doing all things corrected then you should get this message.

What is Rails?

Rails is an application framework written on ruby .Rails is a model-view-controller framework.

Installing Rails

Installing rails is very easy. You must follow all the command correctly these are showing below :-


After running the above command Rails is now install, for using the rails we have to tell rbenv to check the rails

gem install rails -v 6.1.4.1

After running the command it is verified Rails is installed in our machine.

rails -v
# Rails 6.1.4.1

Setting up a Database

We are going to install sqlite3 from homebrew. We couldn’t use the built-in function of macOS  because this built-in function is having some trouble.

brew install sqlite3

Here we have two options first for these people who have some experience in mySQL . Second, for beginners those have no experience in database those people use the POSTGRESQL. It is simple to understand.

If you are new to the Rails on Ruby database, then I am strongly suggesting setting up on the POSTGRESQL.

If you are from the PHP background then you will be familiar with mySQL.

MySQL

Now we are going to install the mySQL server and client from homebrew.

brew install mysql

Once the following command is finished, then you have also to wait some minutes /seconds depends on your system. After, you have to run the following commands.

brew services start mysql

By default, mySQL username is “root” having no password.

After that you also skip to the final step.

PostgreSQL

If you are new user on the mySQL then you also have to install PostgreSQL

brew install postgresql

This is highly recommend for new users.

Now we have to install PostgreSQL and client server from the homebrew. Use the following commands.

brew services start postgresql

After this command, Also follow the following command.

By default, the username of PostgreSQL is the same as your macOS username having no password.

Final Steps

Mojave changed the location of header files necessary for compiling C extensions. You need to run the following command to install pg, nokogiri, or other gems that require C extensions:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

All set guys , in these steps we have to create our Rails application.

rails new myapp

#### If you want to use MySQL
rails new myapp -d mysql

#### If you want to use Postgres
# Note you will need to change config/database.yml's username to be
# the same as your OSX user account. (for example, mine is 'chris')
rails new myapp -d postgresql

# Move into the application directory
cd myapp

# If you setup MySQL or Postgres with a username/password, modify the
# config/database.yml file to contain the username/password that you specified

# Create the database
rake db:create

rails server

You can now visit http://localhost:3000 to view your new website!

Now your machine is ready to make some Rails applications.

If you received an error that said Access denied for user ‘root’@’localhost’ (using password: NO) then you need to updated your config/database.yml file to match the database username and password.

Related Articles: How to install Ruby on Rails on mac.

Leave a Comment

Your email address will not be published. Required fields are marked *