Bundler cannot install any gems without sudo

荒凉一梦 提交于 2019-11-28 17:13:23
Nakilon

You may host gems in your user home folder, that does not need root permissions:

bundle install --path ~/.gem

To avoid passing this parameter manually add export GEM_HOME=$HOME/.gem to your .bash_profile -- this solves sudo issue on Mac OS and other *nix systems. You then also might need to have access to gems that provide executables (such as bundler), so add this too:

PATH=$PATH:$HOME/.gem/bin

or in some cases:

PATH=$PATH:$HOME/.gem/ruby/<version>/bin

ref: https://stackoverflow.com/a/5862327/322020


UPD: But keep in mind that if you start using rbenv having this environment variable be the same might cause problems when using too different versions of Ruby, so you might want to temporary unset GEM_HOME or prepend custom one each time you launch rbenv-ed Ruby.

Matthew Clark

Your RVM gem directory should be owned by the rvm group. So, instead of changing ownership, it might be wise to simply add the user to the rvm group:

# $(whoami) evaluates to your username
# You may want to change this to a different username depending on your config
# but $(whoami) is a passable default
usermod -a -G rvm $(whoami)

This is due to the way you installed ruby.

Frankly, it works *just fine* if you don't mind the sudo. At the end of the day, it's just your laptop... Not some server running in a bank.

If you really care, chown gem folders as needed.

I had this happen today. This might be a unique situation, but I had copied a Rails source tree from a system that had RVM installed globally (system-wide in /usr/local/rvm), to a system that just had RVM installed per-user (~/.rvm).

I was trying to do bundle install and getting the "Your user account isn't allowed to install to the system Rubygems." error. After a lot of poking around, I noticed that in my ~/.rvm directory there was a symbolic link:

~/.rvm/gems/ruby-2.1.1/cache -> /usr/local/rvm/gems/cache

Removing that symlink got bundle install working again without sudo.

I had the same problem and found out that Bundler before installing new gems checks if it has write permission for all files found $GEM_HOME/build_info. In my case it didn't, because although user that run bundler was in 'rvm' user group and that group owned all those files, group wasn't allowed to write some of them.

That happened because I installed some of the gems under root, which has umask 0022 (all files created by root, can't be written by group) instead of umask 0002 that others have and which rvm expects.

If you are using RVM, then do these two steps and you'll be golden

  1. Make sure your user belongs to the RVM group

    sudo usermod -a -G rvm myUserName

  2. Make sure build_info is writable for all users in the RVM group

    sudo chmod 664 $GEM_HOME/build_info/*

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