May 16, 2017

Why you shouldn't use the System Ruby

When you are getting started with Ruby development, and possibly using a *nix system for the first time, it will seem great that Ruby is already installed there for you. It's just right there, ready to use and get going, right?

Unfortunately, it's not the Ruby that you should be using. It will be out of date by the time you even get the operating system most likely, upgrading it would mean that you have to mess with the guts of your computer's operating system and then even if you do you will likely need to install gems for system wide use with sudo.

But what's wrong with this?

If you attempt to upgrade your system ruby, you are going to be moving from one version of it to another. There is a very good chance that there are programs on your computer that rely on this version of the system ruby setup the way that it is. This is not the only problem you are likely to face. There is also all the gems that you need. Any time you use the system ruby, you are likely going to need to install your gems like this:

> sudo gem install GEM_NAME

This means that the gem that you are installing will be installed by the root user of your computer, giving it license to run as that user potentially. Do you read and understand all the code in Ruby gems that you install all of the time??? I know I don't, so you want to limit the reach of these gems to only the things that they need to be able to run and access, your application.

More than one Ruby

And now the main reason you want to avoid your system ruby. You are going to need to run more than one of them. As you work on more projects, there is likely going to be a time when you need to come back to a project that uses an older version of ruby than the one you are using now. You aren't going to stop working on the other project, and you don't want to hamstring a new one by hanging onto an older version of Ruby because it came with your computer. Wouldn't it be great if you could have multiple versions of Ruby that are available to you, installed under your user account that you can switch between as needed?

There's an app for that

Luckily the multiple version of Ruby future is the world that we live in now. There are several different pieces of open source software available to you that allow you to do this, the most notable being Ruby Version Manager (RVM) and Rbenv. I have used RVM in the past, but after having some issues (that are likely not a problem anymore, this was a few years back) I have moved to using Rbenv, so I am going to go through how to get your personal Ruby development environment up and running using Rbenv.

Install Rbenv

There is great detail on the Rbenv Github Page about how it works and how to install it, and I will quickly go over the installation steps from there here. First, you want to switch to your home directory so that you can install the rbenv software:

> cd

Once you are there, you want to clone the repository:

> git clone https://github.com/rbenv/rbenv.git .rbenv

Once your clone has finished, you have most of what you need. Now you need to run a command:

> ~/.rbenv/bin/rbenv init

to get specific instructions on how to initialize rbenv for your shell. Once you have done that you need to run:

> type rbenv
#=> "rbenv is a function"

And rbenv is setup and ready to go.

Ruby build

Now that you have rbenv we have an optional step, but one that I recommend. Installing Ruby build. This will help simplify the installation of new Ruby versions by providing the rbenv install command:

> rbenv install RUBY_VERSION

The easiest way to do this is to install Ruby build as an rbenv plugin:

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

This will give you the latest version. Whenever a new Ruby is released, you will just need to do a git pull on the ruby build plugin, and you will then be able to install a new Ruby.

Install a Ruby

Now that you have rbenv and ruby-build, you are ready to install whatever version of Ruby you need. For me this week it was 2.3.3, so all I needed to do to get that was:

> rbenv install 2.3.3
ruby-build: use openssl from homebrew
Downloading ruby-2.3.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.3.tar.bz2
Installing ruby-2.3.3...
ruby-build: use readline from homebrew
Installed ruby-2.3.3 to /Users/chris/.rbenv/versions/2.3.3

To start using it, all you have to do is:

> rbenv local 2.3.3

> ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin16]

Very simple. Now all we need is bundler to get going with our ruby version and your ruby app:

> gem install bundler
Fetching: bundler-1.14.6.gem (100%)
Successfully installed bundler-1.14.6
Parsing documentation for bundler-1.14.6
Installing ri documentation for bundler-1.14.6
Done installing documentation for bundler after 4 seconds
1 gem installed

> rbenv rehash

Done. We run rbenv rehash after installing a gem that includes executables as it will update what executables are available to Ruby via rbenv.

All Done

Great news, you now have a working ruby development environment on your computer, capable of running more that one version of ruby so you can switch between them as you need to. And because all of this is install into your home directory, if you ever make a mistake and get your ruby versions messed up and into a state that you can't predict or repair, you can delete everything under ~/.rbenv and just start again by installing the ruby version(s) you need and bundler and getting going again, all without causing any harm to your operating system or the software that depends on it.

I hope this helps you get moving with your Ruby environment. If you have any questions about it or any problems, please feel free to send me an email, or a message on Twitter, I'd love to help if you need it.