Skip
Arish's avatar

4. Installing Rails


Installing Ruby on Rails

Now that Ruby is installed, let's install Rails. Rails is distributed as a Ruby gem, making installation straightforward.

Install the Rails Gem

Open your terminal and run:

bash
1gem install rails

This installs the latest stable version of Rails along with all its dependencies.

Installing a Specific Version

If you need a specific Rails version:

bash
1# Install Rails 7.1
2gem install rails -v 7.1.0
3
4# Install the latest Rails 7.x
5gem install rails -v '~> 7.0'

Verify the Installation

Check that Rails is installed correctly:

bash
1rails -v
2# Output: Rails 7.1.3 (or your installed version)

Additional Dependencies

Rails needs a few more tools to work properly:

Node.js and Yarn

Rails uses Node.js for JavaScript processing and Yarn for package management:

bash
1# macOS (using Homebrew)
2brew install node yarn
3
4# Ubuntu/Debian
5curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
6sudo apt install -y nodejs
7npm install -g yarn
8
9# Verify installation
10node -v
11yarn -v

Database

Rails supports multiple databases. SQLite comes built-in for development:

bash
1# SQLite is usually pre-installed on macOS
2sqlite3 --version
3
4# On Ubuntu/Debian
5sudo apt install sqlite3 libsqlite3-dev

For production, you'll likely want PostgreSQL:

bash
1# macOS
2brew install postgresql
3brew services start postgresql
4
5# Ubuntu/Debian
6sudo apt install postgresql postgresql-contrib libpq-dev
7sudo service postgresql start

Create Your First Rails App

Let's test the installation by creating a new Rails application:

bash
1# Create a new Rails app
2rails new my_first_app
3
4# Navigate into the app directory
5cd my_first_app
6
7# Start the development server
8rails server

Now open your browser and visit http://localhost:3000. You should see the Rails welcome page!

Understanding What Rails Generated

When you run rails new, it creates a complete application structure:

my_first_app/ ├── app/ # Your application code │ ├── controllers/ # Handle HTTP requests │ ├── models/ # Database models │ ├── views/ # HTML templates │ └── ... ├── config/ # Configuration files │ ├── routes.rb # URL routing │ └── database.yml # Database settings ├── db/ # Database files │ ├── migrate/ # Database migrations │ └── seeds.rb # Seed data ├── Gemfile # Gem dependencies └── ...

Rails New Options

Customize your app when creating it:

bash
1# Use PostgreSQL instead of SQLite
2rails new myapp --database=postgresql
3
4# Skip Git initialization
5rails new myapp --skip-git
6
7# Use Tailwind CSS
8rails new myapp --css=tailwind
9
10# Skip Action Mailer
11rails new myapp --skip-action-mailer
12
13# API-only application (no views)
14rails new myapp --api
15
16# See all options
17rails new --help

Quick Development Tips

Start the Server

bash
1rails server
2# or shorthand
3rails s

Rails Console (Interactive Ruby with your app loaded)

bash
1rails console
2# or
3rails c

Database Commands

bash
1# Create database
2rails db:create
3
4# Run migrations
5rails db:migrate
6
7# Seed the database
8rails db:seed
9
10# Reset database (drop, create, migrate, seed)
11rails db:reset

You now have Rails installed and ready to build amazing web applications!