Why do I get a "permission denied" error while installing a gem?
Asked Answered
B

10

79

I'm attempting to install Jekyll. After running gem install jekyll I get this error:

ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied - /usr/local/lib/ruby/gems/2.0.0/gems/jekyll-1.0.3/CONTRIBUTING.md

I can see that Jekyll is installed when I run gem list so I'm thoroughly confused:

*** LOCAL GEMS ***

bigdecimal (1.2.0)
classifier (1.3.3)
colorator (0.1)
commander (4.1.3)
directory_watcher (1.4.1)
fast-stemmer (1.0.2)
highline (1.6.19)
io-console (0.4.2)
jekyll (1.0.3)
json (1.7.7)
kramdown (1.0.2)
liquid (2.5.0)
maruku (0.6.1)
minitest (4.3.2)
posix-spawn (0.3.6)
psych (2.0.0)
pygments.rb (0.5.1)
rake (0.9.6)
rdoc (4.0.0)
rubygems-update (2.0.3)
safe_yaml (0.7.1)
syntax (1.0.0)
test-unit (2.0.0.0)
yajl-ruby (1.1.0)

I've had a lot of problems with my user paths in the past, so I'm wondering if this error could have something to do with that?

Here is the output of gem env:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.0.3
  - RUBY VERSION: 2.0.0 (2013-02-24 patchlevel 0) [x86_64-darwin12.3.0]
  - INSTALLATION DIRECTORY: /usr/local/Cellar/ruby/2.0.0-p0/lib/ruby/gems/2.0.0
  - RUBY EXECUTABLE: /usr/local/Cellar/ruby/2.0.0-p0/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/Cellar/ruby/2.0.0-p0/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-12
  - GEM PATHS:
     - /usr/local/Cellar/ruby/2.0.0-p0/lib/ruby/gems/2.0.0
     - /Users/me/.gem/ruby/2.0.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/

Here is my ".bash_profile":

export PATH="/usr/local/bin:/usr/local/sbin:~/bin:$PATH"

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
PATH=$PATH:above/path/to/gems

source ~/.rvm/scripts/rvm

Could somebody please help me get Jekyll installed, or at least get past this permissions error?

Bushhammer answered 9/7, 2013 at 14:29 Comment(1)
#28846665 is the life saver.Isometrics
U
87

Your Ruby is installed in /usr/local/Cellar/ruby/....

That is a restricted path and can only be written to when you use elevated privileges, either by running as root or by using sudo. I won't recommend you run things as root since you don't understand how paths and permissions work. You can use sudo gem install jekyll, which will temporarily elevate your permissions, giving your command the rights needed to write to that directory.

However, I'd recommend you give serious thought into NOT doing that, and instead use your RVM to install Ruby into your own home directory, where you'll automatically be able to install Rubies and gems without permission issues. See the directions for installing into a local RVM sandbox in "Single-User installations".

Because you have RVM in your ~/.bash_profile, but it doesn't show up in your Gem environment listing, I suspect you either haven't followed the directions for installing RVM correctly, or you haven't used the all-important command:

rvm use 2.0.0 --default

to configure a default Ruby.

For most users, the "Single-User installation" is the way to go. If you have to use sudo with that configuration you've done something wrong.

Umbilicus answered 9/7, 2013 at 15:51 Comment(2)
Thank you for making that so clear. I've gone back through and installed RVM. Everything is working perfectly.Bushhammer
I found your explanation and especially your concluding paragraph most helpful.Undecagon
P
67

I wanted to share the steps that I followed that fixed this issue for me in the hopes that it can help someone else (and also as a reminder for me in case something like this happens again)

The issues I'd been having (which were the same as OP's) may have to do with using homebrew to install Ruby.

To fix this, first I updated homebrew:

brew update && brew upgrade
brew doctor

(If brew doctor comes up with any issues, fix them first.) Then I uninstalled ruby

brew uninstall ruby

If rbenv is NOT installed at this point, then

brew install rbenv
brew install ruby-build
echo 'export RBENV_ROOT=/usr/local/var/rbenv' >> ~/.bash_profile
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile

