rbenv: You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory

I have installed Hombrew .And have rbenv installed by brew install rbenv. But when i run gem install bundler ,it said : You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory. I want to install Bundler to manage gems, but it failed. Maybe something wrong with me ,can you help me ?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 29 (8 by maintainers)

Commits related to this issue

Most upvoted comments

First determine your goal. Is it

“I’m not sure I installed rbenv and a ruby version correctly” Yep, sure it can be tricky.

  1. sudo xcode-select --install
  2. Install rbenv with brew install rbenv
  3. Add eval "$(rbenv init -)" to the end of ~/.zshrc or ~/.bash_profile
  4. Install a ruby version rbenv install 2.3.0
  5. Select a ruby version by rbenv rbenv global 2.3.0
  6. Open a new terminal window
  7. Verify that the right gem folder is being used with gem env home (should report something in your user folder not system wide)

“I want to use bundler and rbenv together” Perfect match!

  1. Make sure you installed rbenv correctly ☝️
  2. Select a ruby version locally for the folder you are located in e.g. rbenv local 2.3.0 (this creates a file called .ruby-version in the directory)
  3. Verify that the right gem folder is being used with gem env home (should report something in your user folder not system wide)
  4. Run gem install bundler
  5. Run bundle install
  6. Run on of your favorite gems defined in the Gemfile by bundle exec ... e.g. bundle exec fastlane

“I want to install a gem on user for a specific version of ruby” Nice, rbenv is your friend.

  1. Make sure you installed rbenv correctly ☝️
  2. Select a ruby version globally by e.g. rbenv global 2.3.0 or even better locally for the folder you are located in e.g. rbenv local 2.3.0
  3. Run gem install ...

“I want to install a gem globally on my system ruby” Then why are you using rbenv? Okay, if you decide this is the right option for you then go ahead with sudo gem install ...

resolved the issue by trying with sudo that is “sudo gem install mysql2”

$ brew info rbenv | tail -7
==> Caveats
Rbenv stores data under ~/.rbenv by default. If you absolutely need to
store everything under Homebrew's prefix, include this in your profile:
  export RBENV_ROOT=/usr/local/var/rbenv

To enable shims and autocompletion, run this and follow the instructions:
  rbenv init
$ rbenv init
# Load rbenv automatically by appending
# the following to ~/.bash_profile:

eval "$(rbenv init -)"

Edit your ~/.bash_profile and put eval "$(rbenv init -)" at the end 😊

Just to add to what @jeremy and @mislav said:

After appending your ~/.bash_profile with export RBENV_ROOT=/usr/local/var/rbenv (Homebrew’s caveat), and then appending eval "$(rbenv init -)" to the bottom, be sure to $ exit your terminal and login again.

I had this same issue with permissions and couldn’t even wget rbenv-doctor until I appended ~/.bash_profile and hopped out and back in Terminal

Thanks!

@DavidLJ I’m not sure why.

@GuoZhiQiang OK that’s a good start. Now, type rbenv version and make sure that it’s not “system”. Then, do gem install bundler and it should work!

So for me, after MANY hours of trying to figure this out, switching to the new rbenv version (i.e. changing from system to the new version) did the trick.

$ rbenv versions
* system (set by /Users/apple/.rbenv/version)
  2.2.4
$ rbenv local 2.2.4
$ rbenv versions
  system
* 2.2.4 (set by /Users/apple/.ruby-version)

A similar situation On MacOS using bash as shell, the permission issue can happen because of BSD Ruby that ships with MacOS. If you install another Ruby (a newer one), using homebrew, for example, then you end up with two Rubies, but the BSD Ruby is owned by root and you can’t write to it.

Do NOT think a sudo will help.

You need to make sure when you call ruby or gem from the terminal, you’re getting the new Ruby, likely/usr/local/bin/ruby, and not the default /usr/bin/ruby.

You can try:

  • Running which ruby or which gem will show what version it is running (your’s or the system’s)
  • Running which -a ruby or which -a gem will show all versions your system currently possesses.
  • Ensuring /usr/local/bin comes before /usr/bin on your $PATH
    • To edit the PATH variable: find it on a bash init file, likely $HOME/.bashrc, and change its value.
    • To see what it is currently set to: echo $PATH
  • Restarting the terminal after trying other steps could also help
    • You can also run source $HOME/.bashrc
  • Running /usr/local/bin/ruby or /usr/local/bin/gem directly

@mislav @jeremy I have changed the Ruby from system to rbenv’s versions. The gem install bundler run successfully. Thanks !

Go ahead and put eval "$(rbenv init -)" at the end of your .bash_profile. If that file doesn’t exist in your home directory, create it. Then open a new shell and try the doctor again. Should be working!

Unfortunately, none of these solutions worked for me.

What ultimately worked? [REDACTED BY @mislav]

I don’t know that it’s the *right way to do it, but it unblocked me, so I’m happy with it for now.

Thanks everyone. Had this issue today and got to solve it following the instructions give here 👍

Setting rbenv version as global worked for me! Thanks @hfossli !

Can you run sudo xcode-select --install and try again? If it doesn’t work, please report the issue in the ruby-build repo. Thanks!