Support Online
Skip to main content

Installing Ruby on Rails with rbenv on Ubuntu 22.04 Server

Meta description (155 characters): Easily install Ruby and Ruby on Rails, manage versions, and quickly start your projects using rbenv on Ubuntu 22.04.

🧠 What Will You Learn in This Guide?

Ruby on Rails is one of the most popular frameworks for developing web applications. In this guide, we will install Ruby and Rails using the rbenv version manager on a server with Ubuntu 22.04. You will manage different Ruby versions, install the necessary dependencies, and get your Rails environment ready for development.


⚙️ Prerequisites

Before you start you need to have:

  • A server or virtual machine with Ubuntu 22.04 installed (example: tr1-app01.example.com)
  • User account with authority sudo
  • Installed Node.js (required for some features of Rails)

1️⃣ Installing rbenv and System Dependencies

In order for Ruby to be compiled, we need to install basic development tools.

Update Package List:

sudo apt update

Install Required Dependencies:

sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev

This command installs the necessary compilation tools for Ruby to run.

Install rbenv:

curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash

This command downloads and installs the rbenv and ruby-build plugin on your system.

Set PATH Variable:

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

Add rbenv to Autostart File:

echo 'eval "$(rbenv init -)"' >> ~/.bashrc

Enable Changes:

source ~/.bashrc

Verify Installation:

type rbenv

If you see the message “rbenv is a function”, the installation is successful.


2️⃣ Installing Ruby with rbenv

rbenv comes with the ruby-build plugin and allows you to easily install different Ruby versions.

List Available Ruby Versions:

rbenv install -l

Install Ruby 3.2.0:

rbenv install 3.2.0

It may take a few minutes as the installation is compiling.

Set Default Ruby Version:

rbenv global 3.2.0

Verify Installation:

ruby -v

Sample output: ruby 3.2.0 (2022-12-25 revision a528908271) [x86_64-linux]


3️⃣ Gem and Bundler Installation

Ruby libraries are distributed under the name Gem. Let's do a little configuration to optimize the installation.

Disable Documentation Generation:

echo "gem: --no-document" > ~/.gemrc

This setting reduces time by turning off local documentation generation during gem installation.

Install Bundler:

gem install bundler

Bundler manages the dependencies of your Ruby projects.

Check Installation Path:

gem env home

Sample output: /home/kullanici/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0


4️⃣ Ruby on Rails Installation

Now your Ruby environment is ready. Now we can install the Rails framework.

Install Rails 7.0.4:

gem install rails -v 7.0.4

Installation may take a few minutes because Rails contains many dependencies.

Update rbenv Command Cache:

rbenv rehash

Required for recognition of new gem commands (e.g. rails, bundle).

Verify Rails Installation:

rails -v

Output: Rails 7.0.4


5️⃣ rbenv and Ruby Management

Update rbenv:

cd ~/.rbenv
git pull

Keeping rbenv updated will allow you to install new Ruby versions without any problems.

Uninstall Old Ruby Version:

rbenv uninstall 3.1.0

You can safely remove obsolete Ruby versions.

To Completely Uninstall rbenv:

nano ~/.bashrc

Delete the following lines:

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

Finally:

rm -rf `rbenv root`

❓ Frequently Asked Questions (FAQ)

1. What is the advantage of using rbenv?

It allows you to manage different Ruby versions on a project-specific basis. It eliminates the risk of conflict.

2. What is the difference between rbenv and RVM?

rbenv is a lighter and simpler tool; It focuses only on version management.

3. What does “rbenv rehash” do?

When a new Ruby version or gem is installed, it updates the command shortcuts on the system.

4. What does “no documentation” do during gem install?

It reduces installation time by 30-40% by turning off local documentation generation.

5. How can I install a different version of Rails?

You can list the versions with gem search '^rails$' --all and install the version you want with the gem install rails -v X.X.X command.


🎯 Result

Now the Ruby on Rails development environment is completely ready on your Ubuntu 22.04 server. Thanks to rbenv, you can safely manage different Ruby versions and easily start your Rails projects.

💡 After developing your Ruby on Rails application, you can immediately test its performance by publishing it on the GenixNode platform!