Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix permissions on /usr/local for Homebrew
#!/usr/bin/ruby
#
# This script fixes /usr/local only.
#
# 6th January 2010:
# Modified the script to just fix, rather than install. - rpavlik
#
# 30th March 2010:
# Added a check to make sure user is in the staff group. This was a problem
# for me, and I think it was due to me migrating my account over several
# versions of OS X. I cannot verify that for sure, and it was tested on
# 10.6.2 using the Directory Service command line utility and my laptop.
#
# My assumptions are:
# - you are running OS X 10.6.x
# - your machine is not managed as part of a group using networked
# Directory Services
# - you have not recently killed any baby seals or kittens
#
# 14th March 2010:
# Adapted CodeButler's fork: http://gist.github.com/331512
#
module Tty extend self
def blue; bold 34; end
def white; bold 39; end
def red; underline 31; end
def reset; escape 0; end
def bold n; escape "1;#{n}" end
def underline n; escape "4;#{n}" end
def escape n; "\033[#{n}m" if STDOUT.tty? end
end
class Array
def shell_s
cp = dup
first = cp.shift
cp.map{ |arg| arg.gsub " ", "\\ " }.unshift(first) * " "
end
end
def ohai *args
puts "#{Tty.blue}==>#{Tty.white} #{args.shell_s}#{Tty.reset}"
end
def warn warning
puts "#{Tty.red}Warning#{Tty.reset}: #{warning.chomp}"
end
def system *args
abort "Failed during: #{args.shell_s}" unless Kernel.system *args
end
def sudo *args
args = if args.length > 1
args.unshift "/usr/bin/sudo"
else
"/usr/bin/sudo #{args}"
end
ohai *args
system *args
end
def getc # NOTE only tested on OS X
system "/bin/stty raw -echo"
if RUBY_VERSION >= '1.8.7'
STDIN.getbyte
else
STDIN.getc
end
ensure
system "/bin/stty -raw echo"
end
####################################################################### script
abort "Don't run this as root!" if Process.uid == 0
abort <<-EOABORT unless `groups`.split.include? "staff"
This script requires the user #{ENV['USER']} to be in the staff group. If this
sucks for you then you can install Homebrew in your home directory or however
you please; please refer to the website. If you still want to use this script
the following command should work:
dscl /Local/Default -append /Groups/staff GroupMembership $USER
EOABORT
ohai "This script will fix permissions on:"
chmods = %w( . bin etc include lib lib/pkgconfig Library sbin share var share/locale share/man
share/man/man1 share/man/man2 share/man/man3 share/man/man4
share/man/man5 share/man/man6 share/man/man7 share/man/man8
share/info share/doc share/aclocal ).
map{ |d| "/usr/local/#{d}" }.
select{ |d| File.directory? d and not File.writable? d }
chgrps = chmods.reject{ |d| File.stat(d).grpowned? }
unless chmods.empty?
ohai "The following directories will be made group writable:"
puts *chmods
end
unless chgrps.empty?
ohai "The following directories will have their group set to #{Tty.underline 39}staff#{Tty.reset}:"
puts *chgrps
end
if STDIN.tty?
puts
puts "Press enter to continue"
abort unless getc == 13
end
if File.directory? "/usr/local"
sudo "/bin/chmod", "g+w", *chmods unless chmods.empty?
# all admin users are in staff
sudo "/usr/bin/chgrp", "staff", *chgrps unless chgrps.empty?
else
sudo "/bin/mkdir /usr/local"
sudo "/bin/chmod g+w /usr/local"
# the group is set to wheel by default for some reason
sudo "/usr/bin/chgrp staff /usr/local"
end
ohai "Fix successful!"
@maletor

This comment has been minimized.

Show comment
Hide comment
@maletor

maletor Aug 18, 2011

This is where it's at.

maletor commented Aug 18, 2011

This is where it's at.

@maletor

This comment has been minimized.

