Skip to content

Setup Ruby on Rails

This guide walks you through setting up a Ruby on Rails development environment from scratch. Ruby on Rails is a web application framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.

Install Bundler

  • Install the Bundler gem:
gem install bundler
  • Verify Bundler installation:
bundler -v

Bundler manages gem dependencies for your Ruby projects, ensuring that the gems you need are installed and loaded.

Install Database System

  • Install a database system (PostgreSQL recommended for production compatibility):
# macOS
brew install postgresql
brew services start postgresql

# Ubuntu/Debian
sudo apt install postgresql postgresql-contrib libpq-dev
sudo systemctl start postgresql

While Rails comes with SQLite by default, using PostgreSQL from the beginning ensures development/production parity and prevents migration issues later.

Install Node.js and Yarn

  • Install Node.js and Yarn for the asset pipeline:
# macOS
brew install node yarn

# Ubuntu/Debian
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
npm install -g yarn
  • Verify installations:
node -v
yarn -v

Rails uses JavaScript for features like Turbo, Stimulus, and the asset pipeline. Node.js and Yarn are required for managing JavaScript dependencies.

Install Rails

  • Install the Rails gem:
gem install rails
  • Verify Rails installation:
rails -v

This installs the latest stable version of Rails. If you need a specific version, use gem install rails -v 7.0.4 (replace with your desired version).

Create a New Rails Application

  • Generate a new Rails application with PostgreSQL:
rails new myapp --database=postgresql
cd myapp

The --database=postgresql flag configures the app to use PostgreSQL instead of the default SQLite. Use additional flags like --css=tailwind or --javascript=esbuild to customize the front-end stack.

Configure Database

  • Edit config/database.yml to set up your database connection:
database.yaml
1
2
3
4
5
6
7
8
# Update username and password if needed
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: postgres
password:
host: localhost
  • Create the database:
rails db:create

The database.yml file contains the configuration for connecting to your database in different environments (development, test, production).

Start the Rails Server

  • Launch the development server:
rails server
# Or the shorter version
rails s

The Rails server runs your application locally and automatically reloads when you make changes to your code.

Install Development Tools

  • Add useful gems to your development environment in the Gemfile:
group :development, :test do
gem 'pry-rails'        # Better console
gem 'rspec-rails'      # Testing framework
gem 'rubocop-rails'    # Code style checker
gem 'dotenv-rails'     # Environment variables
end
  • Install the gems:
bundle install

These development tools help improve your workflow with better debugging, testing, and code quality tools.

Verify Your Setup

  • Run Rails tests to confirm everything works:
rails test
# Or if using RSpec
rails generate rspec:install
rspec

A successful test run confirms that your development environment is properly configured and ready for development.