Search code examples
rubyosx-elcapitanrbenv

How to install gems without sudo on Mac OS X


I've read numerous posts but none so far has done the trick.

How can I, on OSX (El Capitan Beta) install a gem for my own user? Posts suggest specifying -user-install for example but I cannot call gem without sudo at all as it throws permission errors.

I followed "Install gem as user on OSX 10.10 Yosemite" but still cannot call gem.

I installed Homebrew, installed rbenv per the above guide, installed a version of Ruby with rbenv, checked it was loaded correctly, but the final step in the guide is to call gem install which I still cannot do.

Calling gem after installing rbenv results in:

/Users/Me/.rbenv/versions/2.1.5/lib/ruby/2.1.0/rubygems/stub_specification.rb:71:in `initialize': 
Permission denied @ rb_sysopen - /Users/Ne/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/speci

rbenv env dump:

RBENV_VERSION=2.1.5
RBENV_ROOT=/Users/Me/.rbenv
RBENV_HOOK_PATH=:/Users/Me/.rbenv/rbenv.d:
  /usr/local/etc/rbenv.d:
  /etc/rbenv.d:
  /usr/lib/rbenv/hooks
PATH=/Users/Me/.rbenv/versions/2.1.5/bin:
  /usr/local/Cellar/rbenv/0.4.0/libexec:
  /Users/Me/.rbenv/plugins/rbenv-env/bin:
  /Users/Me/.rbenv/shims:
  /usr/local/heroku/bin:
  /usr/local/sbin:
  /usr/local/bin:
  /usr/local/share/npm/bin:
  /Applications/Atom.app/Contents/Resources/app/apm/bin:
  /Users/Me/.bin:
  /Applications/Atom.app/Contents/Resources/app/apm/node_modules/atom-package-manager/bin:
  /usr/local/bin:/usr/bin:
  /bin:
  /usr/sbin:/sbin:
  /opt/X11/bin:
  /usr/local/MacGPG2/bin
RBENV_DIR=/Users/Me/.rbenv/plugins

"brew and gem both throws 'Permission denied' error whenever I run it" seems related though it was not solved except by using some other method which I may have to do.


Solution

  • If you can't use gem without sudo, it sounds like you haven't initialized rbenv correctly because the shims aren't available.

    These steps are from the documentation. Confirm you did them ALL:

    Add ~/.rbenv/bin to your $PATH for access to the rbenv command-line utility.

    $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
    

    Ubuntu Desktop note: Modify your ~/.bashrc instead of ~/.bash_profile.

    Zsh note: Modify your ~/.zshrc file instead of ~/.bash_profile.

    Add rbenv init to your shell to enable shims and autocompletion.

    $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
    

    Same as in previous step, use ~/.bashrc on Ubuntu, or ~/.zshrc for Zsh.

    Restart your shell so that PATH changes take effect. (Opening a new terminal tab will usually do it.) Now check if rbenv was set up:

    $ type rbenv
    #=> "rbenv is a function"