This article provides step-by-step instructions on setting up a Ruby ecosystem on a personal computer running on Ubuntu 21.04, using Ruby Version Manager (aka RVM).

The instructions given are for Ubuntu 21.04 users, but it should also work for Ubuntu 20.04 and other Ubuntu-compatible distributions with Pop!_OS inclusive.

Tested environment

An x64-based laptop running on Ubuntu 21.04

  • Linux kernel version: 5.11.0-31-generic

Ruby Installation via RVM

RVM for Ubuntu 21.04

RVM is a command-line tool allowing you to manage multiple Ruby environments on your computer. RVM has a dedicated Ubuntu package, maintained by Josh Buker (https://github.com/rvm/ubuntu_rvm), but this PPA is not keeping up to date. In this article, we will install RVM using the generic RVM installer.

Install the GnuPG 2 package if not yet installed:

$ sudo apt install gnupg2

and get the GPG keys:

$ gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: key 105BD0E739499BDB: public key "Piotr Kuczynski <piotr.kuczynski@gmail.com>" imported
gpg: key 3804BB82D39DC0E3: new key but contains no user ID - skipped
gpg: Total number processed: 2
gpg:           w/o user IDs: 1
gpg:               imported: 1

Install Curl (aka cURL) if not yet installed:

$ sudo apt install curl

and enter the following command to run the RVM installation script:

$ curl -sSL https://get.rvm.io | bash -s stable
Downloading https://github.com/rvm/rvm/archive/1.29.12.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.12/1.29.12.tar.gz.asc
gpg: Signature made Fri 15 Jan 2021 18:46:22 GMT
gpg:                using RSA key 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: Good signature from "Piotr Kuczynski <piotr.kuczynski@gmail.com>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 7D2B AF1C F37B 13E2 069D  6956 105B D0E7 3949 9BDB
GPG verified '/home/hawk/.rvm/archives/rvm-1.29.12.tgz'
Installing RVM to /home/hawk/.rvm/
    Adding rvm PATH line to /home/hawk/.profile /home/hawk/.mkshrc /home/hawk/.bashrc /home/hawk/.zshrc.
    Adding rvm loading line to /home/hawk/.profile /home/hawk/.bash_profile /home/hawk/.zlogin.
Installation of RVM in /home/hawk/.rvm/ is almost complete:

  * To start using RVM you need to run `source /home/hawk/.rvm/scripts/rvm`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
Thanks for installing RVM 🙏
Please consider donating to our open collective to help us maintain RVM.

👉  Donate: https://opencollective.com/rvm/donate

Now, run the source command for initialising the RVM scripts:

$ source ~/.rvm/scripts/rvm

and check the RVM installation by the version:

$ rvm -v
rvm 1.29.12 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]

To load RVM into your shell session as a function, tick on the Run command as a login shell option from the Terminal settings as follows, and then restart.

snip0101

Installing Ruby via RVM

Now, you are ready to install the latest available Ruby. Enter from a Terminal session:

$ rvm install ruby
Searching for binary rubies, this might take some time.
No binary rubies available for: ubuntu/21.04/x86_64/ruby-3.0.0.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for ubuntu.
Installing requirements for ubuntu.
Updating systemhawk password required for 'apt-get --quiet --yes update':
....
Installing required packages: gawk, autoconf, automake, bison, libffi-dev, libgdbm-dev, libncurses5-dev, libsqlite3-dev, libtool, libyaml-dev, sqlite3, zlib1g-dev, libgmp-dev, libreadline-dev, libssl-dev...............
Requirements installation successful.
Installing Ruby from source to: /home/hawk/.rvm/rubies/ruby-3.0.0, this may take a while depending on your cpu(s)...
ruby-3.0.0 - #downloading ruby-3.0.0, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 18.6M  100 18.6M    0     0  1520k      0  0:00:12  0:00:12 --:--:-- 3863k
ruby-3.0.0 - #extracting ruby-3.0.0 to /home/hawk/.rvm/src/ruby-3.0.0.....
ruby-3.0.0 - #configuring......................................................|
ruby-3.0.0 - #post-configuration..
ruby-3.0.0 - #compiling........................................................-
ruby-3.0.0 - #installing.....................
ruby-3.0.0 - #making binaries executable...
Installed rubygems 3.2.3 is newer than 3.0.9 provided with installed ruby, skipping installation, use --force to force installation.
ruby-3.0.0 - #gemset created /home/hawk/.rvm/gems/ruby-3.0.0@global
ruby-3.0.0 - #importing gemset /home/hawk/.rvm/gemsets/global.gems.............-
ruby-3.0.0 - #generating global wrappers........
ruby-3.0.0 - #gemset created /home/hawk/.rvm/gems/ruby-3.0.0
ruby-3.0.0 - #importing gemsetfile /home/hawk/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-3.0.0 - #generating default wrappers........
ruby-3.0.0 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-3.0.0 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri
You need to enter your login password in the course of the above process.

