Step-by-step guide for installing Ruby and RVM on Ubuntu with command line setup.

Install Ruby and Set Up RVM Programming Environment on Ubuntu 22.04

Table of Contents

Introduction

Installing Ruby and setting up the RVM programming environment on Ubuntu is a key step in building a solid development foundation. Ruby, a dynamic programming language, works seamlessly on Ubuntu when paired with RVM (Ruby Version Manager), which simplifies managing multiple Ruby versions. Whether you’re a beginner or familiar with Ubuntu, this guide will walk you through the entire process, from installation to running your first “Hello, World” Ruby program. With this setup, you’ll have a powerful environment for exploring Ruby development and creating robust applications. In this article, we’ll show you how to get Ruby and RVM running smoothly on your Ubuntu 22.04 system.

What is Ruby Version Manager (RVM)?

Ruby Version Manager (RVM) is a tool that helps you easily install and manage different versions of Ruby on your system. It simplifies the process of setting up a Ruby environment, ensuring all necessary libraries are installed, and makes it easy to switch between versions of Ruby if needed.

Step 1 — Using the Terminal

To install Ruby, you’ll be using the command line interface (CLI). The command line is basically a text-only way to interact with your computer, instead of clicking buttons or using a mouse like you do with the graphical user interface (GUI). Instead of clicking on icons and menus, you type commands as text and get feedback in text form. This “non-graphical” interface, also known as a shell, is super powerful because it lets you control your computer directly. It also helps you automate tasks that you do all the time, like moving files or running programs. It’s an essential tool for developers, and once you get the hang of it, you’ll see just how much more efficient it is for managing your development environment. With the shell, you can run commands to do everything from simple file operations to configuring your system.

If you’ve never used the command line before, don’t worry—it might seem a little intimidating at first, but you’ll get comfortable with it pretty quickly. If you need help getting started, you can look up some basic terminal commands or even check out a beginner guide like “An Introduction to the Linux Terminal.” In this tutorial, we’ll walk you through setting up Ruby on your system using the command line on Ubuntu, specifically through installing RVM (Ruby Version Manager). RVM is a great tool that makes managing Ruby versions and dependencies super simple.

Now that your Ubuntu server is up and running, you’ll need to dive into the terminal to install RVM. This tool will take care of installing Ruby, along with all the libraries and dependencies Ruby needs to run. It’ll make getting Ruby up and running on your system way easier.

Make sure to follow each command carefully as the setup process can be a bit tricky for beginners.



$ sudo apt-get update

Once your system is updated, you can proceed with installing RVM.



$ sudo apt-get install curl gpg


$ \curl -sSL https://get.rvm.io | bash -s stable

Output

# Output after successful installation of RVM
% rvm is a function
% rvm 1.29.12 (latest stable) installed

Once RVM is installed, you can verify it with:


$ rvm –version

This will display the version of RVM installed on your system.

For more insights on working with the command line, check out this detailed guide on command-line tools for beginners.

Step 2 — Installing RVM and Ruby

RVM (Ruby Version Manager) automates the process of setting up a Ruby environment on your Ubuntu system. This tool makes it easier to install Ruby and manage different versions of Ruby on your system. To get started, we need to install RVM, which will, in turn, install Ruby and its prerequisites.

The most efficient way to install Ruby with RVM is to run the installation script available on the official RVM website. First, you’ll need to use the gpg command to contact a public key server and request the RVM project’s key. This key is used to sign each RVM release, allowing you to verify the legitimacy of the release before downloading it. From your home directory, execute the following command:


$ gpg –keyserver hkp://pool.sks-keyservers.net –recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

If the command above fails, you can try the following alternative commands:


$ curl -sSL https://rvm.io/mpapis.asc | gpg2 –import


$ curl -sSL https://rvm.io/pkuczynski.asc | gpg2 –import

Next, we’ll use curl to download the RVM installation script. If curl is not installed on your system, you can install it by running the following command:


$ sudo apt-get install curl

This will prompt you for your password. When you type your password, it will not be displayed on the screen as a security measure, but rest assured that your keystrokes are being recorded. After entering your password, press ENTER to proceed with the installation of curl . Once installed, use the following command to download the RVM installation script:


$ \curl -sSL https://get.rvm.io -o rvm.sh

