How to install gems without sudo on Mac OS X

不想你离开。 提交于 2019-12-06 05:22:24

问题


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.


回答1:


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"



回答2:


I would firstly recommend Homebrew, a popular choice among developers on OSX and it doesn't require sudo. You can do brew install ruby and go from there.

Also try sandboxed environments like RVM and rbenv, for the same reason.




回答3:


I think that the best solution to get working your gems running on all projects and local is to work with bundler and make sure to install running

bundle install --path vendor/bundle

That creates a vendor/bundle folder in your project and you should add to your .gitignore



来源:https://stackoverflow.com/questions/31414249/how-to-install-gems-without-sudo-on-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!