When the installation is complete, check the installed Ruby version(s) by the rvm list command:

$ rvm list
=* ruby-3.0.0 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

(Optional) Installing a previous release

In this article, we pretend that one of the Gem packages has an issue with the latest Ruby update. So, we practice installing an earlier "stable" Ruby release until the problem is gone.

You can list all the installable Rubies by entering:

$ rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.10]
[ruby-]2.5[.8]
[ruby-]2.6[.6]
[ruby-]2.7[.2]
[ruby-]3[.0.0]
ruby-head

:

On noticing the previous release number — for example, 2.7[.x] in this exercise — feed it to the rvm install command:

$ rvm install 2.7
Searching for binary rubies, this might take some time.
No binary rubies available for: ubuntu/21.04/x86_64/ruby-2.7.2.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for ubuntu.
Requirements installation successful.
Installing Ruby from source to: /home/hawk/.rvm/rubies/ruby-2.7.2, this may take a while depending on your cpu(s)...
ruby-2.7.2 - #downloading ruby-2.7.2, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14.0M  100 14.0M    0     0  4262k      0  0:00:03  0:00:03 --:--:-- 4262k
ruby-2.7.2 - #extracting ruby-2.7.2 to /home/hawk/.rvm/src/ruby-2.7.2.....
ruby-2.7.2 - #configuring......................................................-
ruby-2.7.2 - #post-configuration..
ruby-2.7.2 - #compiling........................................................|
ruby-2.7.2 - #installing..................
ruby-2.7.2 - #making binaries executable...
Installed rubygems 3.1.4 is newer than 3.0.9 provided with installed ruby, skipping installation, use --force to force installation.
ruby-2.7.2 - #gemset created /home/hawk/.rvm/gems/ruby-2.7.2@global
ruby-2.7.2 - #importing gemset /home/hawk/.rvm/gemsets/global.gems.............-
ruby-2.7.2 - #generating global wrappers........
ruby-2.7.2 - #gemset created /home/hawk/.rvm/gems/ruby-2.7.2
ruby-2.7.2 - #importing gemsetfile /home/hawk/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.7.2 - #generating default wrappers........
ruby-2.7.2 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.7.2 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri

Check the installed Rubies along with their respective status:

$ rvm list
=> ruby-2.7.2 [ x86_64 ]
 * ruby-3.0.0 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

Note the above rvm install 2.7 command not only installed this specific version — here, Ruby 2.7[.2] — but also made it the "current" version for the current terminal session (i.e. it ran the command rvm use 2.7 silently).

If you plan to use this specific version for the time being — i.e. not just for the current terminal session — you need to make it the "default" Ruby:

$ rvm --default use 2.7
Using /home/hawk/.rvm/gems/ruby-2.7.2
$ rvm list
=* ruby-2.7.2 [ x86_64 ]
   ruby-3.0.0 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

Later, when the issue is gone, you can delete the obsolete version, first by resetting the default Ruby to the latest:

$ rvm --default use 3.0
Using /home/hawk/.rvm/gems/ruby-3.0.0
$ rvm list
   ruby-2.7.2 [ x86_64 ]
=* ruby-3.0.0 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

and then by running the rvm remove command:

$ rvm remove 2.7
ruby-2.7.2 - #removing src/ruby-2.7.2..
ruby-2.7.2 - #removing rubies/ruby-2.7.2..
ruby-2.7.2 - #removing gems....
ruby-2.7.2 - #removing wrappers....
ruby-2.7.2 - #removing environments....
Using /home/hawk/.rvm/gems/ruby-3.0.0

Note that a similar command rvm uninstall just deletes the corresponding folder under '~/.rvm/rubies' while leaving everything else. Use rvm remove for cleaning up most of the install of a Ruby version.

Gem Environment

Various Ruby applications are available as Ruby "Gems". RugyGems.org is the Ruby community’s Gem hosting service.

snip0102

To get the summary of your current Gem environment, enter from a terminal:

$ gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 3.2.3
  - RUBY VERSION: 3.0.0 (2020-12-25 patchlevel 0) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/hawk/.rvm/gems/ruby-3.0.0
  - USER INSTALLATION DIRECTORY: /home/hawk/.gem/ruby/3.0.0
  - RUBY EXECUTABLE: /home/hawk/.rvm/rubies/ruby-3.0.0/bin/ruby
  - GIT EXECUTABLE:
  - EXECUTABLE DIRECTORY: /home/hawk/.rvm/gems/ruby-3.0.0/bin
  - SPEC CACHE DIRECTORY: /home/hawk/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /home/hawk/.rvm/rubies/ruby-3.0.0/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - x86_64-linux
  - GEM PATHS:
     - /home/hawk/.rvm/gems/ruby-3.0.0
     - /home/hawk/.rvm/rubies/ruby-3.0.0/lib/ruby/gems/3.0.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /home/hawk/.rvm/gems/ruby-3.0.0/bin
     - /home/hawk/.rvm/gems/ruby-3.0.0@global/bin
     - /home/hawk/.rvm/rubies/ruby-3.0.0/bin
     - /home/hawk/.rvm/bin
     - /home/hawk/.nvm/versions/node/v14.17.5/bin
     - /usr/local/cuda/bin
     - /usr/local/sbin
     - /usr/local/bin
     - /usr/sbin
     - /usr/bin
     - /sbin
     - /bin
     - /usr/games
     - /usr/local/games
     - /snap/bin
     - /snap/bin

To list locally installed Gem packages, use the gem list command:

$ gem list
*** LOCAL GEMS ***

abbrev (default: 0.1.0)
base64 (default: 0.1.0)
benchmark (default: 0.1.1)
bigdecimal (default: 3.0.0)
bundler (default: 2.2.3)
bundler-unload (1.0.2)
cgi (default: 0.2.0)
csv (default: 3.1.9)
date (default: 3.1.0)
debug (default: 0.1.0)
delegate (default: 0.2.0)
did_you_mean (default: 1.5.0)
digest (default: 3.0.0)
drb (default: 2.0.4)
english (default: 0.7.1)
erb (default: 2.2.0)
etc (default: 1.2.0)
executable-hooks (1.6.1)
fcntl (default: 1.0.0)
fiddle (default: 1.0.6)
fileutils (default: 1.5.0)
find (default: 0.1.0)
forwardable (default: 1.3.2)
gdbm (default: 2.1.0)
gem-wrappers (1.4.0)
getoptlong (default: 0.1.1)
io-console (default: 0.5.6)
io-nonblock (default: 0.1.0)
io-wait (default: 0.1.0)
ipaddr (default: 1.2.2)
irb (default: 1.3.0)
json (default: 2.5.1)
logger (default: 1.4.3)
matrix (default: 0.3.1)
minitest (5.14.2)
mutex_m (default: 0.1.1)
net-ftp (default: 0.1.1)
net-http (default: 0.1.1)
net-imap (default: 0.1.1)
net-pop (default: 0.1.1)
net-protocol (default: 0.1.0)
net-smtp (default: 0.2.1)
nkf (default: 0.1.0)
observer (default: 0.1.1)
open-uri (default: 0.1.0)
open3 (default: 0.1.1)
openssl (default: 2.2.0)
optparse (default: 0.1.0)
ostruct (default: 0.3.1)
pathname (default: 0.1.0)
power_assert (1.2.0)
pp (default: 0.1.0)
prettyprint (default: 0.1.0)
prime (default: 0.1.2)
pstore (default: 0.1.1)
psych (default: 3.3.0)
racc (default: 1.5.1)
rake (13.0.3)
rbs (1.0.0)
rdoc (default: 6.3.0)
readline (default: 0.0.2)
readline-ext (default: 0.1.1)
reline (default: 0.2.0)
resolv (default: 0.2.0)
resolv-replace (default: 0.1.0)
rexml (3.2.4)
rinda (default: 0.1.0)
rss (0.2.9)
rubygems-bundler (1.4.5)
rvm (1.11.3.9)
securerandom (default: 0.1.0)
set (default: 1.0.1)
shellwords (default: 0.1.0)
singleton (default: 0.1.1)
stringio (default: 3.0.0)
strscan (default: 3.0.0)
syslog (default: 0.1.0)
tempfile (default: 0.1.1)
test-unit (3.3.7)
time (default: 0.1.0)
timeout (default: 0.1.1)
tmpdir (default: 0.1.1)
tracer (default: 0.1.1)
tsort (default: 0.1.0)
typeprof (0.11.0)
un (default: 0.1.0)
uri (default: 0.10.1)
weakref (default: 0.1.1)
yaml (default: 0.1.1)
zlib (default: 1.1.0)

After you have had your Ruby ecosystem set up, you can install any Gem packages simply using the gem install command. For example, to install Asciidoctor, enter from a terminal:

$ gem install asciidoctor

When upgrading a Gem package, use gem update and not gem install:

$ gem update asciidoctor

If you accidentally used gem install, it will end up having both versions installed. If that happens, remove the older version using the gem cleanup command:

$ gem cleanup asciidoctor