Can't install gems on OS X “El Capitan”

2019-01-03 04:04发布

I am not able to install and run fakes3 gem on El Capitan Beta 5.

I tried:

sudo gem install fakes3
ERROR:  While executing gem ... (Errno::EPERM)
    Operation not permitted - /usr/bin/fakes3

Then I tried doing it the cocoapods way. It worked for cocoapods but not for fakes3.

mkdir -p $HOME/Software/ruby
export GEM_HOME=$HOME/Software/ruby
gem install cocoapods
[...]
1 gem installed
gem install fakes3
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

14条回答
一纸荒年 Trace。
2楼-- · 2019-01-03 04:12

Disclaimer: @theTinMan and other Ruby developers often point out not to use sudo when installing gems and point to things like RVM. That's absolutely true when doing Ruby development. Go ahead and use that.

However, many of us just want some binary that happens to be distributed as a gem (e.g. fakes3, cocoapods, xcpretty …). I definitely don't want to bother with managing a separate ruby. Here are your quicker options:

Option 1: Keep using sudo

Using sudo is probably fine if you want these tools to be installed globally.

The problem is that these binaries are installed into /usr/bin, which is off-limits since El Capitan. However, you can install them into /usr/local/bin instead. That's where Homebrew install its stuff, so it probably exists already.

sudo gem install fakes3 -n/usr/local/bin

Gems will be installed into /usr/local/bin and every user on your system can use them if it's in their PATH.

Option 2: Install in your home directory (without sudo)

The following will install gems in ~/.gem and put binaries in ~/bin (which you should then add to your PATH).

gem install fakes3 --user-install -n~/bin

Make it the default

Either way, you can add these parameters to your ~/.gemrc so you don't have to remember them:

gem: -n/usr/local/bin

i.e. echo "gem: -n/usr/local/bin" >> ~/.gemrc

or

gem: --user-install -n~/bin

i.e. echo "gem: --user-install -n~/bin" >> ~/.gemrc

(Tip: You can also throw in --no-document to skip generating Ruby developer documentation.)

查看更多
干净又极端
3楼-- · 2019-01-03 04:14

As it have been said, the issue comes from a security function of Mac OSX since "El Capitan".

Using the default system Ruby, the install process happens in the /Library/Ruby/Gems/2.0.0 directory which is not available to the user and gives the error.

You can have a look to your Ruby environments parameters with the command

$ gem env

There is an INSTALLATION DIRECTORY and a USER INSTALLATION DIRECTORY. To use the user installation directory instead of the default installation directory, you can use --user-install parameter instead as using sudo which is never a recommanded way of doing.

$ gem install myGemName --user-install

There should not be any rights issue anymore in the process. The gems are then installed in the user directory : ~/.gem/Ruby/2.0.0/bin

But to make the installed gems available, this directory should be available in your path. According to the Ruby’s faq, you can add the following line to your ~/.bash_profile or ~/.bashrc

if which ruby >/dev/null && which gem >/dev/null; then
    PATH="$(ruby -rubygems -e 'puts Gem.user_dir')/bin:$PATH"
fi

Then close and reload your terminal or reload your .bash_profile or .bashrc (. ~/.bash_profile)

查看更多
4楼-- · 2019-01-03 04:14

This is the solution that I have used:

Note: this fix is for compass as I wrote it on another SO question, but I have used the same process to restore functionality to all terminal processes, obviously the gems you are installing are different, but the process is the same.

I had the same issue. It is due to Apple implementing System Integrity Protection (SIP). You have to first disable that...

Reboot in recovery mode:

Reboot and hold Command + R until you see the apple logo.

Once booted select Utilities > Terminal from top bar.

type: csrutil disable

then type: reboot

Once rebooted

Open terminal back up and enter the commands:

sudo gem uninstall bundler

sudo gem install bundler

sudo gem install compass

sudo gem install sass

sudo gem update --system

The the individual gems that failed need to be fixed, so for each do the following:

On my machine this was the first dependency not working so I listed it:

sudo gem pristine ffi --version 1.9.3

Proceed through the list of gems that need to be repaired. In all you are looking at about 10 minutes to fix it, but you will have terminal commands for compass working.

Screenshot

查看更多
Fickle 薄情
5楼-- · 2019-01-03 04:15
sudo chown -R $(whoami):admin /usr/local

That will give permissions back (Homebrew installs ruby there)

查看更多
Summer. ? 凉城
6楼-- · 2019-01-03 04:17

I had to rm -rf ./vendor then run bundle install again.

查看更多
萌系小妹纸
7楼-- · 2019-01-03 04:19

Reinstalling RVM worked for me, but I had to reinstall all of my gems afterward:

rvm implode
\curl -sSL https://get.rvm.io | bash -s stable --ruby
rvm reload
查看更多
登录 后发表回答