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
sudounless The Odin Project specifically says to do so. Failing to follow this can cause a lot of headaches, and never run as therootuser. In some instances, you might see a message in the terminal telling you to usesudoand install something withapt. 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
- 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:
-
Go to
.rbenv/folder in your home directory.cd ~/.rbenv -
Pull latest changes for your ruby version manager (
rbenv).git pull -
Run this command.
~/.rbenv/bin/rbenv init -
Go to
ruby-buildplugin directory. This is whatrbenvuses to compile ruby from source code (which is written in C).cd ~/.rbenv/plugins/ruby-build -
Pull latest changes for
ruby-build.git pull -
Now you can check list of all the latest versions available for you to install.
rbenv install --list-all -
Now you can install the version of Ruby you like by running:
rbenv install x.y.zwhere in
x.y.z-xis the major version,yis the minor version andzis the patch version. This is called semantic versioningFor 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.