Skip to content

Instantly share code, notes, and snippets.

@devinrhode2
Forked from aubguillemette/install.rb
Last active December 22, 2023 06:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save devinrhode2/a37f9a05656fef604332779ec6e5f0b4 to your computer and use it in GitHub Desktop.
Save devinrhode2/a37f9a05656fef604332779ec6e5f0b4 to your computer and use it in GitHub Desktop.
Homebrew without sudo-Aub's fork with a few minor changes from kenchan's fork
#!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
# chmod +x install.rb
# ./install.rb
YOUR_HOME = ENV['HOME']
HOMEBREW_PREFIX = "#{YOUR_HOME}/usr/local"
system "mkdir -p #{HOMEBREW_PREFIX}"
HOMEBREW_CACHE = "#{YOUR_HOME}/Library/Caches/Homebrew"
HOMEBREW_REPO = 'https://github.com/Homebrew/brew'
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
#ohai "/usr/bin/sudo", *args
#system "/usr/bin/sudo", *args
system *args
end
def getc # NOTE only tested on OS X
system "/bin/stty raw -echo"
if STDIN.respond_to?(:getbyte)
STDIN.getbyte
else
STDIN.getc
end
ensure
system "/bin/stty -raw echo"
end
def wait_for_user
puts
puts "Press RETURN to continue or any other key to abort"
c = getc
# we test for \r and \n because some stuff does \r instead
abort unless c == 13 or c == 10
end
module Version
def <=>(other)
split(".").map { |i| i.to_i } <=> other.split(".").map { |i| i.to_i }
end
end
def macos_version
@macos_version ||= `/usr/bin/sw_vers -productVersion`.chomp[/10\.\d+/].extend(Version)
end
def git
@git ||= if ENV['GIT'] and File.executable? ENV['GIT']
ENV['GIT']
elsif Kernel.system '/usr/bin/which -s git'
'git'
else
exe = `xcrun -find git 2>/dev/null`.chomp
exe if $? && $?.success? && !exe.empty? && File.executable?(exe)
end
return unless @git
# Github only supports HTTPS fetches on 1.7.10 or later:
# https://help.github.com/articles/https-cloning-errors
`#{@git} --version` =~ /git version (\d\.\d+\.\d+)/
return if $1.nil? or $1.extend(Version) < "1.7.10"
@git
end
def chmod?(d)
File.directory?(d) && !(File.readable?(d) && File.writable?(d) && File.executable?(d))
end
def chgrp?(d)
!File.grpowned?(d)
end
# Invalidate sudo timestamp before exiting
#at_exit { Kernel.system "/usr/bin/sudo", "-k" }
# The block form of Dir.chdir fails later if Dir.CWD doesn't exist which I
# guess is fair enough. Also sudo prints a warning message for no good reason
system "cd #{YOUR_HOME}/usr"
####################################################################### script
abort "See Linuxbrew: http://brew.sh/linuxbrew/" if /linux/i === RUBY_PLATFORM
abort "MacOS too old, see: https://github.com/mistydemeo/tigerbrew" if macos_version < "10.5"
abort "Don't run this as root!" if Process.uid == 0
#abort <<-EOABORT unless `groups`.split.include? "admin"
#This script requires the user #{ENV['USER']} to be an Administrator. If this
#sucks for you then you can install Homebrew in your home directory or however
#you please; please refer to our homepage. If you still want to use this script
#set your user to be an Administrator in System Preferences or `su' to a
#non-root user with Administrator privileges.
#EOABORT
abort <<-EOABORT unless Dir["#{HOMEBREW_PREFIX}/.git/*"].empty?
It appears Homebrew is already installed. If your intent is to reinstall you
should do the following before running this installer again:
rm -rf #{HOMEBREW_PREFIX}/Cellar #{HOMEBREW_PREFIX}/.git && brew cleanup
EOABORT
# Tests will fail if the prefix exists, but we don't have execution
# permissions. Abort in this case.
abort <<-EOABORT if File.directory? HOMEBREW_PREFIX and not File.executable? HOMEBREW_PREFIX
The Homebrew prefix, #{HOMEBREW_PREFIX}, exists but is not searchable. If this is
not intentional, please restore the default permissions and try running the
installer again:
sudo chmod 775 #{HOMEBREW_PREFIX}
EOABORT
abort <<-EOABORT if `/usr/bin/xcrun clang 2>&1` =~ /license/ && !$?.success?
You have not agreed to the Xcode license.
Before running the installer again please agree to the license by opening
Xcode.app or running:
sudo xcodebuild -license
EOABORT
ohai "This script will install:"
puts "#{HOMEBREW_PREFIX}/bin/brew"
puts "#{HOMEBREW_PREFIX}/Library/..."
puts "#{HOMEBREW_PREFIX}/share/man/man1/brew.1"
chmods = %w( . bin etc include lib lib/pkgconfig Library sbin share var var/log 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| File.join(HOMEBREW_PREFIX, d) }.select { |d| chmod?(d) }
chgrps = chmods.select { |d| chgrp?(d) }
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}admin#{Tty.reset}:"
puts(*chgrps)
end
wait_for_user if STDIN.tty?
if File.directory? HOMEBREW_PREFIX
sudo "/bin/chmod", "g+rwx", *chmods unless chmods.empty?
ohai "/usr/bin/chgrp", "admin", *chgrps unless chgrps.empty?
sudo "/usr/bin/chgrp", "admin", *chgrps unless chgrps.empty?
else
sudo "/bin/mkdir", HOMEBREW_PREFIX
sudo "/bin/chmod", "g+rwx", HOMEBREW_PREFIX
# the group is set to wheel by default for some reason
ohai "/usr/bin/chgrp", "admin", HOMEBREW_PREFIX
sudo "/usr/bin/chgrp", "admin", HOMEBREW_PREFIX
end
sudo "/bin/mkdir", HOMEBREW_CACHE unless File.directory? HOMEBREW_CACHE
sudo "/bin/chmod", "g+rwx", HOMEBREW_CACHE if chmod? HOMEBREW_CACHE
if macos_version >= "10.9"
developer_dir = `/usr/bin/xcode-select -print-path 2>/dev/null`.chomp
if developer_dir.empty? || !File.exist?("#{developer_dir}/usr/bin/git")
ohai "Installing the Command Line Tools (expect a GUI popup):"
sudo "/usr/bin/xcode-select", "--install"
puts "Press any key when the installation has completed."
getc
end
end
ohai "Downloading and installing Homebrew..."
Dir.chdir HOMEBREW_PREFIX do
if git
# we do it in four steps to avoid merge errors when reinstalling
system git, "init", "-q"
# "git remote add" will fail if the remote is defined in the global config
system git, "config", "remote.origin.url", HOMEBREW_REPO
system git, "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"
args = git, "fetch", "origin", "master:refs/remotes/origin/master", "-n"
args << "--depth=1" if ARGV.include? "--fast"
system(*args)
system git, "reset", "--hard", "origin/master"
else
# -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
# we use -k for curl because Leopard has a bunch of bad SSL certificates
curl_flags = "fsSL"
curl_flags << "k" if macos_version <= "10.5"
system "/bin/bash -o pipefail -c '/usr/bin/curl -#{curl_flags} #{HOMEBREW_REPO}/tarball/master | /usr/bin/tar xz -m --strip 1'"
end
end
warn "#{HOMEBREW_PREFIX}/bin is not in your PATH." unless ENV['PATH'].split(':').include? "#{HOMEBREW_PREFIX}/bin"
ohai "Installation successful!"
ohai "Next steps"
if macos_version < "10.9" and macos_version > "10.6"
`/usr/bin/cc --version 2> /dev/null` =~ %r[clang-(\d{2,})]
version = $1.to_i
puts "Install the #{Tty.white}Command Line Tools for Xcode#{Tty.reset}: https://developer.apple.com/downloads" if version < 425
else
puts "Install #{Tty.white}Xcode#{Tty.reset}: https://developer.apple.com/xcode" unless File.exist? "/usr/bin/cc"
end
puts "You must set `export PATH=#{HOMEBREW_PREFIX}/bin:$PATH` to your shell??"
puts "Run `brew update`??"
puts "Run `brew doctor` #{Tty.white}before#{Tty.reset} you install anything"
puts "Run `brew help` to get started"
@devinrhode2
Copy link
Author

