Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user)

People also ask

How do I change Ruby version on Mac?

Set Ruby version with rvm on Mac 0 on the command line. To switch to the system ruby, enter rvm use system . To switch back to the default, rvm default . The command rvm list will show all installed Rubies, including the current and default versions.

How do I know what version of Ruby I have Mac?

How to check Ruby version on Mac. Don't type the $ character. The $ character is just a cue that you should enter a shell command. The ruby -v command will show you the Ruby version number.

What is Library Ruby Gems?

RubyGems is a Ruby packaging system designed to facilitate the creation, sharing and installation of libraries (in some ways, it is a distribution packaging system similar to, say, apt-get , but targeted at Ruby software).


Update:

  • I now have a very detailed step-by-step tutorial for setting up a proper Ruby development environment on a Mac, including a Homebrew troubleshooting section with the most common issues people run into.
  • Although my step-by-step tutorial is guaranteed to work, I also have a script that will save you a lot of time.
  • My tutorial uses a Ruby manager to install Ruby (chruby specifically) because that's the only way I recommend that you install Ruby. To learn more, read my guide that explains the various ways you can install Ruby gems on a Mac.

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? I wrote a detailed article that shows why you should never use sudo to install gems.

The solution involves two main steps:

  1. Install a separate version of Ruby that does not interfere with the one that came with your Mac.
  2. 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.

There are several ways to install Ruby on a Mac. The best way that I recommend, and that I wish was more prevalent in the various installation instructions out there, is to use an automated script (like the one I wrote and linked to above) that will set up a proper Ruby environment for you. This drastically reduces the chances of running into an error due to inadequate instructions that make the user do a bunch of stuff manually and leaving it up to them to figure out all the necessary steps.

The other route you can take is to spend extra time doing everything manually and hoping for the best. 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:

  • chruby and ruby-install - my personal recommendations and the ones that are automatically installed by my script. These can be installed with Homebrew:
brew install chruby ruby-install
  • rbenv - can be installed with Homebrew

  • RVM

  • asdf

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

Whether you need to configure .zshrc or .bash_profile depends on which shell you are 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)

  • 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/2.7.0/bin:$PATH"' >> ~/.zshrc

The 2.7.0 in the command above assumes Homebrew installed a Ruby version that starts with 2.7. If you're using a different version (which you can check with ruby -v), replace 2.7 with the first two digits of your Ruby version.

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

To check that you're now using the non-system version of Ruby, you can run the following commands:

which ruby

It should be something other than /usr/bin/ruby

ruby -v

It should be something other than 2.6.3 if you're on macOS Catalina. As of today, 3.0.1 is the latest Ruby version.

Once you have this new version of Ruby installed, you can now install bundler (or any other gem):

gem install bundler

Worked for me using the parameter --user-install running following command:

gem install name_of_gem --user-install

Edit

There was one gem I still could not install (it required the Ruby.h headers of the Ruby development kit or something), then I tried the different version managers, but somehow that still did not really work as it was stated in the documentations how to just install and switch (it did just not switch the versions). Then I removed all the installed version managers and installed afterwards with brew install ruby the latest version and did set the PATH variable, too. (It will be mentioned after the installation of ruby from brew), which worked.


If you don't want to run sudo then install ruby using homebrew

brew install ruby
export GEM_HOME="$HOME/.gem"
gem install rails

You may want to add export GEM_HOME="$HOME/.gem" to your ~/.bash_profile or .zshrc if you're using zsh

Note: RubyGems keeps old versions of gems, so feel free to do some cleaning after updating:

gem cleanup

Just export GEM_HOME:

export GEM_HOME="$HOME/.gem"

And then try:

gem install cocoapods

As @idleberg mentions, on Mac OS, it is best to install rbenv to avoid permissions errors when using manually installed ruby.

Installation

$ brew update
$ brew install rbenv

Add the following in .bashrc file:

eval "$(rbenv init -)"

Now, we can look at the list of ruby versions available for install

$ rbenv install -l

Install version 2.3.8 for example

$ rbenv install 2.3.8

Now we can use this ruby version globally

$ rbenv global 2.3.8

Finally run

$ rbenv rehash
$ which ruby
/Users/myuser/.rbenv/shims/ruby
$ ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [x86_64-darwin17]

Go for it

Now install bundler

$ gem install bundler

All done!


I have faced same issue after install macOS Catalina. I had try below command and its working.

sudo gem update

It's generally recommended to use a version manager like rbenv or rvm. Otherwise, installed Gems will be available as root for other users.

If you know what you're doing, you can use sudo gem install.