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
- Verify Bundler installation:
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):
1
2
3
4
5
6
7
8
| # macOS
brew install postgresql
brew services start postgresql
# Fedora/RHEL
sudo dnf install postgresql postgresql-server libpq-devel
sudo postgresql-setup --initdb
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:
1
2
3
4
5
6
| # macOS
brew install node yarn
# Fedora/RHEL
sudo dnf install nodejs
npm install -g yarn
|
Rails uses JavaScript for features like Turbo, Stimulus, and the asset pipeline. Node.js and Yarn are required for managing JavaScript dependencies.
Install Rails
- Verify Rails installation:
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:
1
2
| 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.
- Edit
config/database.yml to set up your database connection:
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
|
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:
1
2
3
| 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.
- Add useful gems to your development environment in the Gemfile:
1
2
3
4
5
6
| 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
|
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:
1
2
3
4
| 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.