Ruby 3 on Rails 7: RESTful JSON API

Part 1: Installing & Setting Up

Charlie Campbell
3 min readJan 20, 2022

By the end of this three-part mini blog series, you will be able to create a Ruby on Rails API with basic CRUD operations. This will run in a Docker container. We will also be unit testing the code using rspec and making sure it conforms to Ruby’s style guide by using rubocop. Please note that the portion of this guide to do with setup is specifically for macOS.

In this part of the guide we will:

  • Install Homebrew
  • Install Ruby and Rails
  • Install PostgreSQL
  • Setup a Ruby on Rails application
  • Install and setup insomnia against localhost

This part of the guide will cover the installation of Ruby. Setting up our first Rails application and getting it running on ‘localhost’.

Prerequisites

  • Basic CLI knowledge
  • Basic GIT usage

Installing Homebrew

This section covers how to install Homebrew. If you already have Homebrew installed you can skip this step. Homebrew is a package manager for macOS.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Installing Ruby

This section covers how to install ruby via ‘rbenv’. If you have already installed Ruby you can skip this.

Now that we have Homebrew installed we can use it to install rbenv. Rbenv is a ruby version manager that allows us to run multiple versions of Ruby side-by-side.

Install rbenv using the following in your terminal.

brew install rbenv

Verify that rbenv has been installed by running the following. It should return something along the lines of rbenv 1.2.0.

rbenv -v

Now that rbenv is installed we can check which versions of ruby are available to us.

rbenv install -l

Let's install the latest version of ruby. At the time of writing, that's 3.1.0.

rbenv install 3.1.0

Add the following to either your zshrc or bashrc. When you’re ready to save hit control + x and save.

nano ~/.zshrc
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

Let's set our global ruby version.

rbenv global 3.1.0

Let's verify that rbenv has set our ruby version.

ruby -v

Lastly, we need to install the database we’re going to use. In this case PostgreSQL.

brew install postgresql

Setting Up Rails

Now that we’ve set up the version of ruby that we want to use we need to set up rails.

First, we need to install ‘bundler’. This allows us to manage our Ruby applications dependencies.

gem install bundler

We also need rails.

gem install rails

Verify that rails has been installed by running.

rails -v

Now it’s time to create our new rails app. As we want an API and don’t want to make use of Minitest in favor of rspec we need to pass a few flags.

  • -T will create the project without minitest.
  • -d postgresql specifies that we would like to use a Postgres database.
  • --api creates the project with a preconfigured smaller stack for API-only apps.
ls new projects -d postgresql -T --api

Start your rails server by running:

rails server

Head to http://localhost:3000/ to see the application running!

Setting Up Insomnia

Insomnia is an open-source API client. We will be using it to test our application. It allows us to quickly and easily send REST requests to our server.

Installation: https://insomnia.rest/

Next Part: …coming very soon

--

--