devinrhode2 commented Sep 28, 2020

Running with the chgrp admin commands was a bit un-eventful, and appeared to succeed, even though I have no admin rights:

$ ./install.rb
==> This script will install:
/Users/devinrhode3/usr/local/bin/brew
/Users/devinrhode3/usr/local/Library/...
/Users/devinrhode3/usr/local/share/man/man1/brew.1

Press RETURN to continue or any other key to abort
MBP:~ devinrhode3$

I then uncommented the second half of the file, after the chgrp commands, and it all appeared to work fine!

MBP:~ devinrhode3$ ./install.rb
==> This script will install:
/Users/devinrhode3/usr/local/bin/brew
/Users/devinrhode3/usr/local/Library/...
/Users/devinrhode3/usr/local/share/man/man1/brew.1

Press RETURN to continue or any other key to abort
==> Downloading and installing Homebrew...
remote: Enumerating objects: 53, done.
remote: Counting objects: 100% (53/53), done.
remote: Compressing objects: 100% (38/38), done.
remote: Total 154441 (delta 26), reused 33 (delta 15), pack-reused 154388
Receiving objects: 100% (154441/154441), 39.40 MiB | 35.96 MiB/s, done.
Resolving deltas: 100% (114162/114162), done.
From https://github.com/Homebrew/brew
 * [new branch]      master     -> origin/master
