You are correct that macOS won't let you change anything with the Ruby version that comes installed with your Mac. However, it's possible to install gems like bundler
using a separate version of Ruby that doesn't interfere with the one provided by Apple.
Using sudo
to install gems, or changing permissions of system files and directories is strongly discouraged, even if you know what you are doing. Can we please stop providing this bad advice?
The solution involves two main steps:
- Install a separate version of Ruby that does not interfere with the one that came with your Mac.
- Update your
PATH
such that the location of the new Ruby version is first in the PATH
. Some tools do this automatically for you. If you're not familiar with the PATH
and how it works, it's one of the basics that you should learn, and you'll understand why you sometimes get "command not found" errors and how to fix them.
First, you will want to install Homebrew, which installs the prerequisite command line tools, and makes it easy to install other necessary tools.
Then, the two easiest ways to install a separate version of Ruby are:
If you would like the flexibility of easily switching between many Ruby versions [RECOMMENDED]
Choose one of these four options:
brew install chruby ruby-install
If you chose chruby
and ruby-install
, you can then install the latest Ruby like this:
ruby-install ruby
Once you've installed everything and configured your .zshrc
or .bash_profile
according to the instructions from the tools above, quit and restart Terminal, then switch to the version of Ruby that you want. In the case of chruby
, it would be something like this:
chruby 3.1.3
Whether you need to configure .zshrc
or .bash_profile
depends on which shell you're using.
If you know for sure you don't need more than one version of Ruby at the same time (besides the one that came with macOS) [NOT RECOMMENDED]
Even if you think you won't need another version now, you will eventually and you won't be able to easily switch. This will cause confusion and headaches, which is why I don't recommend installing and managing Ruby with Homebrew.
If you choose to use Homebrew to install Ruby despite my warnings, you'll be on your own if you run into any issues.
- Install ruby with Homebrew:
brew install ruby
Then update your PATH
by running this command:
echo 'export PATH="/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/3.1.0/bin:$PATH"' >> ~/.zshrc
The 3.1.0
in the command above assumes Homebrew installed a Ruby version that starts with 3.1
. If it installed a different version, replace 3.1
with the first two digits of your Ruby version.
If you're on an M1/M2 Mac, replace /usr/local
with /opt/homebrew
Then "refresh" your shell for these changes to take effect:
source ~/.zshrc
Or you can open a new terminal tab, or quit and restart Terminal.
Replace .zshrc
with .bash_profile
if you are using Bash. If you're not sure, read my guide to find out which shell you're using.
To check that you're now using the non-system version of Ruby, you can run the following commands:
which ruby
It should not be /usr/bin/ruby
ruby -v
It should be 3.1.3 or later.
Once you have this new version of Ruby installed, you can now install bundler (or any other gem):
gem install bundler
sudo chown -R $USER /Library/Ruby/Gems/
– Witerbenv global 3.1.2
then it will work. – Norby