Jul 3, 2023 7 min read

How to Install Ruby on Rails with rbenv on Ubuntu 22.04

Install Ruby on Rails with rbenv on Ubuntu 22.04 with our step-by-step tutorial. It's a powerful and popular web development framework.

Install Ruby on Rails with rbenv on Ubuntu 22.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Ruby on Rails with rbenv on Ubuntu 22.04, let's briefly understand – What is Ruby on Rails?

Ruby on Rails, commonly known as Rails, is a powerful and popular web development framework. It provides a streamlined and efficient way to build dynamic websites and web applications. Developed using the Ruby programming language, Rails emphasizes simplicity, convention over configuration, and rapid development.

It offers a wide range of built-in tools and libraries, enabling developers to focus on coding rather than repetitive tasks. With its elegant syntax and extensive community support, Ruby on Rails is an excellent choice for creating robust and scalable web solutions.

In this tutorial, you will install Ruby on Rails on Ubuntu 22.04 using rbenv. We will also address a few FAQs on how to install Ruby on Rails with rbenv on Ubuntu 22.04.

Advantages of Ruby on Rails

  1. Productivity Boost: Ruby on Rails emphasizes convention over configuration, allowing developers to write less code and achieve more.
  2. Rapid Development: Rails' extensive libraries and built-in tools enable developers to build web applications quickly and efficiently.
  3. Scalability: Rails' modular design and performance optimizations make it scalable for handling growing user demands.
  4. Community Support: Ruby on Rails has a large and active community that provides support, resources, and continuous updates.
  5. Testing and Debugging: Rails has an integrated testing framework that simplifies the process of testing and debugging web applications.

Prerequisites to Install Ruby on Rails with rbenv on Ubuntu 22.04

To finish this tutorial, you'll need the following:

  • One Ubuntu 22.04 server set up using the initial server setup instructions for Ubuntu 22.04, with a sudo non-root user and a firewall.
  • Option 2 of How to Install Node.js on Ubuntu 22.04 describes how to install Node.js using the official PPA. A JavaScript Runtime is required by a few Rails functionalities, like the Asset Pipeline. This capability is offered by Node.js.

Step 1 – Install rbenv and Dependencies

Many packages that you can install using your package manager are necessary for Ruby to function. You can use rbenv to install Ruby after they have been set up.

Update your package list first:

sudo apt update

Next, set up the prerequisites for Ruby installation:

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

You can install rbenv itself after installing the requirements. To run the installer, pipe the install script directly into bash using curl after retrieving it from GitHub:

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

Then, so that you can use the rbenv command line tool, add ~/.rbenv/bin to your $PATH. To do this, change your ~/.bashrc file so that it affects future login sessions:

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

Then, add the eval "$(rbenv init -)" to your ~/.bashrc file to make rbenv automatically load:

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

Next, update your current shell session with the modifications you made to your ~/.bashrc file:

source ~/.bashrc

Use the type command to see more details about the rbenv command and ensure that it is configured properly.

type rbenv

The following will appear in your terminal window:

Output
rbenv is a function
rbenv ()
{
    local command;
    command="${1:-}";
    if [ "$#" -gt 0 ]; then
        shift;
    fi;
    case "$command" in
        rehash | shell)
            eval "$(rbenv "sh-$command" "$@")"
        ;;
        *)
            command rbenv "$command" "$@"
        ;;
    esac
}

Both rbenv and ruby-build are currently installed. You'll install Ruby next.

Step 2 – Installing Ruby with ruby-build

You may now use a single command to install any version of Ruby that you might require after installing the ruby-build plugin. Initially, a list of Ruby's current versions is provided:

rbenv install -l

The output of that command displays a list of every version you can install:

Output
2.7.7
3.0.5
3.1.3
3.2.0
jruby-9.4.0.0
mruby-3.1.0
picoruby-3.0.0
truffleruby-22.3.1
truffleruby+graalvm-22.3.1

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all / -L' to show all local versions.

Then, install Ruby 3.2.0:

rbenv install 3.2.0

Be aware that installing Ruby can be a time-consuming procedure, so allow enough time for the installation to finish.

Once it has been installed, use the global sub-command to make it the default version of Ruby:

rbenv global 3.2.0

Check Ruby's version number to ensure that it is installed correctly:

ruby -v

If Ruby 3.2.0 was installed, this command's output would look like this:

Output
ruby 3.2.0 (2022-12-25 revision a528908271) [x86_64-linux]

You've now installed at least one Ruby version and chosen your default Ruby version. You will then install Rails and gems.

Step 3 – Working with Gems

