Let's install Ruby!
Should be simple. Let's have a look at the download page of the Ruby website:
Just looking over the variety of install options here can be overwhelming for a beginner.
Now consider that different Ruby projects often require different versions of Ruby to run correctly. As you begin to work on multiple Ruby projects, this means you are likely going to need more than one version of Ruby installed on your machine, and to be able to switch back and forth between them.
For this and other reasons, most development teams use a tool to simplify the process of installing and managing Ruby versions. That's what we'll be looking at in this chapter.
On linux-based systems like Ubuntu and OS X, we'll be looking at rbenv. For Windows, we'll look at Ruby Installer (https://rubyinstaller.org/).
Let's look at rbenv first. If you're using a Windows machine, you can skip to the section "Use Ruby Installer on Windows", below.
rbenv for Mac and Linux systems
If you're using a mac or a linux-based system like Ubuntu, rbenv is a great tool for managing Ruby versions: rbenv install page
Since it doesn't also handle installation out of the box, we'll need to install the ruby-build plugin as well to get that functionality: https://github.com/rbenv/ruby-build#readme
On a Mac, you can use the Homebrew package manager to get both of these. If you have Homebrew installed, you can do:
$brew update $brew install rbenv ruby-build
In fact, if you have a mac and don't have Homebrew installed, I'd recommend installing it now. To do web development in 2017 you probably want to use a package manager, and as OS X doesn't have a native package management system (as many linux systems do), Homebrew has arisen to fill that gap and become the package manager of choice for OS X. You can install it here: https://brew.sh/
On another linux-based system, the process of acquiring these packages will be slightly different. I recommend checking out the install pages for rbenv and ruby-build (see links above ^).
Whatever method you use, make sure you install both rbenv and ruby-build before continuing with this chapter.
Now that you've got rbenv and ruby-build installed, let's run the rbenv setup script.
$ rbenv init
Note that if you just installed rbenv, the shell where you installed it might not have access yet to the "rbenv" command. If you can't get rbenv to work, try opening a new Terminal window or shell. This will make sure that your command line has access to any newly installed commands.
OK, whew, that was a lot of legwork and we still don't have Ruby installed! Don't worry, that's next.
Let's see what versions of Ruby are available for installing:
$ rbenv install -l
Running this command should output a list of Ruby versions. If you scroll through the list, you can see that there are different formats for these version strings. The list should start with numbered versions in the semver format. These are versions of the standard Ruby interpreter, also known as Matz's Ruby Interpreter, or MRI, after Yukihiro "Matz" Matsumoto, the creator of Ruby. This is the "original" Ruby. Later in the list, you should see a series of "Ruby versions" that begin with letters like "jruby" or "rbx". These "versions" are actually rewrites of the Ruby language on top of other languages.
A little about the different Rubys
The core Ruby implementation is the one that was written by language's creator, Yukihiro Matsumoto (aka "Matz") in 1993, and updated since as the language has evolved. This is called MRI, short for Matz's Ruby Interpreter. When you see a version number in rbenv with no identifying letters coming before the semver numbers, it signifies a version of MRI. The highest number in the list of MRI versions is the most current stable version of Ruby, and that's the one we'll install in a moment.
MRI is actually written using another programming language: C. That is, all the behavior of Ruby is defined in code that's actually written in C. When you run a Ruby program, the Ruby interpreter you're using converts all your ruby-code into C-code, then has the computer run this "compiled" C code.
This is where some of the other Rubys come in. In order to gain performance and other benefits, over the years people have implemented the behavior of Ruby using different underlying languages. jruby, for example, is a rewrite of Ruby using Java instead of C to run it.
Some other versions of Ruby are not such radical rewrites, and may just be "forks" of the language that allow different behaviors. Remember, Ruby is open-source, so anyone can create any variation on it that they want, including you!
When are we actually going to install Ruby?!@(#*$%@!)
Go through the output of
rbenv install -land find the highest version of MRI in the list. Take that version string exactly as it is, and use it in the install command. Note that you want the plain semver version, not anything with a special string at the end such as "dev", "preview", or "rc1". Those strings indicate versions of language that are still in development, or not "stable".
At the time that I'm writing this course, the highest MRI version listed by rbenv is 2.3.1, so my install command looks like this:
$ rbenv install 2.3.1
After that's done running, MRI 2.3.1 should be installed on your machine. Let's first ask rbenv what version of Ruby is currently available to the command line:
$ rbenv version
This command tells you the active version of Ruby that you're using now. Let's tell rbenv now to always use 2.3.1 unless we specify something different:
$ rbenv global 2.3.1
Now you can type
at the command prompt and be dropped into an interactive Ruby shell that is running the latest stable Ruby! You should be able to do any of the things you learned with Ruby previously. Try it out!
Installing on Windows
This chapter has focused on rbenv, which only works in linux-based systems like OS X, Ubuntu, and others. If you're using Windows, you'll have quite a different workflow from Mac developers. While you won't be able to use rbenv, there are other tools that accomplish something similar, and of course everything about the different Ruby implemntations and versioning is still true for you.
If you're using Windows, I recommend using the Ruby Installer project to keep up to date with Ruby versions. Follow the instructions for installing the Ruby Installer here:
Ruby Installer (https://rubyinstaller.org/)