Ruby on Linux
Although many distros come with a system Ruby installed or a repository version, we suggest using a version manager such as rbenv or RVM. This will ensure you have an up to date Ruby version and a clean environment separated from your system Ruby.
The instructions for installing via rbenv on Ubuntu are provided below (since that is what we generally use).
Install Ruby via rbenv
Installing rbenv is a manual process, which is sort of nice because it is so minimal!
1. Install build dependencies
You will need these packages to build Gems:
sudo apt install autoconf bison build-essential libssl-dev libyaml-dev libreadline-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev
Install and then close your terminal.
(this is based on the suggested build environment from ruby-build, slightly updated and tweaked for simplicity)
2. Install rbenv
Using the Basic Git Checkout method to install rbenv is the simpliest and most up to date.
Open a terminal and clone rbenv into your home directory:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Then run the init function to set it up:
~/.rbenv/bin/rbenv init
Close terminal and open a new one.
3. Add ruby-build
Clone ruby-build into the rbenv plugins directory:
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
4. Install Ruby
Check the Ruby download page to find the latest stable version or your project requirements to find your desired Ruby version number.
Open your terminal and use rbenv install + version number, e.g.:
rbenv install 4.0.10
This can take awhile since ruby-build will download and build from source.
Once complete, set the version you want to use:
rbenv global 4.0.10
Now, ruby -v should report what you just set.
To avoid possible issues, we suggest you update Ruby Gem at this time. Gem is a tool to install Ruby Gems packages on your system. It comes with your Ruby, but may be out of date in the installer. To update, type the command: gem update --system
Updating Ruby on Linux
To update your Ruby version on Linux, you will start by updating rbenv and ruby-build. This is important since the list of available Ruby versions (rbenv install -l) is NOT automatically updated.
Since you installed the tools using git, to update rbenv and ruby-build you simply git pull the most recent master branch code.
cd ~/.rbenv && git pull
cd "$(rbenv root)"/plugins/ruby-build && git pull
Next, follow the Install Ruby instructions above for your new version, installing and setting it as global.
Then update Gems with gem update --system.
In any existing projects, you will want to delete the “Gemfile.lock” file (since it is based on your old versions) and do a fresh bundle install to get the updated dependencies.
Finally, to clean up old versions of Ruby, you can list all versions installed using rbenv versions, then remove old versions with rbenv uninstall, like rbenv uninstall 4.0.5.
Note: there is technically an Ubuntu package available so you can use sudo apt install rbenv ruby-build. Unfortunately ruby-build is super out of date, so it will only list very out-of-date versions of Ruby to install. This is not a good install method!