Before you can begin using Ruby to develop software, your development environment must have Ruby installed. Fortunately, your system probably came with Ruby pre-installed. If not, the package manager supplied by your system can install it in just a few minutes.
You should understand how Ruby got on your system (this means either the physical computer or the cloud-based development environment you're using), and how to determine the version of Ruby your system uses. For instance, on a Mac, you can use the Ruby version supplied by Apple as part of OS X or macOS. However, this will likely be an older version of Ruby lacking the most recent features. There may also be some usage restrictions that make it hard to use.
In this chapter, we'll discuss how Ruby gets on your system, and how to determine which Ruby version you are using.
GitHub Codespaces uses pre-built workspaces (codespaces) based on a standard Linux distribution. They take things a bit further, though, and install additional software that almost every Rubyist needs. Specifically, codespaces includes several items seldom found in a default system Ruby. Most notably, it automatically installs the RVM Ruby version manager, and sets things up so that RVM controls which Ruby you use, not the system.
The pre-installed RVM removes several potential stumbling blocks from your path to Ruby enlightenment. For instance, you don't have to install RVM yourself. With RVM, you can install and manage Ruby components without root privileges. This protects you from the problems that can arise when you misuse root privileges.
GitHub Codespaces is the path of least resistance for people who aren't comfortable managing system tools and libraries; we recommend it if you have never installed or configured system tools. If you go this route, you can skip the following sections on installing Ruby version managers.
It's not practical to keep this book completely up-to-date with the latest version of Ruby. Throughout the book, we use some variant of version 2.3 or version 2.2, both of which are now obsolete. You can use whatever version of Ruby that you find convenient; the most recent release is preferable in most cases. However, keep in mind that there may be some minor discrepancies; don't worry if there are some slight differences in outputs.
AWS Cloud9 is no longer available for new customers of AWS. Consider using GitHub Codespaces instead.
AWS Cloud9 uses pre-built workspaces based on standard Linux distributions. They take things a bit further, though, and install additional software that almost every Rubyist needs. Specifically, the workspace for Ruby projects includes several items seldom found in a default system Ruby. Most notably, it automatically installs the RVM Ruby version manager, and sets things up so that RVM controls which Ruby you use, not the system.
The pre-installed RVM removes several potential stumbling blocks from your path to Ruby enlightenment. For instance, you don't have to install RVM yourself. With RVM, you can install and manage Ruby components without root privileges. This protects you from the problems that can arise when you misuse root privileges.
AWS Cloud9 is the path of least resistance for people who aren't comfortable managing system tools and libraries; we recommend it if you have never installed or configured system tools. If you go this route, you can skip the following sections on installing Ruby version managers.
It's not practical to keep this book completely up-to-date with the latest version of Ruby. Throughout the book, we use some variant of version 2.3 or version 2.2, both of which are now obsolete. You can use whatever version of Ruby that you find convenient; the most recent release is preferable in most cases. However, keep in mind that there may be some minor discrepancies; don't worry if there are some slight differences in outputs.
On Macs, Ruby is part of the standard OS X/macOS installation. You can find the ruby
command at /usr/bin/ruby
. This is called your "system Ruby". Your system Ruby is a complete installation of Ruby and its standard components. However, the installation is usually an older version of Ruby. To check the exact version, you can ask your system Ruby for its version number. On the author's Mac:
$ /usr/bin/ruby -v
reports that the version is 2.0.0p648. As of this writing, the latest Ruby version is version 2.3.2.
The Mac system Ruby has a characteristic that makes it undesirable for developers: it needs root access to install and manipulate other Ruby components (we'll discuss these later). Root access is a privileged user level that isn't always available to the developer, so the developer lacks the permissions needed to easily install these components. That's a real hardship.
Between being outdated and needing root access, the system version of Ruby on a Mac is suboptimal for development; you should install a Ruby version manager and use it to install the Rubies you need (you'll see people say "Rubies" to just mean "different versions of Ruby"). We discuss Ruby version managers in a later chapter.
On a Mac, you should install Homebrew, a package manager designed for Macs; you may need this to install some programs that are useful with Ruby, like PostgreSQL. Follow the instructions on the Homebrew home page to install Homebrew on your system. After installing Homebrew, you can use it to install and manage lots of tools, including Ruby.
Apple intends to remove Ruby entirely from future versions of the macOS operating system. At that point, you will have to use a Ruby version manager or a package manager like Homebrew to install Ruby.
Depending on your distribution, Ruby may or may not be pre-installed on your Linux system. If not, you can install it with your package manager, e.g., RPM, Yum, DPKG, etc. Regardless, the ruby
program will end up in the /usr/bin
and /usr/lib/ruby
directories, just like on a Mac.
As with a Mac, the system version of Ruby may be old compared to the latest release. You can determine the system Ruby version with:
$ /usr/bin/ruby -v
The Linux system Ruby may also suffer from needing root access, as described in the previous section.
Between being outdated and needing root access, the system version of Ruby on Linux is suboptimal for development. Thus, you should install a Ruby version manager, and use it to install the Rubies you need. We discuss Ruby version managers in a later chapter.
You can determine where your system finds the ruby
command with this command:
$ which ruby
This shows the path name of the file that will run when you type ruby
. The path is the easiest way to tell which Ruby you are running:
/usr/bin/ruby
This is the system Ruby on both Mac and Linux systems. As described above, it isn't suitable for development owing to age and permissions issues. If you see that you are using a system Ruby, you should install a Ruby version manager (discussed later).
The system Ruby also stores some additional commands (like irb
and rake
) in the /usr/bin
directory. /usr/lib/ruby
contains other Ruby components such as the libraries and Gems. On a Mac, /usr/lib/ruby
provides access to the Ruby components, but this is just an alias for the real directory that is nested deeply in /System/Library/Frameworks
. (You should never modify or delete files in this directory directly.)
/usr/local/rbenv/shims/ruby
The presence of the words "rbenv" or "shims" means you are using a version of Ruby installed by the rbenv Ruby version manager. We discuss rbenv later on. Note that the pathname may not match the one shown: look for "rbenv" or "shims" as part of the name to determine if you are using an rbenv-managed Ruby.
/usr/local/rvm/rubies/ruby-2.2.2/bin/ruby
The presence of the word "rvm" means you are using a version of Ruby installed by the RVM Ruby version manager. Note that this path name also shows you which Ruby version is now active. We discuss RVM later on. Note that the pathname may not match the one shown: look for "rvm" as part of the name to determine if you are using an RVM-managed Ruby.
Other names are possible as well, but these are the ones -- with variations as mentioned above -- you are most likely to see. The most likely variation is that you may have rbenv or RVM configured to install Ruby somewhere in your home directory.
To determine what version of Ruby is current, run:
$ ruby -v
To learn what the latest version of Ruby is, visit the Ruby download page. The "Stable" version with the highest version number is the latest version.
Besides the ruby
command, a Ruby installation contains a host of other files and tools, including:
irb
REPL (Read Evaluate Print Loop)
rake
utility: a tool to automate Ruby development tasks
gem
command: a tool to manage RubyGems
rdoc
and ri
)
In this chapter, we learned some very basic information about the system version of Ruby. Most importantly, we've pointed out that the system Ruby may not be suitable for development (on AWS Cloud9, this isn't a concern). We also briefly touched on what gets installed with Ruby.
Much of the development process with any programming language is about finding and fixing problems. Sometimes, though, these problems are more about the tools that you use to write, test, build, document, analyze, and distribute the software. To fix these tool-related problems, you need to know your development environment and how the various tools interact with that environment. Ruby is no exception.
In the coming chapters, we'll explore several commonly used Ruby tools and discuss how they work, how they interact with your system, and what you need to do when you have trouble.