public
Last active — forked from mxcl/install_homebrew.markdown

Fix permissions on /usr/local for Homebrew

  • Download Gist
install_homebrew.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#!/usr/bin/ruby
#
# This script installs to /usr/local only. To install elsewhere you can just
# untar https://github.com/mxcl/homebrew/tarball/master anywhere you like.
#
#
# 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 "/usr/local/.git already exists!" if File.directory? "/usr/local/.git"
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 install:"
puts "/usr/local/bin/brew"
puts "/usr/local/Library/Formula/..."
puts "/usr/local/Library/Homebrew/..."
 
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
 
Dir.chdir "/usr/local" do
ohai "Downloading and Installing Homebrew..."
# -m to stop tar erroring out if it can't modify the mtime for root owned directories
# pipefail to cause the exit status from curl to propogate if it fails
system "/bin/bash -o pipefail -c '/usr/bin/curl -sSfL https://github.com/mxcl/homebrew/tarball/master | /usr/bin/tar xz -m --strip 1'"
end
 
ohai "Installation successful!"
 
warn "/usr/local/bin is not in your PATH." unless ENV['PATH'].split(':').include? '/usr/local/bin'
warn "Now install Xcode: http://developer.apple.com/technologies/xcode.html" unless Kernel.system "/usr/bin/which -s gcc"

This is where it's at.

What happened on line 18?

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.

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

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

worked for me on 10.7.3, thanks a lot.

Continues to work well on 10.7.4

Worked well on 10.8.2
thank you!

Still works a charm on 10.8.2

10.8.3 working, thanks!

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

ruby fix_homebrew.rb

Works on 10.8.4. Thanks!

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?

works on 10.9.1. Thanks!

Incredible - thank you! … // works on 10.9.2

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.