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