184

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.
1
  • 3
    Don't use sudo to install Ruby, or change gems. Instead use rbenv or RVM to install and manage Ruby. Read and follow the directions on their respective sites. And, in either case, after installing, don't use sudo unless you understand what is changing and why you'd want to do it. Aug 12, 2015 at 18:54

15 Answers 15

335

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.)

13
  • Thank you so much! I have struggled with new versions of brew and ruby but still could not get pass the /usr/bin problem. Specifying the desired path when running gem install solved the problem. Dec 14, 2015 at 22:56
  • The .gemrc should have gem: -n /usr/local/bin (need a space after -n). Jan 25, 2016 at 19:52
  • @PaulSchreiber Well, I've been using it without a space for five months now. :) Are you sure?
    – nschum
    Jan 25, 2016 at 21:48
  • 2
    Plus 1 for Option #2, which is a viable and safe alternative if you only want those binaries without a separate Ruby. Our home directories are the perfect place for the added gems, and a quick tweak to PATH makes them accessible. I can't recommend Option #1 because someone will end up overwriting a gem and breaking their system Ruby or Apple's built-in code that uses it, and then they'll ask yet another question on SO about how to fix it. Jan 29, 2016 at 23:03
  • 1
    This is an excellent answer, but maybe out of date. On Sierra, the option I needed was --install-dir /usr/local/bin Jun 8, 2017 at 21:05
110

In my case, I had to re-install Ruby using Brew. That seems to have solved the problem as I can install gems again.

brew install ruby

After this, you need to log out and log back in, either graphically or just restarting your terminal.

6
  • Worked well for me! I suppose it gets rid of any weirdness in the bundled ruby install... IMO this is the best solution.
    – n13
    Feb 11, 2016 at 3:40
  • You can tell which ruby is being run with ``` which -a ruby ``` On my machine that gives: ``` $ which -a ruby /usr/local/bin/ruby /usr/local/bin/ruby /usr/bin/ruby ``` As to why /usr/local/bin is on PATH twice -- looks like brew install ruby puts it there, even if it's already there. Presumably the ruby in /usr/local is configured to install its gem's in a location that doesn't need root access (which makes perfect sense actually).
    – WallStProg
    Apr 11, 2016 at 23:20
  • 7
    P.S. You need to logout and log back in (via Terminal) or the gem install will still fail.
    – WallStProg
    Apr 11, 2016 at 23:29
  • Worked fine for me. I combined the accepted answer with this one. Thank you both
    – youssman
    Jul 23, 2016 at 14:36
  • 1
    You don't have to log out and log back in again. Just run rehash to make sure your shell realizes that there's a new ruby earlier on the $PATH. Even this will not be necessary in the situation where you hadn't run ruby yet in the shell session. May 27, 2017 at 0:52
38

That is because of the new security function of OS X "El Capitan". Try adding --user-install instead of using sudo:

$ gem install *** --user-install

For example, if you want to install fake3 just use:

$ gem install fake3 --user-install
4
  • Yes! Thank you so much! Sep 15, 2016 at 14:27
  • After spending 30 min with brew approach, closing terminal etc at last this solution worked on 10.10.5 But still not sure why brew approach didn't work. Please clarify. which ruby post brew install /usr/bin/ruby
    – vikramvi
    Jan 4, 2017 at 15:01
  • 1
    Wow now the issue is back gem install redcarpet -v '3.3.4' --user-install throwing error WARNING: You don't have /Users/user_name/.gem/ruby/2.0.0/bin in your PATH, gem executables will not run. Earlier I was not getting this error
    – vikramvi
    Jan 4, 2017 at 15:54
  • 1
    stackoverflow.com/questions/19579392/… , this helped me to resolve issue at last
    – vikramvi
    Jan 4, 2017 at 16:39
24
sudo gem install -n /usr/local/bin cocoapods

Try this. It will definately work.

1
19

You have to update Xcode to the newest one (v7.0.1) and everything will work as normal.

If after you install the newest Xcode and still doesn't work try to install gem in this way:

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

For example:

sudo gem install -n /usr/local/bin fakes3
sudo gem install -n /usr/local/bin compass
sudo gem install -n /usr/local/bin susy
10