Then I used rbenv to install ruby. First, find the desired version:

rbenv install -l

Install that version (e.g. 2.2.2)

rbenv install 2.2.2

Then set the global version to the desired ruby version:

rbenv global 2.2.2

At this point you should see the desired version set for the following commands:

rbenv versions

and

ruby --version

Now you should be able to install bundler:

gem install bundler

And once in the desired project folder, you can install all the required gems:

bundle
bundle install
Polarization answered 6/7, 2015 at 15:56 Comment(5)
So now we have an RVM and an rbenv answer. Now we just need one more with chruby which many people are switching to. For more information, there's a Ruby Rogues episode comparing ruby version managers.Doubletalk
Good Job and thank you. rbenv repo link if anyone needs it github.com/sstephenson/rbenvCadmarr
What is the reason for setting echo 'export RBENV_ROOT=/usr/local/var/rbenv' >> ~/.bash_profile instead of just using default ~/.rbenv ?Markettamarkey
I am having this Error While doing brew update. Error: /usr/local must be writable!Rorke
This is the answer that helped me.Bite
T
46

Seems like a permissions issue. This is what worked for me

sudo chown -R $(whoami) /Library/Ruby/Gems/*

or in your case

sudo chown -R $(whoami) /usr/local/lib/ruby/gems/2.0.0/gems/

What does this do:

This is telling the system to change the files to change the ownership to the current user. Something must have gotten messed up when something got installed. Usually this is because there are multiple accounts or users are using sudo to install when they should not always have to.

Tini answered 4/5, 2018 at 18:34 Comment(2)
It was a permissions issue for us as well, while trying gem update —system but we had to go through the /usr/local/rvm/rubies directory tree carefully and ensure group permissions were set correctly.Tm
Thank you for your post. This was my issue. The problem is, once used sudo to install a gem, it breaks permissions on the folders and won't let you install any other.Greaseball
T
10

After setting the gems directory to the user directory that runs the gem install, using export GEM_HOME=/home/<user>/gems, the issue has been solved.

Television answered 13/1, 2017 at 10:10 Comment(0)
B
5

I think the problem happened when you use rbenv. Try the below commands to fix it.

rbenv shell {rb_version}
rbenv global {rb_version}
or 
rbenv local {rb_version}
Bondswoman answered 21/9, 2019 at 16:32 Comment(1)
rbenv shell works for me.Fermi
S
3

I had the same problem using rvm on Ubuntu, was fixed by setting the source on my terminal as a short-term solution:

source $HOME/.rvm/scripts/rvm

or

source /home/$USER/.rvm/scripts/rvm

and configure a default Ruby Version, 2.3.3 in my case.

rvm use 2.3.3 --default


And a long-term Solution is to add your source to your .bashrc file to permanently make Ubuntu look in .rvm for all the Ruby files.

Add:

source .rvm/scripts/rvm

into

$HOME/.bashrc file.

Shearwater answered 22/11, 2017 at 12:44 Comment(0)
U
0

Install rbenv or rvm as your Ruby version manager (I prefer rbenv) via homebrew (ie. brew update & brew install rbenv) but then for example in rbenv's case make sure to add rbenv to your $PATH as instructed here and here.

For a deeper explanation on how rbenv works I recommend this.

Unlimited answered 14/5, 2017 at 4:39 Comment(0)
P
0

Run the following command to reset permissions to user/local:

sudo chown -R $(whoami):admin /usr/local/* \
&& sudo chmod -R g+rwx /usr/local/*

Feel free to feedback if this is working for you!

Pyretotherapy answered 16/5, 2022 at 10:29 Comment(0)
B
0

Mac user: brew sometimes is not on a default branch, what worked for me:

git -C $(brew --repo homebrew/cask) checkout master

git pull
Batiste answered 9/8, 2023 at 16:29 Comment(0)
B
-1

If you are working in Windows Environment, I recommend running command prompt with administrator.

Biome answered 27/1, 2022 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.