Show comment
Hide comment
@maletor

maletor Aug 18, 2011

What happened on line 18?

maletor commented Aug 18, 2011

What happened on line 18?

@rpavlik

This comment has been minimized.

Show comment
Hide comment
@rpavlik

rpavlik Aug 18, 2011

Glad you found this useful. I have no idea what line 18 is about (presumably a joke) - I haven't touched that part, that was in the original install script this is based on.

Owner

rpavlik commented Aug 18, 2011

Glad you found this useful. I have no idea what line 18 is about (presumably a joke) - I haven't touched that part, that was in the original install script this is based on.

@rpavlik

This comment has been minimized.

Show comment
Hide comment
@rpavlik

rpavlik Jan 4, 2012

I think you need to make sure that your account has administrative privileges in the control panel.

Owner

rpavlik commented Jan 4, 2012

I think you need to make sure that your account has administrative privileges in the control panel.

@rpavlik

This comment has been minimized.

Show comment
Hide comment
@rpavlik

rpavlik Jan 4, 2012

This gist is supposed to do that and a little more, and a bit more carefully, but glad you got it figured out.

Owner

rpavlik commented Jan 4, 2012

This gist is supposed to do that and a little more, and a bit more carefully, but glad you got it figured out.

@craig-corcoran

This comment has been minimized.

Show comment
Hide comment
@craig-corcoran

craig-corcoran Apr 17, 2012

worked for me on 10.7.3, thanks a lot.

craig-corcoran commented Apr 17, 2012

worked for me on 10.7.3, thanks a lot.

@jessedc

This comment has been minimized.

Show comment
Hide comment
@jessedc

jessedc Jul 26, 2012

Continues to work well on 10.7.4

jessedc commented Jul 26, 2012

Continues to work well on 10.7.4

@idejuan

This comment has been minimized.

Show comment
Hide comment
@idejuan

idejuan Nov 14, 2012

Worked well on 10.8.2
thank you!

idejuan commented Nov 14, 2012

Worked well on 10.8.2
thank you!

@vanettem

This comment has been minimized.

Show comment
Hide comment
@vanettem

vanettem Mar 19, 2013

Still works a charm on 10.8.2

vanettem commented Mar 19, 2013

Still works a charm on 10.8.2

@mafrosis

This comment has been minimized.

Show comment
Hide comment
@mafrosis

mafrosis Apr 22, 2013

10.8.3 working, thanks!

mafrosis commented Apr 22, 2013

10.8.3 working, thanks!

@dfriedm

This comment has been minimized.

Show comment
Hide comment
@dfriedm

dfriedm May 18, 2013

Hi folks, total freakin newbie here. Can anyone explain how to use this?

dfriedm commented May 18, 2013

Hi folks, total freakin newbie here. Can anyone explain how to use this?

@rgtk

This comment has been minimized.

Show comment
Hide comment
@rgtk

rgtk Jul 23, 2013

ruby fix_homebrew.rb

rgtk commented Jul 23, 2013

ruby fix_homebrew.rb
@plcancelleri

This comment has been minimized.

Show comment
Hide comment
@plcancelleri

plcancelleri Aug 27, 2013

Works on 10.8.4. Thanks!

plcancelleri commented Aug 27, 2013

Works on 10.8.4. Thanks!

@TechFounder

This comment has been minimized.

Show comment
Hide comment
@TechFounder

TechFounder Sep 29, 2013

Seems to have worked on 10.8.5, although not without problems. Running brew update still returns: fatal: Unable to create '/usr/local/.git/index.lock': Permission denied

Have to use SUDO in this case. Is that the way it's suppose to work?

Also, when I tried to install imagemagick I received: Error: Cannot write to /usr/local/Cellar <= I just tested this and that works so I added a fork to your gist.

Should I just add Cellar to your chmod array and then run it again to make Cellar writable?

TechFounder commented Sep 29, 2013

