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.
Prerequisites
- A system running Ubuntu 20.04 (learn how to Install Ubuntu 20.04 Focal Fossa)
- A user account with sudo privileges
- Access to the terminal window or command line
Install Ruby via Ubuntu Repository
Using the built-in apt package manager offers the fastest and easiest way to install Ruby on Ubuntu:
Note: Learn more about using the apt package manager and the difference between apt and apt-get.
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.
4. Once the installation is complete, verify it by checking the current version of Ruby:
ruby --version
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.
Note: Learn how to set environment variables in Linux.
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
Step 3: Install Ruby
1. List all of the versions of Ruby available for installation through Rbenv with:
rbenv install -l
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
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
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
2. Download and install RVM using the installation script:
curl -sSL https://get.rvm.io | bash -s stable
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
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.
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
5. Verify the Ruby installation by checking the current version:
ruby --version
Note: Learn also how to check Ubuntu version.
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.