Are you trying to install Homebrew on Mac?
This step by step guide is for you.
In essence, Homebrew is a free and open-source package manager meant for macOS.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to fix Mac-related queries.
In this context, you will learn how to install Homebrew on Mac.
With Homebrew, the installation of software on Apple’s macOS operating system and Linux is made simplified. Generally, it serves as a command-line App store.
Additionally, it helps you in avoiding the possible security problems associated with using the Sudo command.
Now let us see the steps to get Homebrew installed on Mac.
By default, a terminal application is provided by macOS. So you can access it by going to Finder, navigating to the Applications folder, and then into the "utilities" folder. Then from here, double-click on the Terminal application to open it up.
Xcode is an integrated development environment (IDE) that has software development tools for macOS. You do not need the Xcode for using Homebrew but some of the software and components you will want to install will rely on Xcode's Command Line Tools package.
Execute the below command in the Terminal to download and install these components;
xcode-select --install
During this process, you will be asked to start the installation and then again you will be prompted to accept a software license. After your confirmation, the tools will download and install automatically.
To install Homebrew, you will need to download an installation script and then execute the script.
Start by downloading the script to your local machine. For that, execute the following command;
curl -fsSL -o install.sh https://raw.githubusercontent.com/Homebrew/install/master/install.sh
This command uses the curl to download the Homebrew installation script from Homebrew's Git repository on GitHub.
Before you run the script that you have downloaded from the internet, make sure to review its contents so you know what the script will do. In this case, use the less command to look at the installation script as shown below;
less install.sh
Once you’re comfortable with the contents of the script, execute the script using the bash command as shown below;
/bin/bash install.sh
These scripts will explain what it will do and will prompt you to confirm that you want to do it. This will let you know what Homebrew is going to do to your system before you let it proceed.
Next, a prompt will appear which will ask you to enter the password. However, while entering the password the keystrokes don’t appear for security reasons. Then press the RETURN key once you’ve entered your password.
Press the letter y for "yes" whenever you are prompted to confirm the installation.
Once the installation completes, enter the directory Homebrew uses to store its executables at the front of the PATH environment variable. This will ensure that the Homebrew installations will be called over the tools that macOS includes.
The file you’ll modify depends on which shell you’re using. If you’re using Bash, you’ll use the file ~/.bash_profile:
nano ~/.bash_profile
However, if you are using ZSH, you will open the file ~/.zshrc.
nano ~/.zshrc
After the opening of the file in the Terminal, add the following lines to the end of the file:
# Add Homebrew's executable directory to the front of the PATH
export PATH=/usr/local/bin:$PATH
To save your changes, hold down the CTRL key and the letter O, and when prompted, press the RETURN key. Then exit the editor by holding the CTRL key and pressing X. This will return you to your Terminal prompt.
In order to activate these changes, close and reopen your Terminal app. Then to load the file using the source command.
If you modified .bash_profile, run the following command;
source ~/.bash_profile
If you modified .zshrc, run the following command;
source ~/.zshrc
Now to verify if the Homebrew is set up correctly or not, execute the following command.
brew doctor
As a result, it must display the below output.
You will get an Output like;
Your system is ready to brew.
Now after the Homebrew installation, you can use it to download a package. The tree command will let you see a graphical directory tree and is available via Homebrew.
You can install tree with the brew install command as shown below;
brew install tree
Homebrew will update its list of packages and then download and install the tree command. Also, Homebrew installs files to "/usr/local" by default, so they won’t interfere with future macOS updates.
You can Verify the installation of tree by displaying the command’s location with the which command:
which tree
The output shows that the tree is located in /usr/local/bin:
Output
/usr/local/bin/tree
Similarly, you can run the tree command to see the version.
tree --version
Then to upgrade the version, run the below command.
brew upgrade tree
In case, if you wish to remove the package from the server then run the below command.
brew uninstall tree
Homebrew Cask lets you install desktop applications. To use Homebrew to install Visual Studio Code, run the following command.
brew cask install visual-studio-code
Then to remove it, run the below command.
brew cask uninstall visual-studio-code
In case, if you don’t need the Homebrew anymore then you can use its uninstall script.
Start by downloading the uninstall script with curl shown below;
curl -fsSL -o uninstall.sh https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh
Then review the contents of the script by running the below command.
less uninstall.sh
After verifying the script, execute the script by running the below command.
bash uninstall.sh --help
A list of options displays on the screen.
For instance, you can run the below command to see what the script will do;
bash uninstall.sh -d
As a result, the script will list everything it will delete.
Once you are ready to remove everything, then run the below command to uninstall Homebrew.
bash uninstall.sh
This will remove Homebrew and any programs you’ve installed with it.
This article will guide you on how to install Homebrew on Mac.