This is my take on making a tutorial on how to install Ruby across Windows, macOS, and Linux all in this one article!
In a pinch, Ruby is an interpreted, dynamically-typed and high-level programming language where everything is an object! While it is a true object-oriented programming language, the paradigms of programming in a procedural and a functional way can also be done.
Oh, about syntax. It's broadly similar to Python without the flairs of indentation, instead ending control statements, functions, and the like with the end
keyword.
Outside of the programming scheme, Ruby is popularly used alongside Ruby on Rails which is a full-stack web framework. Others include Bundler which provides a consistent environment for managing gems and versions between independent Ruby projects, Ruby 2D for creating 2D apps, games, and visuals, and Pry as an interactive shell. All of these four are what you refer to as Gems, basically libraries also containing Ruby code!
That's all for the background of Ruby and without further ado, let's get right to installing Ruby on your machine!
Note: In any mention of a version number past this point indicates the version of Ruby that was released at the time of writing this article. If you are looking to install an older version of Ruby, this guide is also for you! Just find the version you're looking for in the downloads page or by listing it in their respective package/version managers. This applies to future Ruby versions as well.
Windows
Using the standalone RubyInstaller setup executable
If you're a novice, GUI-based person, you can install Ruby with a usual setup application:
- Download the latest setup executable here and make sure it's the one with a DevKit. Note that you need the DevKit version in order to build C/C++ extensions for Ruby and is necessary for Ruby on Rails, and hundreds of Ruby gems rely on it, so might as well go with the DevKit version as opposed to none
- You're given an option to let the installation be limited to the current user or system-wide. If you don't know what to select, let the installation be only installed to you, the current user
- Along the installation, you will be asked to add Ruby executables to your PATH environment variable and associate Ruby-related files. Assuming this is your first installation of Ruby, leave these checked. If you have an existing Ruby installation, either uncheck these or wipe your current Ruby installation before proceeding
- After finishing with the setup window, a terminal window will pop up that's the DevKit part! Specfically, MSYS2 concerns with necessary dependencies for building Ruby as well as Ruby gems with C-extensions. Press
ENTER
to continue. This will pop up again after installation so hitENTER
afterwards - After installation, you're good to go! There's a Start Menu folder for Ruby where the Interactive Shell and documentation can be found. To verify your installation, you can type
ruby -v
in the Terminal (this also verifies if Ruby is in the PATH environment variable as well!). You can also try line-by-line statements in the interactive shell or typeruby
in the Terminal followed by a.rb
file to interpret that Ruby file!
Using the Windows Package Manager winget
If you have an up-to-date version of App Installer (updated from the Microsoft Store), you can use winget
from the terminal to install Ruby with the following syntax:
winget install RubyInstallerTeam.Ruby.{MAJOR}.{MINOR}
where {MAJOR}
is the major version, while {MINOR}
is the minor version.
To list all available versions:
winget search RubyInstallerTeam.Ruby
To install Ruby 3.2.4 with DevKit, the version available from winget at the time of this article:
winget install RubyInstallerTeam.RubyWithDevKit.3.2
Afterwards, you should be able to perform the same little tests mentioned above as if you used the RubyInstaller method!
macOS
Prerequisites:
git
,curl
, Homebrew,libyaml
,asdf
Since OS X 10.11 El Capitan, Ruby is bundled alongside the operating system and the ability to install gems as the operating system came with such.
This is Ruby 2.6.10 that is bundled with macOS 11 Big Sur. With Homebrew, The built-in Ruby and its dependencies (especially libyaml
) can be updated with this command:
brew install ruby
However, this is for the system-wide Ruby installation. Most Gems wouldn't be compatible especially if they require Ruby 3 and later (such as Ruby 2D). We ran this command in preparation for the next step with the now-updated dependencies. Therefore, the use of a version manager like asdf is recommended.
Installing asdf
Asdf is a version manager that not only supports Ruby but also Node.js, Elixir, Erlang and more! Rbenv is usually the main go-to but there's only one con in that it's only a version manager strictly for Ruby, so from a scalability standpoint, I think asdf is the way to go!
After checking if git and curl are installed, we proceed to downloading asdf:
git clone https://github.com/asdf-vm/asdf.git ~/.asdf
After this point, there are two ways to install asdf (or basically make this visible to your shell)
If you have the barebones zsh shell
Modify ~/.zshrc
to include asdf
as a plugin. You can edit this file by executing
sudo nano ~/.zshrc
Afterwards, paste this line into the editor
. "$HOME/.asdf/asdf.sh"
Finally, you can execute Control+X
to exit and Y
to confirm changes. You should be able to execute asdf
in the terminal.
If you have Oh My Zsh installed
Follow the same steps above when opening ~/.zshrc
, except look for plugins=()
then include asdf
inside plugins.
Execute Control+X
to exit and Y
to confirm changes. You should be able to execute asdf
in the terminal.
Making asdf Take Control of Your Ruby Environment
-
Add Ruby as a plugin in asdf
asdf plugin add ruby
-
To search for a Ruby version to download and install
asdf list all ruby
Multiple unrelated plugins may be listed. Scroll up the terminal until you find listings that are just version numbers, those are the available Ruby versions to download. -
To download and install a specific Ruby version, in this case, Ruby 3.2.4
asdf install ruby 3.2.4
-
After installation, you can switch to your newly installed Ruby version after opening a new terminal session
# SWITCH TO THE NEW RUBY asdf global ruby 3.2.4 # SWITCH BACK TO THE SYSTEM'S RUBY asdf global ruby system
Take note that when using version managers, a gem installed in one version is independent from another. So for example, installing the rails
gem in our 3.2.4 installation will not carry to the operating system's 2.6.10 installation.
What if Ruby failed to install in asdf?
A common problem is with libyaml
. It should be resolved by reinstalling it using Homebrew:
brew install libyaml
Afterwards, proceed installing Ruby in asdf
with the same syntax above.
Linux
For context, this is being installed on Linux Mint which is based on Ubuntu, which is based based on Debian
This process should be fairly similar in a Windows Subsystem for Linux (WSL) environment
I know, this heading may be a too much than just specifying what kind of "Linux" it is, but if you're using a different Linux distribution such as Fedora or Arch, you can head here from the official Ruby documentation website exploring these various distributions. The installation should be similar, only differing in the package manager in your Linux distribution.
In my case, my package manager is apt
. To install Ruby:
sudo apt-get install ruby-full
This will prompt you if you want to continue the installation, in this case type Y
and press ENTER
.
Do you want to continue? [Y/n] Y
After that, Ruby should be installed, but in this case we don't have an interactive shell bundled with it (unlike in Windows), this is where I'll teach you how to install gems as a bonus!
Bonus: Installing Gems! ๐
Think of gems like Python or C/C++ libraries; they're also Ruby code that's also open-source! However, do note that this is applied/installed globally, so if you're working on a Ruby project that has gems (or dependencies) that your global Ruby environment does not necessarily need, it's important that you separate them (like using Bundler).
Importing gems to a Ruby project is similar to the likes of C/C++ and Python save for syntax differences
// C/C++
include <stdio.h>
# PYTHON
import json
# RUBY
require 'rake'
Anyway back to Linux, I'll be installing the gem pry
which will allow me to have an interactive shell in the Terminal:
sudo gem install pry
This is how you add these gems or libraries into your Ruby environment. Also notice how we need elevated permissions with the sudo
command when installing most gems.
After installation, we can immediately interact with the Ruby interpreter by simply typing pry
in the terminal! You can perform the same tests I've done above!
Note that with our previous section, macOS, being UNIX-like, it is also possible to install asdf
in Linux as a version manager for all the other frameworks that you need aside from Ruby (in my Linux Mint installation, I still needed to install zlib1g-dev
using apt). The same is true vice-versa in that installing pry
in macOS is also possible.
Bonus Bonus: I want asdf
in Linux too!
Prerequisites:
zlib1g-dev
,libyaml-dev
,libssl-dev
I figured that majority of users are Windows users utilizing the Windows Subsystem for Linux (WSL) environment, which in most cases their default distribution is Ubuntu. Again, if you're running in a completely different Linux distribution, your prerequisites and package managers will differ. For asdf's full documentation of installing in different environments (shells and package managers), head here!
Installing asdf
takes the same steps as we did it in macOS, except that my Linux Mint installation, for example, uses bash. Fortunately, we can edit our shell with the same nano
command with sudo
. If you're using a different shell like zsh, simply replace .bashrc
.
sudo nano .bashrc
Afterwards, we scroll down to the very bottom of the text and add the following lines:
. "$HOME/.asdf/asdf.h"
. "$HOME/.asdf/completions/asdf.bash"
After saving changes on exit. We have to first make sure that libyaml-dev
and libssl-dev
are installed using apt
(courtesy of this StackOverflow question)
sudo apt install libyaml-dev libssl-dev
After all that's installed, we now install our desired Ruby version using a different command (courtesy of the same source above)
CFLAGS="-Wno-error=implicit-function-declaration" asdf install ruby 3.2.4
However after installation, running the ruby -v
command returns the terminal with no Ruby installation. That is because asdf
is now present in our shell. Fortunately, we can simply switch to our new Ruby installation with asdf global ruby 3.2.4
or asdf global ruby system
to return to the Ruby installation we just installed earlier. The pry
gem we installed also works on both installations.
Conclusion
Now that you have reached the end, you have not only installed Ruby as a fresh development environment alongside your system, but also discovered the convenience of version managers and learned the basics on how to install gems on your Ruby environment! You can now use this environment to run Ruby files with the ruby
command in the Terminal or interpret Ruby lines real-time with your respective interactive shells. With the power of gems, you can now explore the vastness of there is to offer with the simplicity of Ruby!
For documentation on the Ruby programming language such as manuals, head here!
Hope to do a future article on a deep dive on gems, specifically Ruby on Rails!
Top comments (2)
good tutorial...i like it.
This is absolutely mental! Cheers for the tutorial, Luigi! Youโre a proper BSCS (Bachelor of Science in Chad Sigma) student!