Seems to have worked on 10.8.5, although not without problems. Running brew update still returns: fatal: Unable to create '/usr/local/.git/index.lock': Permission denied

Have to use SUDO in this case. Is that the way it's suppose to work?

Also, when I tried to install imagemagick I received: Error: Cannot write to /usr/local/Cellar <= I just tested this and that works so I added a fork to your gist.

Should I just add Cellar to your chmod array and then run it again to make Cellar writable?

@zwang

This comment has been minimized.

Show comment
Hide comment
@zwang

zwang Feb 2, 2014

works on 10.9.1. Thanks!

zwang commented Feb 2, 2014

works on 10.9.1. Thanks!

@davidhq

This comment has been minimized.

Show comment
Hide comment
@davidhq

davidhq Mar 26, 2014

Incredible - thank you! … // works on 10.9.2

davidhq commented Mar 26, 2014

Incredible - thank you! … // works on 10.9.2

@spuder

This comment has been minimized.

Show comment
Hide comment
@spuder

spuder Apr 6, 2014

What would happen if you are on a directory service ? Will it break anything?

spuder commented Apr 6, 2014

What would happen if you are on a directory service ? Will it break anything?

@duksis

This comment has been minimized.

Show comment
Hide comment
@duksis

duksis Jul 3, 2014

👍 nice - thanks

duksis commented Jul 3, 2014

👍 nice - thanks

@MatthewMacMillan

This comment has been minimized.

Show comment
Hide comment
@MatthewMacMillan

MatthewMacMillan Jul 25, 2014

Sorry folks, I'm still new to all this. All I was trying to do was update git. So then I installed homebrew. So then... now I'm 3 hours deep in confusion. Can anyone tell me how to USE a gist? Google doesn't seem to know. I've downloaded this file, and... now what?

MatthewMacMillan commented Jul 25, 2014

Sorry folks, I'm still new to all this. All I was trying to do was update git. So then I installed homebrew. So then... now I'm 3 hours deep in confusion. Can anyone tell me how to USE a gist? Google doesn't seem to know. I've downloaded this file, and... now what?

@Michael-Getzinger

This comment has been minimized.

Show comment
Hide comment
@Michael-Getzinger

Michael-Getzinger Aug 7, 2014

I'm getting an error when I try to run the script in Terminal.app with and without using sudo.

Without sudo, it says "fix_homebrew.rb: Permission denied".

With sudo, it asks for my password as it should, but then it says "fix_homebrew.rb: command not found".

I'm running Mac OS 10.9.4.

The reason I ran this in the first place is whenever I try to "brew install" anything, it installs but gives an error:

 Error: The `brew link` step did not complete successfully
 The formula built, but is not symlinked into /usr/local
 Could not symlink share/doc/git-doc
 /usr/local/share/doc is not writable.

I then tried "brew link git" but it gives this error:

 Linking /usr/local/Cellar/git/2.0.4... 
 Error: Could not symlink share/doc/git-doc
 /usr/local/share/doc is not writable.
 Michael-iMac:~ zeno$ 

This same pattern of Homebrew errors also occurs when trying to install (or link) "lame" or "ffmpeg", so it's not just an issue with installing "git".

Any ideas would be greatly appreciated!

Thanks

Michael-Getzinger commented Aug 7, 2014

I'm getting an error when I try to run the script in Terminal.app with and without using sudo.

Without sudo, it says "fix_homebrew.rb: Permission denied".

With sudo, it asks for my password as it should, but then it says "fix_homebrew.rb: command not found".

I'm running Mac OS 10.9.4.

The reason I ran this in the first place is whenever I try to "brew install" anything, it installs but gives an error:

 Error: The `brew link` step did not complete successfully
 The formula built, but is not symlinked into /usr/local
 Could not symlink share/doc/git-doc
 /usr/local/share/doc is not writable.

