Skip to content

Instantly share code, notes, and snippets.

@fenollp
Forked from mxcl/install_homebrew.markdown
Created August 14, 2011 00:03
Show Gist options
  • Save fenollp/1144389 to your computer and use it in GitHub Desktop.
Save fenollp/1144389 to your computer and use it in GitHub Desktop.
Installs Homebrew to /usr/local so you don't need sudo to `brew install`
#!/usr/bin/ruby
# This script installs to /usr/local only. To install elsewhere you can just
# untar https://github.com/sceaga/homebrew/tarball/tiger anywhere you like.
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.first}"
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
def badlibs
@badlibs ||= begin
Dir['/usr/local/lib/*.dylib'].select do |dylib|
ENV['dylib'] = dylib
File.file? dylib and not File.symlink? dylib and `/usr/bin/file "$dylib"` =~ /shared library/
end
end
end
####################################################################### script
abort "/usr/local/.git already exists!" unless Dir["/usr/local/.git/*"].empty?
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 (under Tiger):
sudo dscl . append /Groups/staff GroupMembership $USER
You may also chown -Rf the target directory.
EOABORT
ohai "This script will install:"
puts "/usr/local/bin/brew"
puts "/usr/local/Library/Formula/..."
puts "/usr/local/Library/Homebrew/..."
chmods = %w( share/man lib/pkgconfig var/log share/locale
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}" }
root_dirs = []
%w(bin Cellar etc include lib Library sbin share var .git).each do |d|
d = "/usr/local/#{d}"
if File.directory? d then chmods else root_dirs end << d
end
chmods = chmods.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
sudo "/bin/mkdir /usr/local" unless File.directory? "/usr/local"
sudo "/bin/chmod o+w /usr/local"
begin
sudo "/bin/chmod", "g+w", *chmods unless chmods.empty?
sudo "/usr/bin/chgrp", "staff", *chgrps unless chgrps.empty?
system "/bin/mkdir", *root_dirs unless root_dirs.empty?
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
# REMOVED pipefail option (not available on Tiger PPC).
# REMOVED SSL security, using curl option '-k' (certificate can't be checked due to too old curl-ca.cert).
system "/bin/bash -c '/usr/bin/curl -sSfLk https://github.com/sceaga/homebrew/tarball/tiger | /usr/bin/tar xz -m --strip 1'"
end
ensure
# we reset the permissions of /usr/local because we want to minimise the
# amount of fiddling we do to the system. Some tools require /usr/local to
# be by non-writable for non-root users.
sudo "/bin/chmod o-w /usr/local"
end
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 gcc` = /usr/bin/gcc ]"
unless badlibs.empty?
warn "The following *evil* dylibs exist in /usr/local/lib"
puts "They may break builds or worse. You should consider deleting them:"
puts *badlibs
end
ohai "Installation successful!"
puts "Now type: brew help"
@fenollp
Copy link
Author

fenollp commented Aug 14, 2011

If you see a Warning that says "/usr/local/bin is not in your PATH", you may type /bin/echo "export PATH=$PATH:/usr/local/bin" >> ~/.profile or whichever .bashrc, .csh, …

@fenollp
Copy link
Author

fenollp commented Aug 14, 2011

which -s doesn't exist on Tiger PPC, so I replaced it with [ /usr/bin/which gcc = /usr/bin/gcc ].

@fenollp
Copy link
Author

fenollp commented Aug 14, 2011

If you're having permissions issues (related to staff | wheel), do:

## Add yourself to group:staff
sudo dscl . append /Groups/staff GroupMembership $USER
dscl . -read /Groups/staff | grep GroupMembership
# sudo dscl . delete /Groups/staff GroupMembership $USER

## Change /usr/local/'s group & user (/usr/local is the default target directory)
sudo chown -Rf $USER:staff  /usr/local

## Remove Homebrew
cd `brew --prefix`
sudo rm -rf Cellar/
brew prune
sudo rm -rf Library .git .gitignore bin/brew README.md share/man/man1/brew
rm -rf ~/Library/Caches/Homebrew

## And reinstall Homebrew
# See first comment!

EDIT: I now give hints about this issue related to an old Mac OS X in the .rb
I think it's up to the user to chown his target dir (which can be anywhere) and to let himself be part of group:staff… that's why there're only hints in the .rb.

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