Search code examples
bashmacoshomebrewpost-install

Installing brew from macOS app's pre/postinstall script


In my Packages.app's preinstall script I was able to install brew from a modified version of brew's install.sh that removed the sudo check:

#!/bin/bash
##preinstall

if brew ls --versions wget > /dev/null; then
  # The package is installed
  osascript -e 'tell app "Finder" to display dialog "The package is installed"'
else
  # The package is not installed
  osascript -e 'tell app "Finder" to display dialog "The package is not installed"'
    
  /usr/bin/su root -c ./brew-install.sh

fi

exit 0

This is the line I removed from brew's install.sh to get this to work:

if [[ "${EUID:-${UID}}" == "0" ]]; then

I don't know what "${EUID:-${UID}}" is exactly.

This is what I've tried so far unsuccessfully instead of using su root:

# /bin/bash -c ./brew-install.sh

# sudo dseditgroup -o edit -a $USER -t user admin
# sudo /usr/bin/su $USER -c ./brew-install.sh

# /usr/bin/su ladmin -c ./brew-install.sh

# nohup /usr/bin/su $USER -c /bin/bash -c ./brew-install.sh &

Notably trying to add $USER to admin group from here: https://apple.stackexchange.com/a/76096/261453


Any ideas/solutions?


Solution

  • You can install Brew without root by using the "untar install" option for brew: Why brew installation needs sudo access?

    See Brew docs here: https://docs.brew.sh/Installation#untar-anywhere

    If Brew is still blocking for root reasons you can modify Library/Homebrew/bin/brew.sh to remove the root check and then from postinstall, rm the existing brew.sh and replace if with your copy that you added in Packages.app's Script's Addition Resources:

    Packages.app Script Addition Resources