Installing gem fails with permissions error

42,158

Solution 1

Use a Ruby version manager, like RVM or rbenv.

I can't speak for rbenv, but RVM prepends a Ruby version-specific bin to your PATH that you have access to, so you don't have to use sudo (which is how you could install gems in your current situation, but is highly discouraged).

rbenv similarly prepends to your $PATH, but it uses a shim.

Solution 2

When I had this same error it was because I did not have a Ruby version manager installed, it defaulted to the Ruby version that came shipped with my computer which is owned root.

Since it is a very bad idea to use sudo to install gems, I installed RVM with Ruby 2.1.2

\curl -sSL https://get.rvm.io | bash -s stable --ruby=2.1.2

then made sure that it was being used as the default by running

rvm use ruby-2.1.2

RVM creates a new location to store your Ruby and gems with your user permissions so you don't have to use sudo or get a permissions error. The new gem location is:

~/.rvm/gems

Then carry on as you were...

gem install ....

Solution 3

I do not have RVM installed. I got this error because I installed rbenv but did not run rbenv init yet.

After I inserted the contents from the command rbenv init into .bash_profile, gem install works as expected.

I also happened to set rbenv global <ruby-version> to a downloaded Ruby version first.

Share:
42,158
Aniruddha
Author by

Aniruddha

Education: R.T.M Nagpur University: B.E in Information Technology University of Houston - Clear Lake: M.S in Computer Information Systems Work experience: Web Developer at Jackson Health System, Miami FL

Updated on March 07, 2020

Comments

  • Aniruddha
    Aniruddha about 4 years

    I updated my Mac OS to 10.9 Mavericks yesterday. My current Ruby version shows as

    ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]

    I got Rails installed on my machine and when I try to install gems I get the following message:

    Fetching: eventmachine-1.0.3.gem (100%)
    ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
    

    I am trying all this in my home directory. Any suggestions?

  • Aniruddha
    Aniruddha over 10 years
    I tried installing rvm but I am getting into permission issues. bash: line 704: /Users/aniruddhabarapatre1/.rvm/RELEASE: Permission denied
  • Stuart Nelson
    Stuart Nelson over 10 years
    what do you get when you enter ls -l ~/.rvm/RELEASE on the command line? Something is funny with your permissions.
  • Stuart Nelson
    Stuart Nelson over 10 years
    you would get something like -rw-r--r-- 1 aniruddhabarapatre1 staff 7 Oct 24 19:07 .rvm/RELEASE if you had access.
  • Aniruddha
    Aniruddha over 10 years
    Yes. This is something I'm getting. -rw-r--r-- 1 root staff 7 Sep 1 14:37 /Users/aniruddhabarapatre1/.rvm/RELEASE
  • Stuart Nelson
    Stuart Nelson over 10 years
    Ah yeah. You installed rvm as root (see how it says 'root' in there), so you don't have access to it. You need to remove the .rvm folder with sudo rm -rf .rvm, and then install rvm without sudo. It will then be owned by you, and not root.
  • Aniruddha
    Aniruddha over 10 years
    I tried above command and I still get following message - -rw-r--r-- 1 root staff 7 Sep 1 14:37 /Users/aniruddhabarapatre1/.rvm/RELEASE
  • Stuart Nelson
    Stuart Nelson over 10 years
    did the above command successfully remove .rvm? Make sure that folder is gone, log out of your terminal, log back in, and then the rvm install command: \curl -L https://get.rvm.io | bash -s stable
  • Aniruddha
    Aniruddha over 10 years
    Thanks a ton @Stuart. This helped me a lot. I could install rvm with your instructions and now will move ahead. Anything else that I should be careful about? Although I am still getting permission issues when I'm trying to install gems
  • Stuart Nelson
    Stuart Nelson over 10 years
    The last step is to use rvm. Type rvm install ruby, then make sure it is set to the default. You shouldn't be required to use sudo after that.
  • Aniruddha
    Aniruddha over 10 years
    Should I be doing this in my home directory? I have rails and rubyv2 installed on my machine, but installing gems still needs sudo to be prepended to it.
  • Stuart Nelson
    Stuart Nelson over 10 years
    rvm install ruby; rvm use ruby --default; if you have more questions post a new question or read the documention: rvm.io/rvm/basics
  • gamozzii
    gamozzii about 8 years
    this worked - note (on osx) for some reason 'ruby --version' command showed old version (2.2.0) even though latest was installed. 'rvm list' will show versions installed and available to be switched to. After switching (rvm use ruby-2.1.2) then 'ruby --version' showed correct (2.1.2) version.
  • PJP
    PJP about 7 years
    If the rbenv install directions are followed the initialization code will be installed automatically.