How to install Ruby on Ubuntu 20.04

This article was first published in: https://www.itcoder.tech/posts/how-to-install-ruby-on-ubuntu-20-04/

Ruby is one of the most popular languages today. It has a concise grammar and focuses on simplicity and productivity. Ruby is the language behind the powerful Ruby on Rails framework.

In this guide, we will show you three different ways to install Ruby on Ubuntu 20.04.

Choose the installation method that best suits your environment. If you are developing Ruby applications and need multiple Ruby environments, then your preferred way to install Ruby is Rbenv or RVM.

If you are not sure which version of Ruby you need to install, refer to the documentation of the application you are about to deploy.

1. Install Ruby from Ubuntu source repository

The easiest way to install Ruby on Ubuntu is to use the apt package management tool. At the time of writing this article, the version of Ruby in the Ubuntu source repository is 2.7.0, which is the latest stable version.

Installation is very simple and straightforward. Run the following command as root or another user with sudo privileges to upgrade the package index and install Ruby:

sudo apt update
sudo apt install ruby-full

Once completed, verify the installation result by printing the Ruby version number:

ruby --version

The output will look like this:

ruby 2.7.0p0(2019-12-25 revision 647ee6f091)[x86_64-linux-gnu]

Your Ruby version may be different from the one shown above.

that's it. You have successfully installed Ruby on your Ubuntu machine and you can start using it.

Two, use Rbenv to install Ruby

Rbenv is a lightweight command line tool that allows you to easily switch Ruby versions.

By default, rbenv will not install Ruby. We will use ruby-build to install Ruby. It can be a standalone program or a plug-in for rbenv.

This ruby-build script installs Ruby from source. To build Ruby, install the necessary software libraries and compilers:

sudo apt update
sudo apt install git curl autoconf bison build-essential \
 libssl-dev libyaml-dev libreadline6-dev zlib1g-dev \
 libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev

The easiest way to install the rbenv tool is to use a shell script. Run the following curl script to download and execute the script:

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

This script clones the rbenv and ruby-build repositories from Github to the ~/.rbenv directory.

This installation script will also call another script to verify the installation process. The script output is similar to the following:

Running doctor script to verify installation...
Checking for`rbenv' in PATH: not found
 You seem to have rbenv installed in `/home/vagrant/.rbenv/bin', but that
 directory is not present in PATH. Please add it to PATH by configuring
 your `~/.bashrc', `~/.zshrc', or `~/.config/fish/config.fish'.

To start using rbenv, you need to add $HOME/.rbenv/bin to your PATH environment variable.

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

Run the rbenv -v command to ensure that the installation is successful:

rbenv -v

Output:

rbenv 1.1.2-30-gc879cb0

To get a list of installed Ruby versions, type:

rbenv install -l

For example, to install Ruby version 2.7.1 and set it as the global version, you would enter:

rbenv install 2.7.1
rbenv global 2.7.1

Rbenv will add a folder named shims in front of your PATH environment variable. This folder contains scripts (shims), which are responsible for running Ruby commands and executing them.

Rbenv allows you to set shell, local and global Ruby versions:

When using rbenv to manage Ruby, do not use sudo to install gems. Each Ruby version is installed in the ~/.rbenv/versions directory and is writable by users.

Three, use RVM to install Ruby

Rvm is a command line tool, you can install, manage, and use multiple Ruby environments.

Install the dependencies required to build Ruby from source code:

sudo apt update
sudo apt install curl g++ gcc autoconf automake bison libc6-dev \
  libffi-dev libgdbm-dev libncurses5-dev libsqlite3-dev libtool \
  libyaml-dev make pkg-config sqlite3 zlib1g-dev libgmp-dev \
  libreadline-dev libssl-dev

Run the following commands to add GPG key and install RVM:

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable

To use RVM, load script environment variables, use the source command:

source ~/.rvm/scripts/rvm

To get a list of Ruby versions installed with this tool, type:

rvm list known

Use Rvm to install the latest version of Ruby and set it as the default version:

rvm install ruby
rvm --default use ruby

Verify that Ruby is installed correctly by printing the version number:

ruby -v

The output is as follows:

ruby 2.7.0p0(2019-12-25 revision 647ee6f091)[x86_64-linux]

If you want to install a specific version of Ruby, enter the following command. Replace xxx with the version of Ruby you want to install:

rvm install ruby-x.x.x
rvm --default use ruby-x.x.x

To switch to another version, but not set as the default Ruby, type:

rvm use ruby-x.x.x

For more information on how to use RVM to manage your Ruby installation settings, check their Documentation Page.

Four, summary##

We have shown you different ways to install Ruby on Ubuntu 20.04. Which method you choose depends on your requirements and parameters. Even though it is the easiest way to install the packaged version through the Ubuntu software source, using Rbenv and RVM can give you more choices, allowing you to choose a different Ruby version for each user.

Original: https://linuxize.com/post/how-to-install-ruby-on-ubuntu-20-04/

 Copyright notice: This work uses Creative Commons attribution-Share 4 in the same way.0 International license agreement for licensing.

If you have any questions, please contact us in the following ways:

WeChat:

WeChat group: add the above WeChat, remark the WeChat group

QQ: 3217680847

QQ Group: 82695646

Recommended Posts

How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to install Ruby on CentOS 8
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to install MariaDB on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to install Nginx on Ubuntu 16.04
How to install OpenCV on Ubuntu 20.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install Go 1.6 on Ubuntu 16.04
How to install Go on Ubuntu 18.04
How to install MySQL on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 20.04
How to install VLC on Ubuntu 18.04
How to install TeamViewer on Ubuntu 20.04
How to install Webmin on Ubuntu 20.04
How to install Docker Compose on Ubuntu 18.04
How to install Ubuntu on Raspberry Pi
How to install Bacula Server on Ubuntu 14.04
How to install MySQL on Ubuntu 18.04 (linux)
How to install Ubuntu 19.10 on Raspberry Pi 4
How to install Apache Kafka on Ubuntu 18.04