Install Ruby on Rails with rbenv on Ubuntu 24.04

By Raman Kumar

Updated on Oct 01, 2024

In this tutorial, we'll explain how to install Ruby on Rails with rbenv on Ubuntu 24.04. 

Ruby is a dynamic, open-source programming language known for its simplicity and productivity. It features an elegant syntax that is easy to read and write, making it a popular choice for web development, automation, and scripting tasks. Ruby emphasizes flexibility and developer happiness, allowing programmers to write clean, maintainable code.

Ruby on Rails (often just "Rails") is a powerful web application framework written in Ruby. It follows the Model-View-Controller (MVC) architectural pattern and provides tools to easily create database-backed web applications. Rails promotes convention over configuration, streamlining development by minimizing the need for extensive setup and code repetition. It enables rapid prototyping and scaling of complex web applications.

Prerequisites:

Before we start, make sure you have:

  • A fresh install of Ubuntu 24.04 dedicated server or KVM VPS.
  • A non-root user with sudo privileges
  • Basic knowledge of the command line

Install Ruby on Rails with rbenv on Ubuntu

Step 1: Update Your System

First, it’s always a good practice to ensure that all your packages are up-to-date.

sudo apt update
sudo apt upgrade -y

This ensures you have the latest software and security updates installed.

Step 2: Install Dependencies

To install Ruby and Rails with rbenv, you'll need several dependencies. Install them using the following command:

sudo apt install -y git curl libssl-dev libreadline-dev zlib1g-dev \
build-essential libsqlite3-dev sqlite3 libpq-dev libyaml-dev \
libxml2-dev libxslt1-dev libffi-dev nodejs yarn

Here’s a brief overview of the key packages:

  • git: Required for cloning repositories.
  • curl: Used to download packages.
  • Development libraries (libssl-dev, libreadline-dev, etc.) are needed for compiling Ruby.
  • nodejs and yarn: Needed by Rails for handling JavaScript dependencies.

Step 3: Install rbenv and ruby-build

The next step is to install rbenv, which helps manage Ruby versions, and ruby-build, which provides an easy way to install Ruby versions.

3.1 Install rbenv

Clone the rbenv repository from GitHub and set up rbenv in your environment:

git clone https://github.com/rbenv/rbenv.git ~/.rbenv

Add rbenv to your system’s PATH and enable rbenv in your shell profile (~/.bashrc for Bash users or ~/.zshrc for Zsh users):

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

3.2 Install ruby-build as an rbenv plugin

Next, install ruby-build, a plugin that enables rbenv to install specific Ruby versions:

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

Now, restart your terminal or reload your shell:

source ~/.bashrc

Step 4: Install Ruby with rbenv

Now that rbenv is installed, you can install the latest version of Ruby. First, check which versions are available for installation:

rbenv install -l

To install a specific Ruby version (for example, version 3.3.5), use the following command:

rbenv install 3.3.5

After installation, set this version as the default global Ruby version:

rbenv global 3.3.5

To verify the installation, check the installed Ruby version:

ruby -v

Output:

ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [x86_64-linux]

Step 5: Install Bundler

Bundler is a gem manager used by Ruby on Rails to handle dependencies. You can install it by running:

gem install bundler

Make sure rbenv knows about the newly installed gem:

rbenv rehash

Step 6: Install Rails

Now that Ruby and Bundler are installed, it's time to install Ruby on Rails. You can do this via the following command:

gem install rails

This process might take a while, as it installs all the required dependencies. Once it’s finished, confirm the installation by checking the Rails version:

rails -v

Ouput:

Rails 7.2.1

Step 7: Set Up a New Rails Application

At this point, you’ve installed Ruby on Rails, and you can create a new Rails application. Navigate to your projects directory and create a new Rails project:

rails new myapp

This will generate a new Rails application in the myapp directory.

Step 8: Configure Database (Optional)

By default, Rails uses SQLite as its database, but you may want to use PostgreSQL, which is more commonly used in production environments.

To use PostgreSQL, first install the necessary package:

sudo apt install postgresql postgresql-contrib libpq-dev

Then, create a new Rails application specifying PostgreSQL as the database:

rails new myapp -d postgresql

Navigate to your new application directory:

cd myapp

Next, configure your PostgreSQL database settings in the config/database.yml file. Update the username, password, and host values as per your PostgreSQL setup.

Finally, create the database:

rails db:create

Step 9: Run the Rails Server

With your application set up, you can now run the Rails server. Execute the following command from the application directory:

rails server

By default, the Rails server will start at http://localhost:3000. Open this in your web browser to see your new Rails application in action.

Step 10: Managing Ruby Versions with rbenv

If you need to switch Ruby versions for a different project, use rbenv to install and switch versions as needed.

To install another Ruby version:

rbenv install <ruby-version>

To set a local Ruby version for a specific project directory:

rbenv local <ruby-version>

This will create a .ruby-version file in your project directory, ensuring that the correct Ruby version is used whenever you navigate to this directory.

Conclusion

We’ve successfully seen how to how to install Ruby on Rails with rbenv on Ubuntu 24.04. This tutorial walked you through installing rbenv, setting up Ruby, installing Rails, and configuring a new Rails application. You can now start building your own web applications using this powerful framework.

For further development, you may want to explore additional tools such as Redis, Sidekiq, or configuring deployment setups like Capistrano or Docker for Rails applications.