Set up Ruby on Rails with rbenv on Ubuntu 22.04

This tutorial intends to teach you to Set up or Install Ruby on Rails with rbenv on Ubuntu 22.04.

Ruby on Rails is open-source software that is used to build a web app. Rails is basically a server-side web app development framework written in Ruby programming language. It can be simply defined as a packaged library called RubyGem.

Ruby on Rails application is a library that has ready solutions for tasks that are considered to be repetitive. They can be creating tables, forms, or even menus on the website.

Rails also combine with other languages such as JavaScript, HTML, and CSS. It is considered to be a back-end or server-side web application development platform.

You can follow the steps below to Install Ruby on Rails with rbenv on Ubuntu 22.04.

rbenv is a command-line tool that allows you to easily install, manage, and work with multiple Ruby environments.

Steps To Set up Ruby on Rails with rbenv on Ubuntu 22.04

To complete this guide, you must log in to your server as a root or non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

Also, you must install Node.js on your server. For this purpose, you can use the Nodejs installation with PPA Repo by visiting this guide on How To Install Node.js on Ubuntu 22.04.

Install rbenv on Ubuntu 22.04

First, you need to run the system update by using the command below:

sudo apt update

Then, use the following command to install the required packages and dependencies for Ruby on Rails:

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 -y

Download and Install rbenv

At this point, you can use the following curl command to download the rbenv installer script from GitHub and pipe it directly to bash to run the installer:

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

Then, you need to add ~/.rbenv/bin to your $PATH. To do this, you can use the following command:

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

To load the rbenv automatically, you need to run the following command:

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

Next, source the bashrc to apply the changes:

source ~/.bashrc

Verify that your rbenv tool is working correctly by using the command below:

type rbenv
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
}

Set up Ruby with ruby-build on Ubuntu 22.04

At this point, you can install any version of Ruby that you want. First, list the available versions of Ruby by using the command below:

rbenv install -l
Output
2.7.7
3.0.5
3.1.3
3.2.1

jruby-9.4.1.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, use the following command to install Ruby 3.2.1 on Ubuntu 22.04:

rbenv install 3.2.1

This will take some time to complete.

When you are done, set it as your default version of Ruby by using the command below:

rbenv global 3.2.1

Verify your Ruby installation by checking its version:

ruby -v
Output
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux]

Set up Rails on Ubuntu 22.04

Gems are the way Ruby libraries are distributed. You can use the gem command to manage these gems and use this command to install Rails. Also, you need to install the bundler which is a tool that manages gem dependencies for projects. 

To do these, run the following commands:

# echo "gem: --no-document" > ~/.gemrc
# gem install bundler
Output
Successfully installed bundler-2.4.7
1 gem installed

Verify where gems are being installed by using the command below:

gem env home
Output
/root/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0

Now you can use the command below to install the latest Rails on Ubuntu 22.04:

gem install rails
Output
Successfully installed rails-7.0.4.2

Note: If you would like to install a specific version of rails, you can list the version and install it by using the commands below:

# gem search '^rails$' --all
# gem install rails -v 7.0.4

Whenever you install a new version of Ruby or a gem that provides commands as Rails does, you should run the following command:

rbenv rehash

Verify your Rails installation by checking its version:

rails -v
Output
Rails 7.0.4.2

Update rbenv on Ubuntu 22.04

You can easly update your rbenv tool by using the git pull command. Switch to your ~/.rbenv directory and run the following comamnd:

# cd ~/.rbenv
# git pull

Uninstall Ruby Versions on Ubuntu 22.04

If you have downloaded additional versions of Ruby, you can easily use the command below to uninstall the specific version. For example:

rbenv uninstall 3.2.1

Uninstall rbenv on Ubuntu 22.04

If you dont want to use rbenv tool anymore, you can follow the steps below to remove it.

Firt, open your ~/.bashrc file with your favorite text editor, here we use vi editor:

Find and remove the following two lines from the file:

~/.bashrc

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

When you are done, save and close the file.

Then remove rbenv and all installed Ruby versions with the following command:

 rm -rf `rbenv root`

For more information, you can visit Ruby on Rails Guides.

Conclusion

At this point, you have learned to Install Ruby on Rails with rbenv on Ubuntu 22.04.

Hope you enjoy it. You may be like these articles:

Install and Use Webmin on Ubuntu 22.04

Install and Configure phpMyAdmin on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!