I then tried "brew link git" but it gives this error:

 Linking /usr/local/Cellar/git/2.0.4... 
 Error: Could not symlink share/doc/git-doc
 /usr/local/share/doc is not writable.
 Michael-iMac:~ zeno$ 

This same pattern of Homebrew errors also occurs when trying to install (or link) "lame" or "ffmpeg", so it's not just an issue with installing "git".

Any ideas would be greatly appreciated!

Thanks

@Michael-Getzinger

This comment has been minimized.

Show comment
Hide comment
@Michael-Getzinger

Michael-Getzinger Aug 7, 2014

Hi "MatthewMacMillan" - I'm responding to your post. I'm very new to Homebrew myself, but since no one has yet responded to your comment, I'll tell you what little I know. Before addressing your direct question, here is the little I know about using Homebrew properly:

After installing Homebrew I went to a bash shell and ran "brew update". It's always best to run this command each and every time before using Homebrew. (I believe the readme file said to run this command if you installed or updated Homebrew more than 24 hours ago.)

I then ran "brew install app" where "app" is "git" or whatever application you are trying to install, e.g., "brew install git".

Sorry if you already know all this.

In order to run this "fix_homebrew.rb" script, dragged the script file to an open Terminal window (Mac OS X so I'm running Bash shell by default) and hit return. I got the error detailed in my post above. I then tried to "switch user" when running the command by entering "sudo fix_homebrew.rb" which prompted me for my password when I typed in and hit return, but I got a different error (also outlined above).

Hope this helps... thanks

Michael-Getzinger commented Aug 7, 2014

Hi "MatthewMacMillan" - I'm responding to your post. I'm very new to Homebrew myself, but since no one has yet responded to your comment, I'll tell you what little I know. Before addressing your direct question, here is the little I know about using Homebrew properly:

After installing Homebrew I went to a bash shell and ran "brew update". It's always best to run this command each and every time before using Homebrew. (I believe the readme file said to run this command if you installed or updated Homebrew more than 24 hours ago.)

I then ran "brew install app" where "app" is "git" or whatever application you are trying to install, e.g., "brew install git".

Sorry if you already know all this.

In order to run this "fix_homebrew.rb" script, dragged the script file to an open Terminal window (Mac OS X so I'm running Bash shell by default) and hit return. I got the error detailed in my post above. I then tried to "switch user" when running the command by entering "sudo fix_homebrew.rb" which prompted me for my password when I typed in and hit return, but I got a different error (also outlined above).

Hope this helps... thanks

@jmscholen

This comment has been minimized.

Show comment
Hide comment
@jmscholen

jmscholen Nov 4, 2014

Worked on 10.9.5 Thanks!

for Newbies:
$git clone https://gist.github.com/768518.git
$cd 768518
$ruby fix_homebrew.rb

You will need to enter your password and then you should get the ===> Fix Successful! output if all went well.

jmscholen commented Nov 4, 2014

Worked on 10.9.5 Thanks!

for Newbies:
$git clone https://gist.github.com/768518.git
$cd 768518
$ruby fix_homebrew.rb

You will need to enter your password and then you should get the ===> Fix Successful! output if all went well.

@JulianDevilleSmith

This comment has been minimized.

Show comment
Hide comment
@JulianDevilleSmith

JulianDevilleSmith Jan 4, 2015

^ works on 10.10 too. Just ran...
etc etc etc...

$ ruby fix_homebrew.rb
==> This script will fix permissions on:

Press enter to continue
==> Fix successful!

JulianDevilleSmith commented Jan 4, 2015

^ works on 10.10 too. Just ran...
etc etc etc...

$ ruby fix_homebrew.rb
==> This script will fix permissions on:

Press enter to continue
==> Fix successful!

@ninjaPixel

This comment has been minimized.

Show comment
Hide comment
@ninjaPixel

ninjaPixel Jan 19, 2015

Awesome. Just ran on 10.10.1.
Cheers!

ninjaPixel commented Jan 19, 2015

Awesome. Just ran on 10.10.1.
Cheers!

@JoshCheek

This comment has been minimized.

Show comment
Hide comment
@JoshCheek

JoshCheek Feb 10, 2015

Can anyone explain this for me? E.g.

  • Why does this happen? I see it a lot, but don't know what situation causes it.
  • What is actually wrong here? / how does this script fix it. I feel like I understand the chmod one (directories have incorrect permission to install to these global locations), but I don't understand the chgrp one.
  • Given that it seems to be permissions issues, and from my naive understanding, it seems like any other user on my system will hit issues (e.g. if I own the dirs, then another user will hit this, if they own them, then I will) Or is that where the groups come in? If we're both admin, then neither of us hit it.
  • Any good resources for reading about groups? Ie what they're for, how they're implemented, are there common groups (e.g. I see "wheel" a lot) and what do they mean? What tools allow me to understand and manipulate the state of groups.
  • Why don't we just install into each user's home directory? It seems like that would fix the issue.

JoshCheek commented Feb 10, 2015

Can anyone explain this for me? E.g.

  • Why does this happen? I see it a lot, but don't know what situation causes it.
  • What is actually wrong here? / how does this script fix it. I feel like I understand the chmod one (directories have incorrect permission to install to these global locations), but I don't understand the chgrp one.
  • Given that it seems to be permissions issues, and from my naive understanding, it seems like any other user on my system will hit issues (e.g. if I own the dirs, then another user will hit this, if they own them, then I will) Or is that where the groups come in? If we're both admin, then neither of us hit it.
  • Any good resources for reading about groups? Ie what they're for, how they're implemented, are there common groups (e.g. I see "wheel" a lot) and what do they mean? What tools allow me to understand and manipulate the state of groups.
  • Why don't we just install into each user's home directory? It seems like that would fix the issue.
@cmorisse

This comment has been minimized.

Show comment
Hide comment
@cmorisse

cmorisse Mar 28, 2015

This save my day (or at least my morning !). Thanks so much !!!!

cmorisse commented Mar 28, 2015

This save my day (or at least my morning !). Thanks so much !!!!

@bennylp

This comment has been minimized.

Show comment
Hide comment
@bennylp

bennylp Aug 20, 2015

Worked on 10.10.2 (after "cannot write to /usr/local/lib error"). Cheers!

bennylp commented Aug 20, 2015

Worked on 10.10.2 (after "cannot write to /usr/local/lib error"). Cheers!

@modestoartur

This comment has been minimized.

Show comment
Hide comment
@modestoartur

modestoartur Sep 23, 2015

Worked well on 10.11 Thank u so much!

modestoartur commented Sep 23, 2015

Worked well on 10.11 Thank u so much!

@aliirz

This comment has been minimized.

Show comment
Hide comment
@aliirz

aliirz Oct 30, 2015

Thank you so much for this. 👏

aliirz commented Oct 30, 2015

Thank you so much for this. 👏

@davidmoshal

This comment has been minimized.

Show comment
Hide comment
@davidmoshal

davidmoshal Nov 10, 2015

Thanks for this, worked on 10.11.1 (15B42) !

davidmoshal commented Nov 10, 2015

Thanks for this, worked on 10.11.1 (15B42) !

@mrrts

This comment has been minimized.

Show comment
Hide comment
@mrrts

mrrts Dec 16, 2015

Thanks!!!

mrrts commented Dec 16, 2015

Thanks!!!

@NotAlexNoyle

This comment has been minimized.

Show comment
Hide comment
@NotAlexNoyle

NotAlexNoyle Mar 14, 2016

Thanks! Works on 10.11.3.

NotAlexNoyle commented Mar 14, 2016

Thanks! Works on 10.11.3.

@brianzelip

This comment has been minimized.

Show comment
Hide comment
@brianzelip

brianzelip Mar 26, 2016

Works on 10.11.4.

brianzelip commented Mar 26, 2016

Works on 10.11.4.

@AnneTheAgile

This comment has been minimized.

Show comment
Hide comment
@AnneTheAgile

AnneTheAgile Jul 8, 2016

Works on 10.11.5, ty all! Brew update then worked like a charm.

AnneTheAgile commented Jul 8, 2016

Works on 10.11.5, ty all! Brew update then worked like a charm.

@matt-kg

This comment has been minimized.

Show comment
Hide comment
@matt-kg

matt-kg Oct 6, 2016

Worked on Sierra beta and GM for me.

matt-kg commented Oct 6, 2016

Worked on Sierra beta and GM for me.

@YamilG

This comment has been minimized.

Show comment
Hide comment
@YamilG

YamilG Nov 30, 2016

Worked, thanks!

YamilG commented Nov 30, 2016

Worked, thanks!

@dxiri

This comment has been minimized.

Show comment
Hide comment
@dxiri

dxiri Mar 17, 2017

Worked perfectly on El Capitan 10.11.6 (15G1217), thank you!

dxiri commented Mar 17, 2017

Worked perfectly on El Capitan 10.11.6 (15G1217), thank you!

@avishapiro

This comment has been minimized.

Show comment
Hide comment
@avishapiro

avishapiro May 3, 2017

Works on 10.12.4. Thanks!

avishapiro commented May 3, 2017

Works on 10.12.4. Thanks!

@AJ

This comment has been minimized.

Show comment
Hide comment
@AJ

AJ Nov 16, 2017

Hello,

Just tried this on OSX 10.13 and I get the following error:

ruby fixBrew.rb 
==> This script will fix permissions on:
==> The following directories will be made group writable:
/usr/local/.
==> The following directories will have their group set to staff:
/usr/local/.

Press enter to continue
==> /usr/bin/sudo /bin/chmod g+w /usr/local/.
chmod: Unable to change file mode on /usr/local/.: Operation not permitted
Failed during: /usr/bin/sudo /bin/chmod g+w /usr/local/.

What do I do now?

AJ commented Nov 16, 2017

Hello,

Just tried this on OSX 10.13 and I get the following error:

ruby fixBrew.rb 
==> This script will fix permissions on:
==> The following directories will be made group writable:
/usr/local/.
==> The following directories will have their group set to staff:
/usr/local/.

Press enter to continue
==> /usr/bin/sudo /bin/chmod g+w /usr/local/.
chmod: Unable to change file mode on /usr/local/.: Operation not permitted
Failed during: /usr/bin/sudo /bin/chmod g+w /usr/local/.

What do I do now?

@DjolePetrovic

This comment has been minimized.

Show comment
Hide comment
@DjolePetrovic

DjolePetrovic Mar 2, 2018

hey @AJ

did you find a fix for your problem? I'm having exactly the same issue.

DjolePetrovic commented Mar 2, 2018

hey @AJ

did you find a fix for your problem? I'm having exactly the same issue.

@jenpocheng

This comment has been minimized.

Show comment
Hide comment
@jenpocheng

jenpocheng Jun 2, 2018

chown-ing /usr/local/. is not allowed in High Sierra anymore. Try sudo chown -R $(whoami) $(brew --prefix)/

jenpocheng commented Jun 2, 2018

chown-ing /usr/local/. is not allowed in High Sierra anymore. Try sudo chown -R $(whoami) $(brew --prefix)/

@romu70

This comment has been minimized.

Show comment
Hide comment
@romu70

romu70 Jun 13, 2018

@jenpocheng, I've just tried your command on 10.13.5 and I get: chown: /usr/local/: Operation not permitted

Any advice?

romu70 commented Jun 13, 2018

@jenpocheng, I've just tried your command on 10.13.5 and I get: chown: /usr/local/: Operation not permitted

Any advice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment