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:
- 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):
# 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:
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:
- 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:
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 | |
---|---|
- Create the database:
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:
- Access your application at: http://localhost:3000
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:
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:
A successful test run confirms that your development environment is properly configured and ready for development.