Now, let’s review the flags used with curl . The -s or --silent flag suppresses the progress meter, so it doesn’t display unnecessary output. The -S or --show-error flag ensures that any errors encountered during the download process are displayed. The -L or --location flag tells curl to follow any redirects automatically, in case the server redirects the request to a different location.

Once the rvm.sh script is downloaded, you can review its contents before executing it by running:


$ less rvm.sh

Use the arrow keys to scroll through the file and press q to exit once you are done. Once you’re satisfied with the script’s contents, you can execute it by running:


$ cat rvm.sh | bash -s stable

This script will create a new directory called .rvm in your home directory. This directory will contain Ruby and all of its related components. Additionally, the script modifies your .bashrc file to add the .rvm/bin directory to your system’s PATH environment variable, making the rvm command available in the terminal. However, this modification won’t take effect in your current terminal session, so you’ll need to reload the terminal configuration by running:


$ source ~/.rvm/scripts/rvm

Now, you can use RVM to install the latest stable version of Ruby by running:


$ rvm install ruby –default

This command will download and install Ruby along with its necessary components. It will also set the installed version of Ruby as the default for your system, ensuring that any existing Ruby installations do not conflict with the new version. Keep in mind that the installation process might take some time, depending on your system’s specifications and internet speed.

If you encounter any issues during the installation, make sure you have Homebrew installed. Homebrew is a package manager that can help resolve missing dependencies. To install Homebrew, run:


$ /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”

During the installation, RVM will automatically fetch any missing prerequisites and install them as needed. It may prompt you for your password during this process.

Once the prerequisites are satisfied, RVM will begin downloading and installing Ruby. You will see output similar to the following:

Output

ruby-2.4.0 – #configure ruby-2.4.0 – #download % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 16.4M 100 16.4M 0 0 4828k 0 0:00:03 0:00:03 –:–:– 4829k

After Ruby has been successfully installed, you can verify the installation by checking the Ruby version with:


$ ruby -v

This command will output the specific version of Ruby you installed. For example:

Output

ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]

Lastly, to ensure that RVM automatically uses the newly installed version of Ruby each time you open a terminal window, you’ll need to make a slight adjustment to your system. RVM modifies the .bash_profile file, but this file is only invoked on login shells. So, you must ensure that your terminal opens as a login shell to apply the changes.

Once these steps are completed, Ruby will be ready to use on your system.

For additional details on managing Ruby environments and dependencies, explore this comprehensive guide on Installing Ruby and Managing Versions with RVM.

Step 3 — Creating a Simple Program

Now, let’s create a basic “Hello, World” program in Ruby. This simple exercise serves as a way to test that your Ruby environment is set up correctly and functioning as expected. It also gives you the chance to get comfortable with writing and running Ruby programs.

To begin, you’ll need to create a new file, which we’ll call hello.rb , using the text editor nano . You can do this by typing the following command in your terminal:

$ nano hello.rb

This will open the nano editor. In the editor, type the following Ruby code:

puts “Hello, World!”

This line of code tells Ruby to output the string “Hello, World!” to the screen when the program is executed. After typing the code, you can exit the editor by pressing CTRL+X . When prompted to save the changes, press Y to confirm, and then press ENTER to save the file with the name hello.rb .

Next, you can run the program by typing the following command in your terminal:

$ ruby hello.rb

Once executed, the program will run and display the following output on the screen:

Output
Hello, World!

This output indicates that your Ruby development environment is functioning correctly. The program successfully executed, and you are now ready to explore more advanced Ruby programming techniques. This simple exercise lays the foundation for working with Ruby, and you can now begin creating larger and more complex projects using this environment.

For more information on starting with Ruby and basic program creation, check out this guide on learning Ruby with practical examples.

Conclusion

In conclusion, setting up Ruby and RVM on Ubuntu 22.04 is an essential step for any developer looking to dive into Ruby programming. By following this step-by-step guide, you’ll have Ruby installed and ready to go in no time, along with the powerful RVM tool to manage multiple Ruby versions. Whether you’re a beginner or experienced with Ubuntu, this guide ensures a smooth installation process and a solid foundation for future Ruby development projects. With Ruby and RVM set up on your system, you can now explore a wide range of programming possibilities and start building powerful applications. Stay tuned for updates on the latest Ruby versions and tools that make programming even more efficient!

Set Up Ruby Development Environment on Windows 10 with WSL and RVM