Overview:
Installing Homebrew on macOS can be challenging due to its typical reliance on the command-line interface using a curl
command. However, an alternative approach is available via a .pkg
installer file, which simplifies the process for automated or script-based installations. This method uses GitHub to dynamically retrieve the latest Homebrew.pkg
file, enabling easier, more reliable installations without requiring interactive terminal input.
Homebrew Installation:
This script automates the installation of Homebrew on macOS, ensuring that the system prerequisites are met. Before attempting to install Homebrew, the script checks for the presence of Command Line Tools (CLT), which are required for Homebrew to function correctly. If CLT is not installed, the script will initiate its installation until CLT is installed, since it is needed to allow the Homebrew setup to proceed smoothly.
#!/bin/bash # Function to check if Command Line Tools are installed check_clt() { if ! xcode-select -p &>/dev/null; then echo "########## XCODE COMMAND LINE TOOL #############" echo "Command Line Tools not found." echo "Kindly Install the latest version of Xcode from the App Store" echo "Once Installed, re-run the script...." exit 1 else echo "########## XCODE COMMAND LINE TOOL #############" echo "Command Line Tools are already installed." echo "Xcode Command Line Tool path: $(xcode-select -p)" install_homebrew fi }
# Function to install Homebrew via PKG install_homebrew() { # Figure out logged-in user loggedInUser=$(stat -f %Su /dev/console)
# Figure out home directory. homeDir=$(dscl . -read /users/$loggedInUser | grep NFSHomeDirectory | awk '{print $2}')
# Create log filename with the current date log_file="/Library/Addigy/ansible/packages/Charles HomeBrew Installation (1.3)/install_HomeBrew_$(date +'%Y-%m-%d').log"
# Fetch the latest Homebrew PKG URL download_URL=$(/usr/bin/curl -fs "https://api.github.com/repos/Homebrew/brew/releases/latest" | \ awk -F '"' '/browser_download_url/ && /pkg/ { print $4 }')
if [[ -z "$download_URL" ]]; then echo "Failed to fetch the Homebrew download URL. Exiting." exit 1 fi
# Download the Homebrew PKG echo "" echo "############# DOWNLOADING HOMEBREW PKG FILE #################" echo "Downloading Homebrew PKG from: $download_URL" pkg_download_file="Homebrew-latest.pkg" /usr/bin/curl -L "$download_URL" -o "$pkg_download_file" >> "$log_file" 2>&1
if [ ! -f "$pkg_download_file" ]; then echo "Error: Failed to download the Homebrew PKG." exit 1 fi
# Install the Homebrew PKG echo "" echo "############# INSTALLING HOMEBREW PKG FILE #################" echo "Installing Homebrew..." sudo installer -pkg "$pkg_download_file" -target / -verbose >> "$log_file" 2>&1
if [ $? -eq 0 ]; then echo "Homebrew installed successfully." else echo "Error: Homebrew installation failed." exit 1 fi
# Clean up the downloaded PKG file echo "" echo "############# CLEANING HOMEBREW PKG FILE #################" echo "Cleaning up downloaded PKG..." rm -f "$pkg_download_file"
# Determine Homebrew installation path based on architecture if [[ "$(arch)" == "i386" ]]; then echo "" echo "######### ARCH TYPE #############" echo "Arch: Intel" homebrew_dir="/usr/local/Homebrew/bin/brew" else echo "" echo "######### ARCH TYPE #############" echo "Arch: Apple Silicon" homebrew_dir="/opt/homebrew/bin/brew" fi
# Check if Homebrew is installed by looking for its binary in common installation path if [[ -f "$homebrew_dir" ]]; then echo "" echo "#############HOMEBREW INFORMATION ###################" echo "Homebrew is installed." echo "PATH: $homebrew_dir"
# Grabbing the version for confrimation # Homebrew version sudo -u "$loggedInUser" /bin/bash -c "HOME=$homeDir; $homebrew_dir --version"
# Homebrew getitng register in the log file echo "Homebrew is installed." >> "$log_file" 2>&1 echo "PATH: $homebrew_dir" >> "$log_file" 2>&1
sudo -u "$loggedInUser" /bin/bash -c "HOME=$homeDir; $homebrew_dir --version" >> "$log_file" 2>&1
exit 0
else echo "Homebrew Common installtion Path are not used. Either the installtion is path different or the installtion was incomplete..." exit 1 fi
}
# Main script execution check_clt
|
Breakdown:
- Check for Command Line Tools (check_clt):
- It verifies if Command Line Tools are already installed using xcode-select -p. If they are not found, the script prompts the user to install them manually via the app store. If CLT is installed, the script continues by calling the function to install Homebrew.
- Install Homebrew (install_homebrew):
- Determine Logged-in User: It identifies the logged-in user and their home directory.
- Log File: Create a log file to track the installation process.
- Download Homebrew PKG: The script fetches the latest Homebrew package from GitHub using the API and downloads the .pkg file.
- Install Homebrew: It installs Homebrew using the downloaded package. The process is logged for auditing purposes.
- Cleanup: After installation, it deletes the downloaded package.
- Architecture Detection: Based on the system's architecture (Intel or Apple Silicon), it identifies where Homebrew should be installed (/usr/local for Intel, /opt/homebrew for Apple Silicon).
- Homebrew Verification: It verifies that Homebrew was installed successfully by checking for its binary, displaying the version, and logging the information.
- Main Execution:
- The script starts by calling check_clt, which then either exits if CLT is not present or proceeds to install Homebrew if they are.
|
In summary, this script ensures that Command Line Tools are installed, fetches the latest Homebrew package, installs it, and verifies the installation based on the system's architecture.
Log File Example & Results:
Conditional Script:
Depending on the machine's architectural type, the condition script will check to see if Homebrew is present and then decide whether or not the installation is required. The logic for the installation does not use exit 0 as the default exit for installing this program, hence this will now be regarded as a special conditional script.
#!/bin/bash
# Figure out logged-in user loggedInUser=$(stat -f %Su /dev/console)
# Figure out home dir homeDir=$(dscl . -read /users/$loggedInUser | grep NFSHomeDirectory | awk '{print $2}')
# Determine Homebrew installation path based on architecture if [[ "$(arch)" == "i386" ]]; then homebrew_dir="/usr/local/Homebrew/bin/brew" else homebrew_dir="/opt/homebrew/bin/brew" fi
# Check if Homebrew is installed by looking for its binary in common installation paths if [[ -f "$homebrew_dir" ]]; then echo "" echo "#############HOMEBREW INFORMATION ###################" echo "Homebrew is installed." echo "PATH: $homebrew_dir"
# Grabbing the version for confirmation # Homebrew version sudo -u "$loggedInUser" /bin/bash -c "HOME=$homeDir; $homebrew_dir --version" exit 0 else echo "Homebrew is not installed." exit 1 fi
|
This script performs the following tasks:
-
Determine the Logged-in User: It retrieves the currently logged-in user's username using stat
.
-
Find the User's Home Directory: It uses dscl
to find the home directory path of the logged-in user.
-
Determine Homebrew Installation Path: It checks the system architecture using arch
to set the appropriate Homebrew installation path:
- For Intel architecture (
i386
), it sets the path to /usr/local/Homebrew/bin/brew
.
- For Apple Silicon, it sets the path to
/opt/homebrew/bin/brew
.
-
Check Homebrew Installation: The script checks if the Homebrew binary exists at the determined path:
- If it exists, it prints information about the Homebrew installation, including its path and version (using the logged-in user's context).
- If it does not exist, it outputs a message indicating that Homebrew is not installed.
-
Exit Status: The script exits with a status of 0
if Homebrew is installed and 1
if it is not.
Removal Script:
#!/bin/bash # Figure out logged-in user loggedInUser=$(stat -f %Su /dev/console)
# Figure out home dir homeDir=$(dscl . -read /users/$loggedInUser | grep NFSHomeDirectory | awk '{print $2}')
# Create log filename with the current date log_file="/Library/Addigy/ansible/packages/Charles HomeBrew Installation (1.3)/uninstall_HomeBrew_$(date +'%Y-%m-%d').log"
# Performing the uninstall of Homebrew on the device # Run the Homebrew uninstall script as the logged-in user yes | sudo /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)" >> "$log_file" 2>&1
if [ $? -eq 0 ]; then # The Uninstall script executed successfully echo "Homebrew was uninstalled successfully..." echo "Performing one final check before finishing..." echo "Homebrew was uninstalled successfully..." >> "$log_file" 2>&1 echo "Performing one final check before finishing..." >> "$log_file" 2>&1
# Determine architecture type if [[ "$(arch)" == "i386" ]]; then echo "" echo "######### ARCH TYPE #############" echo "Arch: Intel" homebrew_dir="/usr/local/Homebrew/bin/brew" else echo "" echo "######### ARCH TYPE #############" echo "Arch: Apple Silicon" homebrew_dir="/opt/homebrew/bin/brew" fi
# Check if Homebrew is installed by looking for its binary in common installation path if [[ -f "$homebrew_dir" ]]; then echo "" echo "############# HOMEBREW INFORMATION ###################" echo "Homebrew is still installed." echo "PATH: $homebrew_dir"
# Log Homebrew version sudo -u "$loggedInUser" /bin/bash -c "HOME=$homeDir; $homebrew_dir --version" >> "$log_file" 2>&1
# Indicate Homebrew is still installed in the log file echo "Homebrew is still installed." >> "$log_file" 2>&1 echo "PATH: $homebrew_dir" >> "$log_file" 2>&1 exit 1 else echo "Homebrew is not installed at $homebrew_dir." echo "Homebrew is not installed at $homebrew_dir." >> "$log_file" 2>&1 exit 0 fi else echo "The Uninstall Script was not able to execute. Kindly review the log file attached to the script..." >> "$log_file" 2>&1 exit 1 fi
|
This script automates the uninstallation of Homebrew on macOS, checks whether it was successfully removed, and logs the process. Here's a detailed breakdown of the script:
Breakdown:
- Determine the Logged-in User:
- The script retrieves the currently logged-in user using stat, storing the result in the variable loggedInUser.
- Determine the Home Directory:
- The home directory of the logged-in user is obtained using dscl and stored in the homeDir variable.
- Log File Creation:
- A log file is created to record the uninstallation process, with the filename containing the current date. The log file is saved in /Library/Addigy/ansible/packages/Charles HomeBrew Installation (1.3).
- Uninstall Homebrew:
- The script executes the Homebrew uninstall script from the official repository using curl. The yes command is piped into the uninstall script to automatically confirm any prompts.
- The output of the uninstall process is redirected to the log file.
- Check for Success:
- If the uninstall command executes successfully ($? is 0), the script logs the success message and performs a final check to verify if Homebrew is still installed.
- Architecture Detection:
- The script determines the system's architecture using the arch command:
- If Intel (i386), it sets the homebrew_dir variable to the Intel path.
- If Apple Silicon, it sets the path for Apple Silicon.
- Check for Homebrew Installation:
- The script checks if the Homebrew binary exists at the determined homebrew_dir.
- If Homebrew is still installed, it logs this information, including the installation path, and retrieves the Homebrew version using the --version flag, which is also logged.
- If Homebrew is not found, it logs that Homebrew was not installed at the specified path.
- Exit Codes:
- The script exits with an appropriate status code based on the outcome:
- Exit 1: If the uninstallation was successful but Homebrew is still detected as installed or if the uninstall script failed to execute.
- Exit 0: If Homebrew was successfully uninstalled and is not found.
Summary:
This script handles the uninstallation of Homebrew on macOS, logs the process, and checks if Homebrew was successfully removed. It adapts to both Intel and Apple Silicon architectures, logs relevant information, and exits with appropriate status codes to indicate the success or failure of the operation.
The removal will run several processes that the homebrew shell file can eliminate, but Homebrew will provide the option to delete the following homebrew files in their logs, such as the one in the image above that says you might want to do it yourself.