Checking out files: 100% (2184/2184), done.
HEAD is now at 91d58cdc9 Merge pull request #8808 from reitermarkus/cask-install
==> Installation successful!
==> Next steps
You must set `exports PATH=/Users/devinrhode3/usr/local/bin:$PATH` to your shell??
Run `brew update`??
Run `brew doctor` before you install anything
Run `brew help` to get started

@devinrhode2
Copy link
Author

Unfortunately, brew update failed:

$ brew update
==> Downloading https://homebrew.bintray.com/bottles-portable-ruby/portable-ruby-2.6.3_2.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring portable-ruby-2.6.3_2.yosemite.bottle.tar.gz
==> Homebrew has enabled anonymous aggregate formula and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics have been recorded yet (or will be during this `brew` run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations
==> Tapping homebrew/core
Cloning into '/Users/devinrhode3/usr/local/Library/Taps/homebrew/homebrew-core'...
fatal: unable to access 'https://github.com/Homebrew/homebrew-core/': transfer closed with outstanding read data remaining
Error: Failure while executing; `git clone https://github.com/Homebrew/homebrew-core /Users/devinrhode3/usr/local/Library/Taps/homebrew/homebrew-core` exited with 128.
Error: Failure while executing; `/Users/devinrhode3/usr/local/bin/brew tap homebrew/core` exited with 1.

If you are stuck waiting at this:

==> Tapping homebrew/core
Cloning into '/Users/devinrhode3/usr/local/Library/Taps/homebrew/homebrew-core'...

Just kill the script. Run brew doctor first. (brew update was only something added in ken's fork)

@devinrhode2
Copy link
Author

Inevitably, brew doctor shows this warning:

Warning: Your Homebrew's prefix is not /usr/local.
Some of Homebrew's bottles (binary packages) can only be used with the default
prefix (/usr/local).
You will encounter build failures with some formulae.
Please create pull requests instead of asking for help on Homebrew's GitHub,
Discourse, Twitter or IRC. You are responsible for resolving any issues you
experience while you are running this unsupported configuration.

We'll cross our fingers and march onward.

(note brew doctor originally showed this output:

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: The following directories do not exist:
/Users/devinrhode3/usr/local/Cellar
/Users/devinrhode3/usr/local/Frameworks
/Users/devinrhode3/usr/local/etc
/Users/devinrhode3/usr/local/include
/Users/devinrhode3/usr/local/lib
/Users/devinrhode3/usr/local/opt
/Users/devinrhode3/usr/local/sbin
/Users/devinrhode3/usr/local/share
/Users/devinrhode3/usr/local/var/homebrew/linked

You should create these directories and change their ownership to your account.
  sudo mkdir -p /Users/devinrhode3/usr/local/Cellar /Users/devinrhode3/usr/local/Frameworks /Users/devinrhode3/usr/local/etc /Users/devinrhode3/usr/local/include /Users/devinrhode3/usr/local/lib /Users/devinrhode3/usr/local/opt /Users/devinrhode3/usr/local/sbin /Users/devinrhode3/usr/local/share /Users/devinrhode3/usr/local/var/homebrew/linked
  sudo chown -R $(whoami) /Users/devinrhode3/usr/local/Cellar /Users/devinrhode3/usr/local/Frameworks /Users/devinrhode3/usr/local/etc /Users/devinrhode3/usr/local/include /Users/devinrhode3/usr/local/lib /Users/devinrhode3/usr/local/opt /Users/devinrhode3/usr/local/sbin /Users/devinrhode3/usr/local/share /Users/devinrhode3/usr/local/var/homebrew/linked
==> Tapping homebrew/core
Cloning into '/Users/devinrhode3/usr/local/Library/Taps/homebrew/homebrew-core'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 797309 (delta 1), reused 2 (delta 0), pack-reused 797303
Receiving objects: 100% (797309/797309), 322.18 MiB | 35.31 MiB/s, done.
Resolving deltas: 100% (534014/534014), done.
Checking out files: 100% (5483/5483), done.
Tapped 2 commands and 5225 formulae (5,507 files, 353MB).

Warning: Your Homebrew's prefix is not /usr/local.
Some of Homebrew's bottles (binary packages) can only be used with the default
prefix (/usr/local).
You will encounter build failures with some formulae.
Please create pull requests instead of asking for help on Homebrew's GitHub,
Discourse, Twitter or IRC. You are responsible for resolving any issues you
experience while you are running this unsupported configuration.

MBP:~ devinrhode3$ mkdir -p /Users/devinrhode3/usr/local/Cellar /Users/devinrhode3/usr/local/Frameworks /Users/devinrhode3/usr/local/etc /Users/devinrhode3/usr/local/include /Users/devinrhode3/usr/local/lib /Users/devinrhode3/usr/local/opt /Users/devinrhode3/usr/local/sbin /Users/devinrhode3/usr/local/share /Users/devinrhode3/usr/local/var/homebrew/linked
MBP:~ devinrhode3$ chown -R $(whoami) /Users/devinrhode3/usr/local/Cellar /Users/devinrhode3/usr/local/Frameworks /Users/devinrhode3/usr/local/etc /Users/devinrhode3/usr/local/include /Users/devinrhode3/usr/local/lib /Users/devinrhode3/usr/local/opt /Users/devinrhode3/usr/local/sbin /Users/devinrhode3/usr/local/share /Users/devinrhode3/usr/local/var/homebrew/linked

I ran the suggested commands without sudo, and that cleared those warnings just fine.

@devinrhode2
Copy link
Author

(Updating my script with the final successful version)

@devinrhode2
Copy link
Author

BTW this was run on mojave in the latest iTerm (super basic shell through, no zsh stuff setup yet, of course, since I'd like to install zsh via homebrew)

@devinrhode2
Copy link
Author

devinrhode2 commented Sep 28, 2020

I also recommend most people consider running some of the commands here to update to the latest openssl before running brew update for the first time: https://superuser.com/a/1338769/88226

@devinrhode2
Copy link
Author

devinrhode2 commented Sep 28, 2020

brew install git is throwing a xcode-style popup to install "javac", you can click "more info" which goes to an adobe flash download page... or "ok" which does nothing. Terminal is getting stuck here

==> Installing dependencies for git: gettext and pcre2
==> Installing git dependency: gettext
==> ./configure --prefix=/Users/devinrhode3/usr/local/Cellar/gettext/0.21 --with-included-gettext gl_cv_func_
^C

@devinrhode2
Copy link
Author

Somehow, got everything updated. Git is latest version, etc. One thing: I couldn't change my default shell to the brew-installed one, would just say something like "can't change shell to non-standard shell". So, I ended up uninstalling zsh and using the default /bin/zsh in mojave, which is 5.3 instead of 5.8. All of my (oldish) zgen stuff works fine, with the latest zgen. Basically my terminal works as expected.

@devinrhode2
Copy link
Author

For homebrew cask users, you'll want to add this to your ~/.profile:

export HOMEBREW_CASK_OPTS="--appdir=~/Users/devinrhode3/Applications --fontdir=/Users/devinrhode3/Library/Fonts"

@devinrhode2
Copy link
Author

upgrading packages still works. brew update and brew upgrade <pkg>. Might need to turn off vpn if you got one of those

@Neopect
Copy link

Neopect commented May 4, 2021

I'm on Catalina with zsh and I found a couple of things to get it to work.

 puts "You must set `exports PATH=#{HOMEBREW_PREFIX}/bin:$PATH` to your shell??"

Should be export

Then to edit your PATH in .zshrc so it works after using another tty instance or window is to use this path statement,

export PATH=$HOME/bin:$HOME/usr/local/bin:$PATH

The $HOME/usr/local/bin being the most important one.
The one you provided didn't work on my system and the default zsh one didn't work either. So after some messing around, I found $HOME/usr/local/bin to be the solution when you try to call it.

Otherwise fantastic script modifications to get it working

@devinrhode2
Copy link
Author

Thanks for the feedback @Neopect. I corrected exports to export. The rest I'll have to evaluate once I recover my shell profile code.

Running this on Monterey again...

Copied whole script as-is

./install.rb:113:in `<main>': undefined method `<' for nil:NilClass (NoMethodError)

Seems like that line checks if the macOS version is too old, and then recommends tigerbrew...

It's probably failing cause my version is newer than expected. So I'm removing that line (and replacing with newline, so line numbers stay the same).

=====

Ran again:

./install.rb       
==> This script will install:
/Users/devinrhode2/usr/local/bin/brew
/Users/devinrhode2/usr/local/Library/...
/Users/devinrhode2/usr/local/share/man/man1/brew.1

Press RETURN to continue or any other key to abort
./install.rb:180:in `<main>': undefined method `>=' for nil:NilClass (NoMethodError)

...

So now I think any version comparison for the macos_version is going to be buggy.

@devinrhode2
Copy link
Author

Ok... Giving this a try...

#!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby

# chmod +x install.rb
# ./install.rb

YOUR_HOME = ENV['HOME']
HOMEBREW_PREFIX = "#{YOUR_HOME}/usr/local"
system "mkdir -p #{HOMEBREW_PREFIX}"
HOMEBREW_CACHE = "#{YOUR_HOME}/Library/Caches/Homebrew"
HOMEBREW_REPO = 'https://github.com/Homebrew/brew'

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
  #ohai "/usr/bin/sudo", *args
  #system "/usr/bin/sudo", *args
  system *args
end

def getc  # NOTE only tested on OS X
  system "/bin/stty raw -echo"
  if STDIN.respond_to?(:getbyte)
    STDIN.getbyte
  else
    STDIN.getc
  end
ensure
  system "/bin/stty -raw echo"
end

def wait_for_user
  puts
  puts "Press RETURN to continue or any other key to abort"
  c = getc
  # we test for \r and \n because some stuff does \r instead
  abort unless c == 13 or c == 10
end

module Version
  def <=>(other)
    split(".").map { |i| i.to_i } <=> other.split(".").map { |i| i.to_i }
  end
end

def macos_version
  @macos_version ||= `/usr/bin/sw_vers -productVersion`.chomp[/10\.\d+/].extend(Version)
end

def git
  @git ||= if ENV['GIT'] and File.executable? ENV['GIT']
    ENV['GIT']
  elsif Kernel.system '/usr/bin/which -s git'
    'git'
  else
    exe = `xcrun -find git 2>/dev/null`.chomp
    exe if $? && $?.success? && !exe.empty? && File.executable?(exe)
  end

  return unless @git
  # Github only supports HTTPS fetches on 1.7.10 or later:
  # https://help.github.com/articles/https-cloning-errors
  `#{@git} --version` =~ /git version (\d\.\d+\.\d+)/
  return if $1.nil? or $1.extend(Version) < "1.7.10"

  @git
end

def chmod?(d)
  File.directory?(d) && !(File.readable?(d) && File.writable?(d) && File.executable?(d))
end

def chgrp?(d)
  !File.grpowned?(d)
end

# Invalidate sudo timestamp before exiting
#at_exit { Kernel.system "/usr/bin/sudo", "-k" }

# The block form of Dir.chdir fails later if Dir.CWD doesn't exist which I
# guess is fair enough. Also sudo prints a warning message for no good reason
system "cd #{YOUR_HOME}/usr"

####################################################################### script
abort "See Linuxbrew: http://brew.sh/linuxbrew/" if /linux/i === RUBY_PLATFORM

abort "Don't run this as root!" if Process.uid == 0
#abort <<-EOABORT unless `groups`.split.include? "admin"
#This script requires the user #{ENV['USER']} to be an Administrator. If this
#sucks for you then you can install Homebrew in your home directory or however
#you please; please refer to our homepage. If you still want to use this script
#set your user to be an Administrator in System Preferences or `su' to a
#non-root user with Administrator privileges.
#EOABORT
abort <<-EOABORT unless Dir["#{HOMEBREW_PREFIX}/.git/*"].empty?
It appears Homebrew is already installed. If your intent is to reinstall you
should do the following before running this installer again:
    rm -rf #{HOMEBREW_PREFIX}/Cellar #{HOMEBREW_PREFIX}/.git && brew cleanup
EOABORT
# Tests will fail if the prefix exists, but we don't have execution
# permissions. Abort in this case.
abort <<-EOABORT if File.directory? HOMEBREW_PREFIX and not File.executable? HOMEBREW_PREFIX
The Homebrew prefix, #{HOMEBREW_PREFIX}, exists but is not searchable. If this is
not intentional, please restore the default permissions and try running the
installer again:
    sudo chmod 775 #{HOMEBREW_PREFIX}
EOABORT
abort <<-EOABORT if `/usr/bin/xcrun clang 2>&1` =~ /license/ && !$?.success?
You have not agreed to the Xcode license.
Before running the installer again please agree to the license by opening
Xcode.app or running:
    sudo xcodebuild -license
EOABORT

ohai "This script will install:"
puts "#{HOMEBREW_PREFIX}/bin/brew"
puts "#{HOMEBREW_PREFIX}/Library/..."
puts "#{HOMEBREW_PREFIX}/share/man/man1/brew.1"

chmods = %w( . bin etc include lib lib/pkgconfig Library sbin share var var/log 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| File.join(HOMEBREW_PREFIX, d) }.select { |d| chmod?(d) }
chgrps = chmods.select { |d| chgrp?(d) }

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}admin#{Tty.reset}:"
  puts(*chgrps)
end

wait_for_user if STDIN.tty?

if File.directory? HOMEBREW_PREFIX
  sudo "/bin/chmod", "g+rwx", *chmods unless chmods.empty?
  ohai "/usr/bin/chgrp", "admin", *chgrps unless chgrps.empty?
  sudo "/usr/bin/chgrp", "admin", *chgrps unless chgrps.empty?
else
  sudo "/bin/mkdir", HOMEBREW_PREFIX
  sudo "/bin/chmod", "g+rwx", HOMEBREW_PREFIX
  # the group is set to wheel by default for some reason
  ohai "/usr/bin/chgrp", "admin", HOMEBREW_PREFIX
  sudo "/usr/bin/chgrp", "admin", HOMEBREW_PREFIX
end

sudo "/bin/mkdir", HOMEBREW_CACHE unless File.directory? HOMEBREW_CACHE
sudo "/bin/chmod", "g+rwx", HOMEBREW_CACHE if chmod? HOMEBREW_CACHE











ohai "Downloading and installing Homebrew..."
Dir.chdir HOMEBREW_PREFIX do
  if git
    # we do it in four steps to avoid merge errors when reinstalling
    system git, "init", "-q"

    # "git remote add" will fail if the remote is defined in the global config
    system git, "config", "remote.origin.url", HOMEBREW_REPO
    system git, "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"

    args = git, "fetch", "origin", "master:refs/remotes/origin/master", "-n"
    args << "--depth=1" if ARGV.include? "--fast"
    system(*args)

    system git, "reset", "--hard", "origin/master"
  else
    # -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
    # we use -k for curl because Leopard has a bunch of bad SSL certificates
    curl_flags = "fsSL"

    system "/bin/bash -o pipefail -c '/usr/bin/curl -#{curl_flags} #{HOMEBREW_REPO}/tarball/master | /usr/bin/tar xz -m --strip 1'"
  end
end

warn "#{HOMEBREW_PREFIX}/bin is not in your PATH." unless ENV['PATH'].split(':').include? "#{HOMEBREW_PREFIX}/bin"

ohai "Installation successful!"
ohai "Next steps"












puts "You must set `exports PATH=#{HOMEBREW_PREFIX}/bin:$PATH` to your shell??"
puts "Run `brew update`??"
puts "Run `brew doctor` #{Tty.white}before#{Tty.reset} you install anything"
puts "Run `brew help` to get started"

@devinrhode2
Copy link
Author

The /usr/bin/cc file already exists on my machine, so deleted the last if/else chunk doing some comparisons...

@devinrhode2
Copy link
Author

FYI:

/usr/bin/sw_vers -productVersion
12.6.3

@devinrhode2
Copy link
Author

I manually installed XCode command line tools

@devinrhode2
Copy link
Author

devinrhode2 commented Mar 7, 2023

Seems to have worked!

% ./install.rb                    
==> This script will install:
/Users/devinrhode2/usr/local/bin/brew
/Users/devinrhode2/usr/local/Library/...
/Users/devinrhode2/usr/local/share/man/man1/brew.1

Press RETURN to continue or any other key to abort
==> Downloading and installing Homebrew...
remote: Enumerating objects: 231128, done.
remote: Counting objects: 100% (576/576), done.
remote: Compressing objects: 100% (382/382), done.
remote: Total 231128 (delta 217), reused 480 (delta 171), pack-reused 230552
Receiving objects: 100% (231128/231128), 66.39 MiB | 44.00 MiB/s, done.
Resolving deltas: 100% (169751/169751), done.
From https://github.com/Homebrew/brew
 * [new branch]          master     -> origin/master
HEAD is now at 3e2c713c9 Merge pull request #14870 from nandahkrishna/fix-requirement
Warning: /Users/devinrhode2/usr/local/bin is not in your PATH.
==> Installation successful!
==> Next steps
You must set `exports PATH=/Users/devinrhode2/usr/local/bin:$PATH` to your shell??
Run `brew update`??
Run `brew doctor` before you install anything
Run `brew help` to get started
% 

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