Ruby

Installing Ruby

Ruby Course

Introduction

Before we start learning, we’ll need to install Ruby first. This section is where you could potentially encounter a lot of errors.

Before continuing, let’s review a few best practices to keep in mind:

  • Copy and paste the commands to avoid typos.
  • Follow the directions closely, and don’t skip over any sections.
  • Do NOT use sudo unless The Odin Project specifically says to do so. Failing to follow this can cause a lot of headaches, and never run as the root user. In some instances, you might see a message in the terminal telling you to use sudo and install something with apt. Ignore that and follow our instructions for now.

Now, let’s get started!

Lesson overview

This section contains a general overview of topics that you will learn in this lesson.

  • Installing and updating Ruby.
  • Setting local and global Ruby versions.

Assignment

Install Ruby

  1. Depending on the OS you are running, follow the appropriate Ruby installation guide below:

Updating Ruby

If you’ve just installed Ruby, congratulations! Your version is probably completely up to date, and you can safely skip this section. However, if you stick with this long enough, the need to update your version of Ruby will eventually arise. Fortunately, having rbenv installed makes this quite straightforward. Here are step by step instructions on how to update your ruby version:

  1. Go to .rbenv/ folder in your home directory.

      cd ~/.rbenv
    
  2. Pull latest changes for your ruby version manager (rbenv).

      git pull
    
  3. Run this command.

      ~/.rbenv/bin/rbenv init
    
  4. Go to ruby-build plugin directory. This is what rbenv uses to compile ruby from source code (which is written in C).

      cd ~/.rbenv/plugins/ruby-build
    
  5. Pull latest changes for ruby-build.

      git pull
    
  6. Now you can check list of all the latest versions available for you to install.

      rbenv install --list-all
    
  7. Now you can install the version of Ruby you like by running:

      rbenv install x.y.z
    

    where in x.y.z - x is the major version, y is the minor version and z is the patch version. This is called semantic versioning

    For example:

      rbenv install 3.4.6
    

Setting local and global Ruby versions

rbenv makes it easy to have multiple different versions of Ruby installed and switch between them. You may have one project that runs with Ruby 3.4 and another that runs with Ruby 3.3, and rbenv will coordinate this for you. The version of Ruby set at the project level is called the “local” version.

To set the local Ruby version, run:

rbenv local x.y.z

This creates a .ruby-version file for storing the local version. To check the current project/directory’s local Ruby version, run:

rbenv local

You can also configure your default Ruby version. This is called the “global” version. To set the global default Ruby version run:

rbenv global x.y.z

To check current global ruby version run:

rbenv global

Extras

If you are using Visual Studio Code as your IDE, you can install the “Ruby LSP” extension, which will provide you with semantic highlighting and formatting support.

Using the extension is optional, but it is a quick install; go to the “Extensions” tab in VSC (Ctrl + Shift + X), search “Ruby LSP”, and click install on the first one. Congratulations, the extension is now installed.

The most important features Ruby LSP provides will work out of the box. But it may bug you about using a monorepo setup, missing lockfiles or rubocop - you can choose “Don’t show again” for now. We will introduce these later.

Knowledge check

The following questions are an opportunity to reflect on key topics in this lesson. If you can’t answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.

Support us!

The Odin Project is funded by the community. Join us in empowering learners around the globe by supporting The Odin Project!