Skip to content

Instantly share code, notes, and snippets.

@agness
Forked from stonehippo/install_ruby_with_rbenv.md
Last active September 23, 2020 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agness/eeb01dc02e6ac7ae4a5c392b693f10a2 to your computer and use it in GitHub Desktop.
Save agness/eeb01dc02e6ac7ae4a5c392b693f10a2 to your computer and use it in GitHub Desktop.
Installing a new Ruby with rbenv on Mac OS

Install a new Ruby with rbenv on Mac OS (and make yourself a superhero)

Modified from the original at stonehippo/install_ruby_with_rbenv.md

If you're doing stuff with Ruby on a Mac, e.g. installling Jekyll or something, by default you'll end up having to use the sudo command to do stuff, since the permission to modify the default config is not available to your user account.

This sucks and should be avoided. Here's how to fix that.

Installing a new Ruby

To make this better, we are going install a new, custom Ruby. This used to be a big, scary thing, but thanks to the awesome tools Homebrew and rbenv, it's a snap.*

We'll be using Terminal to install this stuff.

First, install Homebrew

Homebrew (or just brew, since that's the name of the command you'll use), is a package manager for Mac OS. It makes installing all sorts of cool software libraries (and their various versions) very easy.

Open terminal and command your Mac to do your bidding with the following command:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)

This little bit of magic will download the Homebrew install script and walk you through the install process. It's not too scary, and should only take a couple of minutes. Accept all defaults.

Once you have Homebrew, your powers are much greater and you can install all sorts of stuff without ever having to invoke sudo or mess with permissions. And you'll have harnessed the work of thousands of developers who are working in caves and darkend alcoves crafting Homebrew install packages just for you.

Second, install rbenv and a new Ruby

Ok, Homebrew is installed and you now have some new super powers. Let's use them to get you a specific version of Ruby (as opposed to whatever default your OS shipped with.) We'll start by installing rbenv and it's ruby-build plugin. rbenv is a Ruby version manager that lets you toggle between versions of Ruby you're currently using, and ruby-build helps you install whatever set of versions you want on hand. When you're done with this, you'll have the superpower to install/switch between different Rubies versions with a mere thought.

Let's start by install the tools:

$ brew install rbenv ruby-build

Assuming that all went to plan, you have just upgraded yourself and your Mac. But you can't quite use your new capabilities yet. You need to hook up rbenv so it can do some stuff. If you want to know what that stuff is, good for you, go read the documentation. For now, here's what you need to do:

WARNING: If you use a shell other than bash, like zsh, you'll need a slightly different command set up the rbenv environment stuff. If you don't know what I'm talking about, you're probably fine; if you do, then you can figure this out on your own.

$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

To make this apply to your current Terminal session, do this:

$ source ~/.bash_profile

Now it's time to install a new Ruby. We'll choose a fairly modern version of the 2.3 family of Rubies. It's not as shiny as the latest Ruby 2.7 stuff, but it's more familiar to us at PP.

$ rbenv install 2.3.2

That'll take a few minutes. Once it's done, you have a shiny new toy. But to play with it, you need to do a couple of other things. First, make the new Ruby the default for your system:

$ rbenv global 2.3.2

After that, you should install the most useful too for Ruby, Bundler. I am asking you to do this not because it's required, but because I care deeply and profound about you. Really, I do.

$ gem install bundler

I did ask you to install Bundler because of my need to ensure you wellbeing. I also did it to demontrate that, if all went well, you've accomplised the task this little guide was supposed to help you do, installing a new Ruby on your Mac that you have control over, a Ruby sans sudo.

Please enjoy your new Ruby responsibly.


*Ok, maybe not a snap. But it won't hurt too bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment