Skip to content

Instantly share code, notes, and snippets.

@drunkrhin0
Forked from barnett/install_ruby_with_rbenv.md
Last active October 4, 2023 07:46
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 drunkrhin0/cbb1837561874e2517b4f8f8385bb44e to your computer and use it in GitHub Desktop.
Save drunkrhin0/cbb1837561874e2517b4f8f8385bb44e 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

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.

Install Homebrew

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.*

Homebrew (or just brew, since that's the name of the command you'll use), is a package manager for Mac OS like apt for Linux. It makes installing all sorts of cool tools easy.

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

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

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 that's done, your system will be all set up to use Homebrew, and its superhappyfuncommand, brew. Let's try it out by installing a helpful little utility, tree.

$ brew install tree

Once you do that, run the tree utility like this:

$ tree

You should get a pretty ASCII picture of your current directory structure.

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.

Install rbenv and a new Ruby

rbenv is a Ruby version manager that lets you change with Ruby you're currently using. ruby-build helps you install a custom Ruby.

$ brew install rbenv ruby-build

WARNING: Most macs use ZSH now but I've included the bash instructions just in case.

Bash: $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile ZSH: $ echo 'eval "$(rbenv init -)"' >> ~/.zshrc

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

Bash: $ source ~/.bash_profile ZSH: $ source ~/.zshrc

Now it's time to install a new Ruby. We'll choose the most recent version of the Ruby, 3.2.2. You can check the latest version here

$ rbenv install 3.2.2

Then run the following as instructed.

$ rbenv global 3.2.2

Installing Gems

If you encounter You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory. while trying to install a gem try the following:

$ echo 'export PATH="/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/2.6.0/bin:$PATH"' >> ~/.zshrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment