Skip to content

Instantly share code, notes, and snippets.

@stonehippo
Last active November 12, 2024 21:53
Show Gist options
  • Save stonehippo/cc0f3098516fb52390f1 to your computer and use it in GitHub Desktop.
Save stonehippo/cc0f3098516fb52390f1 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)

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

A word of warning: you will have to use Terminal to install this stuff. If you are uncomfortable with text, words, and doing stuff with your computer beyond pointing and hoping, this may not work well for you. But if that's the case, I'm not sure why you were trying to use Ruby in the first place.

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 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. 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 new Ruby. We'll start by installing rbenv and it's ruby-build plugin. rbenv is a Ruby version manager that lets you change with Ruby you're currently using, and ruby-build helps you install a custom Ruby. Seriously, when you're done with this, you'll be like some sort of new X-Men team member, with the mutant power to install new Rubies with a thought.

Let's start by installing 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: recent versions of MacOS ush zsh as the default shell. If you use a shell other than zsh, like bash, 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. (hint: the official rbenv install instructions can help here)

$ echo 'eval "$(~/.rbenv/bin/rbenv init - zsh)"' >> ~/.zshrc

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

$ source ~/.zshrc

Now it's time to install a new Ruby. We'll choose a fairly modern version of the 3.x family of Rubies. It's not as shiny as the latest Ruby 3.3 stuff, but maybe you're more familiar with this older version, like your favorite pair of socks. Or something.

$ rbenv install 3.0.6

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 3.0.6

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

$ gem install bundler

I did ask you to install Bundler because of my need to ensure your 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.

Learning more

Like Spider-Man, you have now been granted great power. And with great power comes the need to read some documentation.

Really, if you want to leverage Homebrew and rbenv–and you really should want to–go read their documentation. You'll learn how to find new things to install and other interesting facts, like why Unicorns vomit rainbows.

Ok, maybe you won't learn anything about that last bit. But you'll be able to do more with the expensive hunk of processed sand on your desk.

Good luck. And ping me on Mastodon (@[email protected]) or Bluesky (@its.stonehippo.com) if you need help, or if something here doesn't work. I wrote this in a few minutes to help out someone who needed to figure out installing Ruby, and I may have missed something, so beware and all that.


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

@AlwinaO
Copy link

AlwinaO commented Jun 9, 2018

This was very helpful. Thank you!!

@benmort
Copy link

benmort commented Jun 26, 2018

cheers

@ZhuofuChen
Copy link

Cheers!

@sydsrirak
Copy link

Wicked, thanks very much!

@rubyphull
Copy link

hi - I am installing Ruby following https://www.theodinproject.com/courses/web-development-101/lessons/your-first-rails-application

after running -
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

I see -
Checking for rbenv shims in PATH: not found --> how can I fix this?

@ashwinimanoj
Copy link

Thanks a lot!

@agness
Copy link

agness commented Dec 17, 2018

The homebrew install command should be updated to

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

Otherwise this is still the best resource around. Thanks for making.

@CamiloQuezadaDev
Copy link

I need your help guys!

rbenv: version ruby-2.5.3' is not installed (set by /Users/camiloquezadamaris/Documents/Cursos/rubyOnRails/blog/.ruby-version)`

@iammike
Copy link

iammike commented Feb 1, 2019

The homebrew install command should be updated to

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

Otherwise this is still the best resource around. Thanks for making.

ping! @stonehippo

@acarbonellb
Copy link

Nice one, thank you.

@vahotm
Copy link

vahotm commented Feb 22, 2019

ruby-build is now dependency for rbenv and installed automatically

@mberrien-fitzsimons
Copy link

Amazing. THANKS!

@edwardbattistini
Copy link

Such a great step by step tutorial ! Love the superhero references !!
Now that bash is going to be replaced by zsh would it be possible to have a tuto with those commands ? Or is there already one ?
Thanks

@stonehippo
Copy link
Author

@edwardbattistini yes, I’ll update this once I switch over to Catalina and zsh.

@stonehippo
Copy link
Author

stonehippo commented Oct 23, 2019

FYI, I’ve updated the Homebrew install script above to the latest version. I’ll probably do some other stuff to modernize this, too, as soon as I get a chance. (Thanks for the reminder, @iammike).

@orkoden
Copy link

orkoden commented Feb 6, 2020

If you use zsh use echo 'eval "$(rbenv init -)"' >> ~/.zshrc

@lisefrac
Copy link

I recently followed these instructions for getting rbenv set up on MacOS Catalina. I am doggedly still using bash instead of zsh, so that didn't cause me any problems. However, I did run into one problem: I could not run echo 'eval "$(rbenv init -)"' >> ~/.bash_profile, even if I tried to use sudo to run it as root.

I expect this is related to how locked down Catalina is in terms of permissions? Either way, I found a workaround. Since basically what that command is doing is added the eval portion to the end of your .bash_profile, I opened up .bash_profile in nano and added it manually. Did need to use sudo to edit .bash_profile, however.

So basically this is what I did:

  • ran sudo nano .bash_profile from inside my home directory
  • Once in nano, I added this line to the end of the file: eval "$(rbenv init -)"
  • Saved and exited with Ctrl-X, yes to save to the same file
  • once back in the shell, ran source ~/.bash_profile as indicated in your instructions (not 100% sure this was necessary)

After that, the rest of the instructions worked fine, and I now have more versions of Ruby than I probably need ;)

@lbdl
Copy link

lbdl commented Nov 19, 2020

@lisefrac you shouldn't need sudo in your own directory as it is yours or rather should be, ls -la might be useful here to check file ownership, also it is more common to use a .bashrc for your own config, you can of course use .bash_profile but its more idiomatic to not, there is some information on loading here: https://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/. TL;DR it doesn't really matter unless you are ssh'ing into remote systems, anyway just so long as it works!

@stonehippo
Copy link
Author

stonehippo commented Nov 19, 2020

@lisefrac it's possible that when you used sudo the first time, you created the .bash_profile with ownership by root. You can fix this with

sudo chown [your username] ~/.bash_profile

To @lbdl's point about .bash_profile and .bashrc: bash will only load .bashrc in sessions that are interactive and non-login, so it's not uncommon to load it in .bash_profile. If your profile is configured to do this (you'll see something like [[ -r ~/.bashrc ]] && . ~/.bashrc near the top), the putting the rbenv stuff in there should be more or less equivalent (it will ensure the it gets added to the environment under pretty much all login and terminal sessions).

@lisefrac
Copy link

it's possible that when you used sudo the first time, you created the .bash_profile with ownership by root. You can fix this with

That was the problem, @stonehippo. Thanks!

@stonehippo
Copy link
Author

@lisefrac glad to hear it!

@Mihoid
Copy link

Mihoid commented Dec 14, 2020

source ~/.bash_profile
Thank you! This is goal 🔥

@Carlito-Pedida
Copy link

This is very helpful Thank you!!!

@flavioltonon
Copy link

Amazing guide! Thanks for the contribution!

@campbellyule
Copy link

Homebrew URL returns 404?

@stonehippo
Copy link
Author

Homebrew URL returns 404?

Sorry @campbellyule which do you mean? The link to the main site in the third paragraph worked when I tested it just now. Are you referring to the homebrew install script link in the first code excerpt?

@stonehippo
Copy link
Author

Ah, I think fixed it, @campbellyule. The path in the install script link referred to the retired "master" branch. Let me know if this resolves the issue.

@Lancerrivas
Copy link

Ty Men, really support me to find the way, cool men!

@campbellyule
Copy link

Ah, I think fixed it, @campbellyule. The path in the install script link referred to the retired "master" branch. Let me know if this resolves the issue.

Yes sorted - thanks

@sftskllnr
Copy link

Thanks bro!

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