How to Install Ruby on Ubuntu 20.04

Introduction

Ruby is a popular open-source programming language focusing on simplicity and efficiency. The language serves as the basis of the Ruby on Rails framework and is used for general-purpose programming, data analysis, and web applications.

In this tutorial, we provide a step-by-step guide to installing Ruby using the official repositories and third-party tools.

How to install Ruby on Ubuntu 20.04

Prerequisites

Install Ruby via Ubuntu Repository

Using the built-in apt package manager offers the fastest and easiest way to install Ruby on Ubuntu:

1. Update the system repositories with:

sudo apt update

2. Use the following command to install Ruby:

sudo apt install ruby-full

3. When prompted, type Y and press Enter to confirm the installation.

Confirming the Ruby installation using the official repository

4. Once the installation is complete, verify it by checking the current version of Ruby:

ruby --version
Verifying the Ruby installation

Install Ruby Using Rbenv

Rbenv is a command line tool that lets you switch between installed versions of Ruby. It can also install new versions of Ruby using the ruby-build plugin.

Step 1: Download Updates and Dependencies

1. Start by updating the system repositories:

sudo apt update

2. Download and install the libraries and compilers Ruby needs to run:

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

3. Type Y and press Enter to confirm the installation.

Step 2: Install Rbenv

1. Download and run the shell script used to install Rbenv:

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

2.  You need to add $HOME/.rbenv/bin to your PATH environment variable to start using Rbenv.

Use the following commands if you are using the Bash shell:

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

For the Zsh shell, use:

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

3. Verify the installation by checking the version of Rbenv:

rbenv -v
Verifying the Rbenv installation

Step 3: Install Ruby

1. List all of the versions of Ruby available for installation through Rbenv with:

rbenv install -l
Listing all versions of Ruby available through Rbenv

2. Select a version of Ruby and install it using:

rbenv install [version number]

In this example, we are installing Ruby 3.0.2:

rbenv install 3.0.2
Installing Ruby using Rbenv

Note: Installing Ruby using Rbenv might take some time. If you want the command output to list each step of the installation process, enable the verbose output by adding the --verbose flag: rbenv install --verbose [version number].

3. Set the newly installed version of Ruby as the global version:

rbenv global [version number]

For this example:

rbenv global 3.0.2

4. Verify the installation by checking the current version of Ruby:

ruby --version
Verifying the Ruby installation using Rbenv

Install Ruby Using RVM

RVM is a command line tool that helps you install and manage multiple Ruby environments on the same system.

Step 1: Download Updates and Dependencies

1. Update the system repositories by using:

sudo apt update

2. Download and install dependencies Ruby needs to run properly:

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

3. Type Y and press Enter to confirm the installation when prompted.

Step 2: Install RVM

1. Add the GPG key used to verify the RVM installation file:

gpg --keyserver hkp://pgp.mit.edu --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
Adding GPG keys used when installing RVM

2. Download and install RVM using the installation script:

curl -sSL https://get.rvm.io | bash -s stable
Installing RVM

3. Load the RVM script environment variables using the source command:

source ~/.rvm/scripts/rvm

Note: Learn more about using the Linux source command.

Step 3: Install Ruby

1. List all versions of Ruby available through RVM:

rvm list known
Listing all versions of Ruby available through RVM

2. Select a version of Ruby and install it using the following syntax:

rvm install ruby-[version number]

Omitting the version number installs the latest stable version of Ruby:

rvm install ruby

3. When prompted, enter your administrator password and press Enter to proceed.

Installing Ruby using RVM

4.  Set the new version of Ruby as the default:

rvm --default use ruby-[version number]

In this example, we are using the default option without the version number:

rvm --default use ruby
Setting the default version of Ruby using RVM

5. Verify the Ruby installation by checking the current version:

ruby --version

Verifying the Ruby installation using RVM

Conclusion

After following this tutorial, you should have a copy of Ruby installed and ready to use. Next, you may want to install the Ruby on Rails stack, which should help simplify and speed up app development.

原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/tech/dev/224167.html

(0)
上一篇 2022年1月7日 07:59
下一篇 2022年1月7日 08:00

相关推荐

发表回复

登录后才能评论