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.
From the Ubuntu standard source repository. This is the easiest way to install Ruby on Ubuntu, and should be suitable for most user scenarios. The Ruby version included in the Ubuntu source repository is 2.7.0
.
Use Rbenv. A script that allows you to install multiple Ruby versions on the same machine.
Use Rvm (ruby environment manager). A script that is heavier and encapsulates more features. It allows you to install, manage, and use multiple different versions of Ruby.
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.
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.
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:
The shell version is available in the current shell and has the highest priority. It can be accessed via rbenv shell<ruby-version> The
command sets the RBENV_VERSION
environment variable to define.
The local version is set in each directory. This version is written in the .ruby-version
file. When you run a Ruby script, rbenv searches for this file in the current and all parent directories. It uses the first file found as the Ruby version. To set the local version, switch to this directory and run rbenv local<ruby-version>
Command.
When there is no shell version and the local version is set, the global version will be enabled. Use global<ruby-version>
To set the global version.
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.
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.
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