Looks like when upgrading to OS X El Capitain, the /usr/local directory is modified in multiple ways :

  1. user permissions are reset (this is also a problem for people using Homebrew)
  2. binaries and symlinks might have been deleted or altered

[Edit] There's also a preliminary thing to do : upgrade Xcode...

Solution for #1 :

$ sudo chown -R $(whoami):admin /usr/local

This will fix permissions on the /usr/local directory which will then help both gem install and brew install|link|... commands working properly.

Solution to #2 :

Ruby based issues

Make sure you have fixed the permissions of the /usr/local directory (see #1 above)

First try to reinstall your gem using :

sudo gem install <gemname>

Note that it will install the latest version of the specified gem.

If you don't want to face backward-compatibility issues, I suggest that you first determine which version of which gem you want to get and then reinstall it with the -v version. See an exemple below to make sure that the system won't get a new version of capistrano.

$ gem list | grep capistrano
capistrano (3.4.0, 3.2.1, 2.14.2)
$ sudo gem install capistrano -v 3.4.0

Brew based issues

Update brew and upgrade your formulas

$ brew update
$ brew upgrade

You might also need to re-link some of them manually

$ brew link <formula>
3

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)

2

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

2

If the gem you are trying to install requires xml libraries, then try this:

sudo gem install -n /usr/local/bin  <gem_name> -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib/

Specifically, I ran into a problem while installing the nokogiri gem v 1.6.8 on OS X El Capitan

and this finally worked for me:

sudo gem install -n /usr/local/bin  nokogiri -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib/

To make sure you have libxml2 and libxslt installed, you can do:

brew install libxml2 libxslt
brew install libiconv

and then check to make sure you have xcode command line tools installed:

xcode-select --install 

should return this error:

xcode-select: error: command line tools are already installed, use "Software Update" to install updates
1

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
0

I ran across the same issue after installing El Capitan, I tried to install sass and compass into a symfony project, the following command returned the following error:

$ sudo gem install compass

ERROR: Error installing compass: ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb 

checking for ffi.h... /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:434:in `try_do': The compiler failed to generate an executable file. (RuntimeError)

So I then tried to install sass with: $ sudo gem install sass

Got the same error message, after some googling I managed to install sass using the following command:

$ sudo gem install -n /usr/local/bin sass

The above worked for me with installing sass but did not work for installing compass. I read that someone somewhere had opened an instance of xcode then closed it again, then successfully ran the same command after which worked for them. I attempted to open xcode but was prompted with a message saying that the version of xcode installed was not compatible with El Capitan. So I then updated xcode from the app store, re-ran the following command which this time ran successfully:

$ sudo gem install -n /usr/local/bin compass

I was then able to run $ compass init

I now have all my gems working and can proceed to build some lovely sass stuff :)

0

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

0

You might have two options:

  1. If you've installed ruby and rails, you can first try running the command:
    rvm fix-permissions 
    
  2. You can uninstall ruby completely, and reinstall in your ~ directory aka your home directory.

If you're using homebrew the command is:

brew uninstall ruby

For rails uninstall without homebrew the command is:

rvm remove

This should reinstall the latest ruby by running command:

curl -L https://get.rvm.io | bash -s stable --rails<br>

Mac has 2.6.3 factory installed, and it's required... if not run this command:

rvm install "ruby-2.6.3"

and then:

gem install rails

You'll get a few error messages at the end saying you have to add some other bundles...
Just make sure you're in the home ~ directory when you're installing so the permissions won't be an issue, but just in case...

I again ran:

rvm fix-permissions

and:

rvm debug

which told me I had to download yarn, I didn't save the output for it. Basically I did whatever the prompt told me to do if it had to do with my OS.

-D

0
-1

I don't like to install stuff with sudo. once you start with sudo you can't stop..

try giving permissions to the Gems directory.

sudo chown -R $(whoami) /Library/Ruby/Gems/2.0.0
1
  • 2
    Don't change ownership of the system Ruby gems. That can cause problems when Mac OS attempts to update anything. Jan 29, 2016 at 23:01
-1
sudo chown -R $(whoami):admin /usr/local

That will give permissions back (Homebrew installs ruby there)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.