Ruby on Rails

How to Install Ruby on Rails on Ubuntu 24.04

Welcome to the Greenhost.Cloud blog! Today, we’re diving into an exciting topic – setting up Ruby on Rails on Ubuntu 24.04. Whether you’re a veteran developer or just starting your coding journey, Rails provides a robust framework for building applications quickly and efficiently.

Prerequisites

Before installing Ruby on Rails, ensure you have the following:

  • A working instance of Ubuntu 24.04.
  • Root or sudo privileges on your system.
  • Basic knowledge of using the terminal.

Let’s get started with the installation!

Step 1: Update Your System

First and foremost, it’s a good practice to update your system’s package index and upgrade all your installed packages to the latest versions.

sudo apt update
sudo apt upgrade -y

Step 2: Install Required Dependencies

Rails requires several packages to function correctly, including Git, Node.js, and a database system (like PostgreSQL). We’ll also install some essential libraries.

sudo apt install -y curl gnupg build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev zlib1g-dev libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common

Additionally, we need to install Node.js, which Rails uses for managing JavaScript assets:

curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs

You can verify the installation with:

node -v

Step 3: Install RVM (Ruby Version Manager)

RVM helps manage Ruby versions and gems easily. Let’s install RVM and then use it to install Ruby.

sudo apt install -y software-properties-common
curl -sSL https://get.rvm.io | bash -s stable

After installation, close and reopen your terminal or run the following command to load RVM into the shell session:

source ~/.rvm/scripts/rvm

Now, install the latest stable version of Ruby:

rvm install ruby --latest

Set this version as the default:

rvm use ruby --default

You can confirm your Ruby installation with:

ruby -v

Step 4: Install Rails

With Ruby successfully installed, you can now install Rails. As of October 2023, the latest version is Rails 7.x. To install it, simply run the following command:

gem install rails -v 7.x

Replace 7.x with the specific version you want if necessary. Verify your Rails installation:

rails -v

Step 5: Install a Database System

For this tutorial, we’ll use PostgreSQL. Install PostgreSQL and its development package:

sudo apt install -y postgresql postgresql-contrib libpq-dev

After installation, you can create a new PostgreSQL user for your Rails application:

sudo -u postgres createuser --interactive

Follow the prompts to set a username and role. You may also want to create a new database for your Rails app:

sudo -u postgres createdb your_app_name_development

Step 6: Set Up Your Rails Application

Now that everything is installed, you can create a new Rails project. Navigate to your desired directory and run:

rails new your_app_name -d postgresql

Replace your_app_name with your preferred application name.

Change into the new directory:

cd your_app_name

Update your config/database.yml file with your PostgreSQL credentials:

development:
  <<: *default
  database: your_app_name_development
  username: your_username
  password:

Step 7: Run Database Migrations

Run the database migrations to set up your database:

rails db:create
rails db:migrate

Step 8: Start the Rails Server

You’re almost ready to go! Start the Rails server with:

rails server

Open your web browser and navigate to http://localhost:3000. If everything is set up correctly, you should see the Rails welcome page!

Conclusion

Congratulations! You’ve just installed Ruby on Rails on your Ubuntu 24.04 system and set up your first application. With its rich ecosystem, Rails allows you to focus on what’s essential—building great applications.

Happy coding, and welcome to the world of Ruby on Rails!


For more tips on web development and cloud hosting, stay tuned to the Greenhost.Cloud blog!