Sep 15, 2020 5 min read

How to Install Ruby on Rails with rbenv on Ubuntu 20.04

Install Ruby on Rails with rbenv on Ubuntu 20.04 with our step-by-step tutorial. It is a framework known for its simplicity and productivity.

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

Choose a different version or distribution

Introduction

Before we begin talking about how to install Ruby on Rails on Ubuntu 20.04, let’s briefly understand – What is Ruby on Rails?

Ruby on Rails is a powerful web development framework known for its simplicity and productivity. It allows developers to build robust and scalable web applications quickly. Ruby on Rails follows the "convention over configuration" principle, providing ready-made solutions and reducing the need for repetitive coding.

With its vast library of gems and active community support, Ruby on Rails is a popular choice for creating dynamic and user-friendly websites. Its efficiency, versatility, and ease of use make it an excellent option for both beginners and experienced developers.

In this tutorial, you will install Ruby and Rails with the command-line tool rbenv on Ubuntu 20.04 server. rbenv lets you switch between different versions of Ruby and provides a solid environment to develop Ruby on Rails applications.

Advantages of Ruby on Rails

  1. Productivity: Ruby on Rails emphasizes convention over configuration, enabling developers to build applications quickly and efficiently.
  2. Simplicity: The framework's elegant syntax and clear structure make it easy to learn and use, boosting development speed.
  3. Scalability: Ruby on Rails offers scalability options, allowing applications to handle increasing traffic and user demands effectively.
  4. Vast Community: The active and supportive community provides extensive resources, libraries, and plugins, enhancing development capabilities.
  5. Time-Tested: With over 15 years of existence, Ruby on Rails has a mature ecosystem, ensuring stability and reliability for web development projects.

Prerequisites to Install Ruby on Rails with rbenv on Ubuntu 20.04

  • Ubuntu 20.04 server with a non-root user.
  • Installed Node.js using the official PPA with some Rails features, such as the Asset Pipeline.

Step 1 - Update and install dependencies

1) At first, you need to update apt-get to ensure that the local package cache is updated.

sudo apt-get update

2) After that, you need to install the dependencies required for rbenv and Ruby with apt-get.

sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev

Step 2 - Install rbenv

1) Now, you can install rbenv. Firstly you need to clone the rbenv repository from git. All these steps are to be completed from the same account from which you will run Ruby.

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

2) After that, add ~/.rbenv/bin to your $PATH to use rbenv’s command line utility. You can load the rbenv automatically by adding ~/.rbenv/bin/rbenv init to your ~/.basrc file.

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

3) Source rbenv and use the type command to get more information about rbenv.

source ~/.bashrc
type rbenv

The following output will be visible on 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
}

Now, you need to install ruby-build first to be able to use the rbenv install command. You will install it as a plugin using git.

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

After that, you have both rbenv and ruby-build in place. You can now start the process of installing Ruby.

Step 3 - Install Ruby

1) Now that you have installed ruby-build rbenv plugin, you can install whichever version of Ruby you want. First, list the versions available using the following command:

rbenv install -l

After that, you will get a long list of versions after using this command and you can choose any one of them.

It's important to note that Ruby installation is a lengthy process and it will take some time to get complete.

As an example here, you can install the Ruby version 2.5.0, as your default version with the global sub-command.

rbenv install 2.5.0
rbenv global 2.5.0

2) Verify that Ruby was installed properly by checking your version number.

ruby -v

As you have installed the 2.5.0 version of Ruby, you will get the following output.

Output

ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

Now, you have Ruby installed and you need to set up gems and Rails.

Step 4 - Working with Gems

Now, you will install Rails using the gems command which extends the functionality of Ruby.

1) Install the bundler gem to manage application dependencies.

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

2) After that, use the gem env command to learn more about the environment and configuration of gems. If you want to check the location of gems of your server use the home argument.

gem env home

The following output will be displayed after following the above steps.

/home/pwsuser/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0

Now you have configured gems, you can proceed with installing Rails.

Step 5 - Install Rails

1) Now, install the recent version of Rails with the following command.

gem install rails

If you want to install a specific version of Rails, firstly you need to list the available version of Rails. You can now install the specific version, using the following command.

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

rbenv creates a directory of shims, which points to the enabled files used by Ruby Version. rehash sub-command is used to maintain the shims in the directory to match every Ruby command with every installed version of Ruby on your server.

You have to run the following command, whenever you install the new version of Ruby.

2) Confirm if Rails has been installed by printing its version, with the following command.

rails -v

3) Now, test your Ruby on Rails installation and start developing web applications.

Step 6 - Updating rbenv

1) You can upgrade to the latest version of rbenv from the following command anytime, as you have done it manually using git.

cd ~/.rbenv
git pull

It will make sure that you are using the latest version available.

Step 7 - Uninstalling Ruby versions

1) Use the ruby-build plugin to automate the removal process of accumulated versions with the uninstall subcommand.

rbenv uninstall 2.1.3

This will clean up your installed versions of Ruby and will remove the versions that you are not using now.

FAQs to Install Ruby on Rails on Ubuntu 20.04

What are the system requirements for installing Ruby on Rails on Ubuntu 20.04?

You need Ubuntu 20.04 installed with a user account that has administrative privileges and a stable internet connection.

How can I check if Ruby is already installed on my Ubuntu 20.04 system?

Open a terminal and type ruby -v. If Ruby is installed, it will display the version number; otherwise, it will prompt you to install it.

What is the recommended way to install Ruby on Rails on Ubuntu 20.04?

After installing Ruby, run sudo gem install rails to install the latest version of Rails and its dependencies.

How can I verify if Rails is successfully installed?

Use the command rails -v in the terminal. It should display the installed version of Rails.

Are there any additional packages required for Rails installation on Ubuntu 20.04?

Yes, you need to install the Node.js JavaScript runtime environment by running sudo apt install nodejs in the terminal.

Can I use a different version of Ruby with Rails on Ubuntu 20.04?

Yes, you can manage multiple Ruby versions using tools like RVM or rbenv. It allows you to switch between versions as needed.

How can I access the Rails application in a web browser?

Open a web browser and enter http://localhost:3000 to access your Rails application running on the local server.

Conclusion

We hope this tutorial helped you successfully install rbenv and Ruby on Rails so that you can use multiple Ruby environments. To learn more about scalability, centralization, and control in your Ruby on Rails application, explore how to scale Ruby on Rails applications across multiple servers.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - 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.