327

I'm getting an error when I run brew in the terminal:

 /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- utils/popen (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/Library/Homebrew/utils.rb:6:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/Library/Homebrew/global.rb:9:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/Library/brew.rb:16:in `<main>'

These are my gem settings:

- RUBYGEMS VERSION: 2.0.14
- RUBY VERSION: 2.0.0 (2014-02-24 patchlevel 451) [universal.x86_64-darwin13]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.0.0
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
  - ruby
  - universal-darwin-13
- GEM PATHS:
  - /Library/Ruby/Gems/2.0.0
  - /Users/ronaldkwan/.gem/ruby/2.0.0
  - /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0
- GEM CONFIGURATION:
  - :update_sources => true
  - :verbose => true
  - :backtrace => false
  - :bulk_threshold => 1000
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
kerokero
  • 3,271
  • 2
  • 13
  • 4

9 Answers9

804

Original Answer

The problem mainly occurs after updating OS X to El Capitan (OS X 10.11) or macOS Sierra (macOS 10.12).

This is because of file permission issues with El Capitan’s or later macOS's new SIP process. Try changing the permissions for the /usr/local directory:

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

If it still doesn't work, use these steps inside a terminal session and everything will be fine:

cd /usr/local/Homebrew
git reset --hard origin/master
brew update

If /usr/local/Library/Homebrew doesn't work, try /usr/local/Homebrew. The problem might be that Homebrew is outdated.

Apr 2021 Update

The command above doesn't work for macOS High Sierra or above, as explained in this GitHub issue. You have to run this instead:

sudo chown -R $(whoami) $(brew --prefix)/*
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
Vineet Kapoor
  • 8,249
  • 1
  • 11
  • 14
  • 39
    I had the problem caused by El Capitan update. This solved the problem. Thanks. – Vivek Mar 09 '16 at 07:37
  • 5
    Same here, upgrade to El Capitan broke it, I had to reset the permissions first and then run the git commands and brew update worked. Thanks. – Bob Mar 15 '16 at 18:08
  • 4
    This is right answer if you are facing that problem after upgrading to El Capitan :) – chemic Apr 22 '16 at 06:59
  • 1
    Worked for me on Sierra. Thanks! – Matt Sephton Aug 23 '16 at 19:58
  • 3
    Had this issue after upgrading to **macOS Sierra**. This solved it, thanks! Though, changing permissions on `/usr/local` doesn't seem to be required now. "Homebrew no longer needs to have ownership of /usr/local. If you wish you can return `/usr/local` to its default ownership with: `sudo chown root:wheel /usr/local`" – Bert Nov 01 '16 at 23:28
  • 1
    @CodeAlchemist yea, but for the update process it did require ownership of /usr/local, then after you're done you can change it back to root:wheel – Clarence Liu Nov 14 '16 at 22:49
  • 1
    Homebrew no longer needs to have ownership of /usr/local. If you wish you canreturn /usr/local to its default ownership with: sudo chown root:wheel /usr/local – Hovo Nov 19 '16 at 14:41
  • 1
    This works for me. Essentially, it changed the group name from 'wheel' to 'admin' for new os version. – phuang07 Nov 23 '16 at 23:28
  • 5
    Second method worked for me. I just needed to find where `HOMEBREW` directory was located. In my case: `/usr/local/Library/Homebrew` – Alexander Mar 07 '17 at 19:47
  • 1
    Second solution did the trick. New version of homebrew no longer needs ownership of `/usr/local`. – Jorge Cabot Aug 04 '17 at 17:52
  • 3
    Did not work for **High Sierra** anymore, s. https://github.com/Homebrew/brew/issues/3228#issuecomment-332679274: `sudo chown -R $(whoami) $(brew --prefix)/*` – davidenke Aug 31 '18 at 13:11
  • 1
    /usr/local can no longer be chown'd in High Sierra. Instead use sudo chown -R $(whoami) $(brew --prefix)/* – Marcelo Cintra de Melo Sep 04 '18 at 14:53
  • 2
    Fixed but path was: cd /usr/local/Homebrew – D-Lorenz Mar 01 '19 at 09:30
  • Fixed for me on ventura 13.2.1 – eduardobursa Mar 13 '23 at 16:41
379

First, open a terminal session and run:

cd /usr/local/
git status

to see if Homebrew is clean.

If it's dirty, run:

git reset --hard && git clean -df

then

brew doctor
brew update

If it's still broken, try this in your session:

sudo rm /System/Library/Frameworks/Ruby.framework/Versions/Current
sudo ln -s /System/Library/Frameworks/Ruby.framework/Versions/1.8 /System/Library/Frameworks/Ruby.framework/Versions/Current

This will force Homebrew to use Ruby 1.8 from the system's installation.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
richarddong
  • 3,941
  • 1
  • 13
  • 6
  • 7
    Probably a good answer, but BTW I get other errors when running both commands give (I mention incase anyone wants to cover this further scenario): - rm: /System/Library/Frameworks/Ruby.framework/Versions/Current: Operation not permitted – PandaWood Feb 19 '16 at 10:49
  • 4
    I simply reinstalled homebrew with a fresh version and it started working. Check homebrew homepage with the link to install, and when you try it it will complain that homebrew is already installed and teach you how to uninstall. – pertz Feb 23 '16 at 00:52
  • So, uninstalling/reinstalling got Homebrew working again for me. It also deleted everything it had installed, basically trashing my system. So not a great solution. – Evan Knowles Mar 01 '16 at 22:07
  • If you're getting errors with ruby and you're using RVM, try issuing `rvm use system` to ensure you're using Mac OS X's ruby when running `brew update` – DavidJ Mar 05 '16 at 22:54
  • Note that the default Ruby on recent versions of OS X is 2.0, not 1.8. – bfontaine Mar 23 '16 at 13:21
  • What does "to see if Homebrew is clean." means? and why it is at /usr/local/? – Denly May 15 '16 at 20:48
  • This method worked for me. I still had to change the permission on /usr/local before it would work. After `brew update` finished it informed me it no longer needed the permissions on /usr/local so I could change it back. – Dave Rager Dec 15 '16 at 14:04
  • vineet kapoor's trick above did not work for my macOS High Sierra, but this one worked! – Kouichi C. Nakamura Sep 14 '18 at 21:51
  • This is an outdated answer, so use it carefully. Apple's pre-installed Ruby version periodically changes so you'll have to modify the commands to account for those changes. – the Tin Man Dec 21 '19 at 21:16
83

Uninstall homebrew:

 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

Then reinstall

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Warning: This script will remove: /Library/Caches/Homebrew/ - thks benjaminsila

Community
  • 1
  • 1
odemolliens
  • 2,581
  • 2
  • 32
  • 34
  • 1
    I run an uninstall of Homebrew and then a reinstall. Fixed the issue. @OsTa ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)" Warning: This script will remove: /Library/Caches/Homebrew/ – BenJaminSila Feb 25 '16 at 07:19
  • 3
    Sort of a shotgun approach but this worked more easily than trying to manage file permissions... – BenKarl Jul 13 '16 at 18:58
  • 1
    Worked for me On a system upgraded to 10.12 (Sierra), after the uninstall/reinstall I was able to run 'brew doctor' and followed the instructions there to fix a few permissions issues. Working fine now – codesponge Apr 19 '17 at 03:42
  • After a hard drive crash my underlying git repository was corrupted and while git reset would therefore not work, un-/reinstalling did. However, I had to reinstall all my installed packages. – bsumirak Oct 11 '17 at 16:09
27

In my case I just needed to remove Homebrew's executable using:

sudo rm -f `which brew`

Then reinstall Homebrew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Inanc Gumus
  • 25,195
  • 9
  • 85
  • 101
  • 2
    In case you get stuck after this in brew install / uninstall loop, use following link - https://github.com/Homebrew/homebrew/issues/44460 – fortm Oct 17 '15 at 18:18
  • Did you get this meesage trying the 2nd command (which seems to be an indication something has gone wrong, though not an error)? - The current contents of /usr/local are bin Cellar CODEOFCONDUCT.md CONTRIBUTING.md etc Frameworks git include lib Library LICENSE.txt opt Qt4.7 README.md share SUPPORTERS.md var .git .gitignore – PandaWood Feb 19 '16 at 10:52
  • @PandaWood no, i didn't. what the content of the error message that you depicting with 'this message' in your comment? – Inanc Gumus Feb 19 '16 at 12:03
  • @deeperx Everything after the "-". In the end, I had to uninstall brew and reinstall again – PandaWood Feb 20 '16 at 16:06
  • @PandaWood Do you use correct commas? Can you copy paste it here directly? – Inanc Gumus Feb 22 '16 at 19:13
24

After updating to El Capitan, /usr/local has root:wheel rights.

Change the rights back to the user using:

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

and:

brew doctor && brew update

This helped me to get Homebrew working again.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Josef Rysanek
  • 377
  • 3
  • 12
  • 2
    isn't it quite dangerous? – FRAGA Feb 19 '16 at 18:42
  • Permissions to `/usr/local` were changed in the OS for security reasons - so, just because you've "returned rights as before" doesn't mean it's a good thing to do. – leanne Jun 01 '17 at 21:04
8

First I executed:

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

Then:

cd $(brew --prefix) && git fetch origin && git reset --hard origin/master
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
d0ping
  • 462
  • 4
  • 16
3

This issue should be fixed in the newest version of Homebrew. Try reinstalling it, which is described on the Homebrew home page.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Fred
  • 421
  • 2
  • 6
1

To me it feels like you have missing header files for popen, which is a C system library.

Check if you have installed xcode successful with the command line tools and have accepted the license.

See this thread for more information: How to install Xcode Command Line Tools

Dennis
  • 780
  • 6
  • 17
0

To restore your Homebrew setup try this:

cd /usr/local/Homebrew/Library && git stash && git clean -d -f && git reset --hard && git pull
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mrded
  • 4,674
  • 2
  • 34
  • 36