69

I am not sure why this is happening but I cannot update brew in my MAC OS X El Capitan. I did:

brew update

and got:

warning: unable to unlink CONTRIBUTING.md: Permission denied
warning: unable to unlink SUPPORTERS.md: Permission denied
fatal: cannot create directory at '.github': Permission denied

to go over that issue I sudoed the above command but got further issues:

sudo brew update
Password:
/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 -- mach (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/extend/pathname.rb:2: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:3: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>'

now whenever I do brew install I get:

brew update

/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 -- mach (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/extend/pathname.rb:2: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:3: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>'

in fact, it seems that whenever I do anything, like just type brew it gets the same message. Does anyone know how to fix this?

bfontaine
  • 18,169
  • 13
  • 73
  • 107
Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
  • 2
    this is probably a super hacky solution but I completely uninstalled homebrew and re-installed it. To uninstall read: https://www.quora.com/How-does-one-uninstall-Homebrew – Charlie Parker Mar 20 '16 at 22:54
  • 1
    Possible duplicate of [homebrew not working on OSX](http://stackoverflow.com/questions/24652996/homebrew-not-working-on-osx) – Ivan Chau May 11 '16 at 15:38
  • Homebrew has been updated to address the problem of `/usr/local` ownership. If you haven't updated Homebrew in a while, follow the steps outlined by @bfontaine below. Once updated, you may revert ownership back to `root:wheel` by running `sudo chown root:wheel /usr/local` and, continue using and updating Homebrew without worrying about file ownership. – Sandeep M Sep 11 '17 at 17:23

1 Answers1

249

You shouldn’t use sudo with Homebrew. First run the following to ensure you own everything under /usr/local:

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

Alternative command for newer macOS version:

sudo chown -R $(whoami) $(brew --prefix)/*

Then reset your Homebrew repo to a clean state:

cd /usr/local && git fetch && git reset --hard origin/master

It should fix your issues.

(replace /usr/local with $(brew --prefix) if you installed Homebrew elsewhere)

bfontaine
  • 18,169
  • 13
  • 73
  • 107
  • 3
    I ran `sudo chown -R $(whoami):admin /usr/local` but the terminal outputted nothing. What was I suppose to see? – Charlie Parker Mar 21 '16 at 23:54
  • Are you sure you really want to change ownership to a user in `/usr/local`? It seems very wrong to me even though it may work. – totoro Apr 16 '16 at 14:34
  • @ThomasChristensen what do you mean? – Charlie Parker Apr 16 '16 at 23:48
  • @CharlieParker `/usr` is on *NIX owned by `root` because it is for all users, and `${HOME}` by the user. Normally there are ways to install it user local within `${HOME}`, otherwise use `sudo` to install it worldwide in `/usr/local`. – totoro Apr 17 '16 at 06:11
  • @ThomasChristensen Yes I’m sure; this is the default location for Homebrew. You can install it in another prefix (e.g. `~/.brew`) but some things might not install correctly. – bfontaine Apr 17 '16 at 10:51
  • @bfontaine Homebrew should do something about that. – totoro Apr 17 '16 at 10:56
  • @totoro Well it allows you to install it somewhere else; it can’t really fix software that breaks if not installed under `/usr/local`. – bfontaine Apr 17 '16 at 18:59
  • so is running `sudo chown -R $(whoami):admin /usr/local` not the right thing to do? I am confused. – Charlie Parker Apr 17 '16 at 19:00
  • @CharlieParker Yes it is. This is the only time you’ll need to use `sudo`; don’t run `brew` commands with `sudo`. – bfontaine Apr 18 '16 at 07:45
  • @CharlieParker You can do it, it just violates the basic file system layout idea of *NIX. You can google about that. – totoro Apr 18 '16 at 08:42
  • Note that if you’re uncomfortable doing that you can still install Homebrew elsewhere, e.g. in `~/.brew` or `~/.homebrew` (but it’s not officially supported). Homebrew targets mostly people with their own computer so installing personal stuff under `/usr/local` is not an issue. – bfontaine Apr 18 '16 at 08:58
  • @totoro I don't even know what I am suppose to be googling. Ownership of `urs/local`? – Charlie Parker Apr 18 '16 at 15:13
  • 1
    @CharlieParker `unix filesystem layout` -> https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard. – totoro Apr 18 '16 at 18:31
  • 2
    I found this in the [Homebrew FAQ in Github](https://github.com/Homebrew/brew/blob/master/share/doc/homebrew/FAQ.md#why-does-homebrew-prefer-i-install-to-usrlocal), which I think is relevant to this answer: "**Why does Homebrew prefer I install to `/usr/local`?... 3. It’s safe** Apple has left this directory for us. Which means there is no `/usr/local` directory by default, so there is no need to worry about messing up existing tools." – alondono May 01 '16 at 04:23
  • 1
    You may also need `brew update` after that to fix stuff. – Dmytro Korduban Aug 15 '16 at 14:27
  • Suggestion worked great with one correction - homebrew no longer needs to have ownership of /usr/local. – MCP Feb 20 '17 at 00:14