Ruby is an object-oriented interpreted programming language that Yukihiro Matsumoto created. This open-source language has a lot of use cases, such as creating web applications and data analytics.
Ruby is loved for its simple syntax, which makes it feel like developers are coding in English.
On the other hand, Ubuntu, a Linux distribution based on Debian, is one of the most famous free and open-source operating systems. The ease of use of Ubuntu has made it popular with developers. If you don’t have Ubuntu installed on your PC, you can get it from the official website.
Installing Ruby on Ubuntu 22.10 is your first step towards creating various solutions using this powerful language.
This article will discuss the 3 different approaches to installing the Ruby development environment in Ubuntu 22.10.
Install Ruby Using Ubuntu 22.10 repository
This approach uses the built-in apt package manager to install Ruby on Ubuntu 22.10. You can achieve this by following these steps;
Step 1: Update packages
The first step is to update the system packages on your Ubuntu operating system. Open your terminal and use the following command;
sudo apt update
Step 2: Upgrade the system
This step upgrades the entire system. Use this command;
sudo apt upgrade
Step 3: Install Ruby
You are now ready to install Ruby. Use this command;
sudo apt install ruby-full
After the installation process is over, you can check the Ruby version installed using this command;
ruby -v
You will get a similar output to this one;
This is the easiest way to install Ruby on Ubuntu 22.10. However, this approach has its downsides.
- May not install the latest Ruby version: This approach installs the Ruby version on the Ubuntu package repositories, which may not be the latest Ruby version.
- Does not install multiple Ruby versions: Ruby works with various gems (libraries/packages) to provide additional functionality. Some of these gems work with only specific Ruby versions. The next two approaches solve this challenge.
Install Ruby using Ruby Version Manager (RVM)
Ruby Version Manager, or rvm, is a command line tool allowing developers to install, manage and work with multiple Ruby environments. rvm has various solutions for developers in various development stages, such as production, development, testing, and Gem management. Follow these steps to install Ruby using rvm;
Step 1: Update Ubuntu Packages
Get your system ready for installation using this command;
sudo apt update
Step 2: Add Ubuntu dependencies
Ruby requires various dependencies to run properly on Ubuntu. Run this command to install all the dependencies;
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
Step 3: Get rvm installation script
Run this command;
curl -sSL https://get.rvm.io | bash -s stable
Step 4: Load rvm script environment
Run this command;
source ~/.rvm/scripts/rvm
Step 5: Check rvm list
Run this command;
rvm list known
Step 6: Install Ruby
You can install a specific Ruby version or leave it up to rvm to pick the latest version on its list. The list of available Ruby versions is displayed when you run the command in step 5.
To install a specific version, such as ruby 3.0.0, use this command;
rvm install ruby 3.0.0
To install the latest version on rvm, use this command;
rvm install ruby
Step 6: Check the Ruby version
Run this command;
ruby -v
If Ruby is installed, you will have a similar output
How to troubleshoot your RVM install
You may have missed one step in your RVM installation and configuration process.
Run this command to check if you have configured your shell correctly;
curl -sSL https://get.rvm.io | bash -s stable
If you get this output;
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: http://curl.haxx.se/docs/sslcerts.html
Then, it means that you didn’t do the configuration right, and you need to install ca-certificates. Run this command to fix the issue;
apt-get install ca-certificates
Run this command to check if the changes have been applied;
type rvm | head -1
If you don’t rvm is a function on the output, the configuration is wrong.
How to uninstall/ remove Ruby using RVM
You can uninstall a certain Ruby version from your PC. You can also do away with rvm and use another tool. Follow these steps;
Step 1: Check the current Ruby version using this command
rvm list
Step 2: Uninstall the identified Ruby version, for our case, it is ruby-3.0.0. Use this command;
rvm remove 3.0.0
Step 3: Check if uninstall was successful using this command;
ruby -v
Step 4: Remove RVM
Even though we have uninstalled ruby, RVM is still on our machine. Use this command to uninstall;
rvm implode
The system will ask you if you want to remove rvm; type ‘yes’, and enter.
Step 5: Remove RVM directories
Use this command;
rm -rf ~/.rvm
Step 6: Edit .bashrc, .bash_profile, .profile, and .zshrc.
In our case, we are using bashrc.
Run this command to open your code editor;
nano ~/.bashrc
Locate all the files that have RVM and delete them. Save the changes and exit the editor.
Install Ruby using rbenv
rbenv is a version manager tool developers can use to switch between different Ruby versions. This tool comes in handy to ensure that you run the right Ruby version on every project you build.
Follow these steps to install rbenv on Ubuntu 22.10;
Step 1: Update your system
Use this command to get your system ready for updates;
sudo apt update
Step 2: Install dependencies
Ruby needs various dependencies to run on your local machine. Install them using this command;
sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev
Step 3: Fetch the install script from GitHub
In this step, you will use curl to get the install script from GitHub. You will then pipe it to bash to make the installer run. Use this command;
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash
Step 4: Add rbnev to your path
This step alters the ~/.bashrc
file, allowing you to use the rbnev command line. Use this command;
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
Step 5: Make rbnev load automatically
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
Step 6: Apply the changes
source ~/.bashrc
Step 7: Check if the installation is successful
type rbenv
Step 8: Install the build plugin
Check all the available Ruby versions using this command;
rbenv install -l
Step 9: Install Ruby
For demonstration purposes, we will install Ruby-3.2.1. Use this command;
Rbnev install 3.2.1
Step 10: Set the installed Ruby version as the default
rbenv global 3.2.1
How to update rbnev
We have installed rbnev using Git. Whenever a new version is created, you can install it through these commands;
cd ~/.rbenv
git pull
How to uninstall Ruby using rbnev
As time passes, you may have more Ruby versions than you need in your machine. You can uninstall such ruby versions using this command;
rbenv uninstall (ruby version)
If, for instance, you want to uninstall Ruby 3.2.1, then the command will be;
rbenv uninstall 3.2.1
How to uninstall rbnev
Step 1: Remove rbnev files from bash.
Use this command to open the code editor;
nano ~/.bashrc
Step 2: Locate these files and delete them, save and exit
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Step 3: Remove rbnev and associated files
Use this command;
rm -rf `rbenv root`
You can now log out to let the changes apply.
Create a simple Program in Ruby
Since we now have Ruby installed on Ubuntu, we can create a hello world to demonstrate how it works. All Ruby files should have a .rb extension. We can use the inbuilt code editor for demonstration purposes;
Step 1: Create a hello.rb file using nano.
Use this command
nano hello.rb
Step 2: Add this text to your editor;
puts "hello world"
Step 3: Save and exit the editor
Step 4: Run this command;
ruby hello.rb
The following should be displayed as the output;
Frequently Asked Questions
No. Ubuntu is open-source software that you can download for free. Ubuntu operates under GNU GENERAL PUBLIC LICENSE.
There are three major approaches; the Ubuntu repository, rvm, and rbnev. The last two options are the most advisable.
Both rvm and rbnev are designed to achieve the same goal, manage different Ruby versions. The choice between the two will be a matter of preference and taste.
Yes, but not advisable. The two tools are likely to conflict if they are not configured properly. Using both tools in the same Ruby application is not advisable as it may lead to inconsistencies.
Conclusion
You now have the different approaches you can use to install Ruby on Ubuntu 22.10. The first option is the easiest, but its biggest disadvantage is that you can manage different Ruby options on your local machine.
Both rvm and rbnev are designed to achieve the same goals. Choosing between rvm
and rbnev
is a matter of preference. You can use either to install libraries and frameworks such as Ruby on Rails and Sinatra, which are packaged as gems.
If you want to escape the task of installing Ruby on your computer, check out the best Ruby online compilers you can try today.