How to Set Up Your First Ruby on Rails Project: Step-by-Step Tutorial

Ruby on Rails is a powerful web application framework written in Ruby. It simplifies the process of building web apps by providing conventions and tools that speed up development. If you’re new to Rails, setting up your first project can seem daunting. This step-by-step tutorial will guide you through the process from installation to running your first app.

Prerequisites

  • Basic knowledge of Ruby programming
  • Computer with internet access
  • Command line familiarity

Step 1: Install Ruby

Ruby on Rails requires Ruby to be installed on your system. You can install Ruby using a version manager like RVM or rbenv, or directly through your operating system’s package manager.

For example, on macOS with Homebrew:

brew install ruby

After installation, verify with:

ruby -v

Step 2: Install Rails

Once Ruby is installed, install Rails using the gem package manager:

gem install rails

Verify the installation:

rails -v

Step 3: Create a New Rails Project

Navigate to the directory where you want your project and run:

rails new my_first_app

This command creates a new folder named my_first_app with all the necessary files.

Step 4: Run the Rails Server

Change into your project directory:

cd my_first_app

Start the server:

rails server

Open your web browser and go to http://localhost:3000. You should see the Rails welcome page.

Step 5: Create Your First Controller and View

Generate a controller named Pages with an action home:

rails generate controller Pages home

This creates files for your controller and view. To see your page, add content to app/views/pages/home.html.erb.

Start the server again if it’s stopped, and navigate to http://localhost:3000/pages/home to view your page.

Conclusion

Congratulations! You have set up your first Ruby on Rails project and created a basic page. From here, you can explore adding models, views, controllers, and databases to build more complex applications.