Ruby libraries are delivered through gems. To manage these gems, use the gem command, and to install Rails, use the same command.

Local documentation is created during the installation process of a gem. Turn off local documentation generation by making a file named ~/.gemrc that has a configuration setting to disable this feature because it can significantly lengthen the installation process for each gem:

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

Projects can use Bundler to manage their gem dependencies. Next, since Rails depends on the Bundler gem, install it:

gem install bundler

You will get the following output:

Output
Fetching bundler-2.4.5.gem
Successfully installed bundler-2.4.5
1 gem installed

To find out more about the setup and environment of gems, use the gem env command (the subcommand env is short for environment). With the home argument, you can determine where gems are being installed, like in:

gem env home

You'll get an output that looks like this:

Output
/home/sammy/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0

After setting up gems, you may install Rails.

Step 4 – Installing Rails

Use the gem install command and the -v argument to indicate the version when installing Rails. You need to utilize version 7.0.4 for this tutorial:

gem install rails -v 7.0.4

The gem command installs the gem you specify, along with any dependencies it might have. Due to the complexity of Rails and its numerous dependencies, the procedure will take some time to complete. Once Rails and its dependencies are installed, you will eventually get the following message:

Output
...
Successfully installed rails-7.0.4
35 gems installed

Note: If you want to install a different version of Rails, you can find a list of the current supported releases by performing a search, which will provide a list of potential releases. Then you can install a certain version, like 4.2.7:

gem search '^rails$' --all
gem install rails -v 4.2.7

Use the command without specifying a version if you want to install the most recent version of Rails:

gem install rails

In order for rbenv to function, a directory of shims that point to the files used by the active Ruby version must be created. Rerbenv keeps shims in that directory to match every Ruby command across every installed version of Ruby on your server with the rehash sub-command. Run the following commands each time you install a new version of Ruby or a gem that offers similar commands to Rails:

rbenv rehash

Print the version of Rails to confirm that it has been installed correctly using the command:

rails -v

The following command will reveal the Rails version installed, if it was installed correctly:

Output
Rails 7.0.4

Now that Ruby on Rails is installed, you can start creating web applications and test them. Let's now go over how to update the rbenv.

Step 5 – Updating rbenv

If you manually installed rbenv using Git, you can always update it to the most recent version by using the git pull command in the directory ~/.rbenv:

cd ~/.rbenv
git pull

By doing this, you can be sure that you are utilizing the most recent version of rbenv.

Step 6 – Uninstalling Ruby versions

You can end up with more versions of Ruby in your ~/.rbenv/versions directory than you would like as you download more of the language. To delete these earlier iterations, use the uninstall subcommand of the ruby-build plugin.

The command line interface for uninstalling Ruby 3.2.0 is as follows:

rbenv uninstall 3.2.0

You can remove outdated Ruby versions so that you don't have more installed than you need by using the rbenv uninstall command.

Step 7 – Uninstalling rbenv

You can uninstall rbenv from your system if you decide you no longer wish to use it.

Open your ~/.bashrc file in your favourite text editor to perform this. In this example, we'll use nano:

nano ~/.bashrc

Look for and delete the next two lines from the file:

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

Save the file after deleting these lines, then quit the editor. If you used nano, use CTRL + X, followed by Y and ENTER to stop using it.

Then, use the command below to uninstall rbenv and all installed Ruby versions:

rm -rf `rbenv root`

To make the changes to your shell effective, log out and back in.

FAQs to Install Ruby on Rails with rbenv on Ubuntu 22.04

How do I set a default Ruby version with rbenv?

To set a default Ruby version, use the rbenv global command followed by the desired Ruby version. For instance, rbenv global 2.7.4 will set Ruby 2.7.4 as the default version.

How do I verify the installed Rails version?

To verify the installed Rails version, use the command rails -v in your terminal. It will display the currently installed Rails version.

How do I create a new Rails application?

To create a new Rails application, run rails new <app_name> in your terminal. Replace <app_name> with the desired name for your application.

How do I start the Rails server?

Change to your Rails application's directory in the terminal and run rails server or rails s to start the Rails server. You can then access your application in a web browser.

How do I access my Rails application in a web browser?

Once the Rails server is running, you can access your application by entering "http://localhost:3000" in a web browser. This will open your Rails application locally on your Ubuntu 22.04 system.

What is rbenv?

rbenv is a lightweight Ruby version management tool that allows you to easily install and switch between different versions of Ruby on your Ubuntu 22.04 system.

Conclusion

Hope this detailed tutorial helped you understand how to install Ruby on Rails with rbenv on Ubuntu 22.04.

If you have any suggestions or queries, kindly leave them in the comments section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.