Log In

How to Install Homebrew on Mac

How to Install Homebrew on Mac
11.04.2024
Reading time: 8 min
Hostman Team
Technical writer

The command line is an option for communicating with a computer system without a graphical interface. Instead of clicking on buttons in an interface, the user enters text commands and receives feedback in the same form. Despite the predominance of graphical interfaces, working with the command line continues to be supported by all operating systems. It is used primarily by software developers and system administrators.

This statement is also true for macOS, where it is possible to install additional modules, such as a package manager. A package manager automates software installation, update, and configuration. Such programs support popular file formats and store installed programs in centralized storage.

In this article, we will examine Homebrew, a package manager for macOS that is often used to work with Python, Ruby, Node.js, and other open-source software. We will describe how to install Homebrew on MacOS and use the basic commands for managing packages.

By the way, Hostman App Platform accommodates all commonly used development environments and programming languages.

Prerequisites

  • Any computer or laptop running macOS Catalina or a newer version installed (older versions might still be compatible)
  • Administrator privileges
  • Internet connection

How to Use the Terminal on macOS

The terminal is preinstalled on macOS systems. To access it, you can use search or go to the Utilities folder located in Applications. Double-click on it to launch the utility.

Yet another launch option is through Spotlight, in which you just need to press Space+Command and enter the word Terminal. 

That's it, the terminal is launched, and you can explore the Homebrew tool for macOS.

Installing Xcode Command Line Tools

The Xcode integrated development environment (IDE) is popular among macOS software developers. It is not required for Homebrew to function, but some of the components are quite useful. Therefore, let's install the package with the command:

xcode-select -install

During the installation process, you will be asked to accept the license. Do that, and the installation will continue. The system will automatically download the necessary files and install them. Now, you can get started with Homebrew on MacOS.

Installing and configuring Homebrew

So, let’s get to installing Homebrew on Mac.

First, you must download and execute the script with the following command:

curl -fsSL -o install.sh https://raw.githubusercontent.com/Homebrew/install/master/install.sh

It uses flags:

  • -f or -fail prohibits the output of information in case of a server error;

  • -s or -silent deactivates the progress indicator;

  • -S or -show-error enables the display of an error message if the installation fails;

  • -L or -location enables automatic redirection when the location changes;

  • -o sets the file name for local storage.

Before running the script, you can view its contents:

less install.sh

Then run it:

/bin/bash install.sh

The script will show information about every step and prompt you to confirm actions (by pressing the Y key). This approach allows you to ensure that all the necessary modules have been installed.

Also during the process, the system will prompt you to enter a password. Please note that, as a security measure, it will not be displayed in the terminal while typing it, so be careful. 

Once completed, you will need to place the folder with the executables in the PATH environment variable. The directory can differ: on ARM systems, they're installed to /opt/homebrew, on Intel, to /usr/local/Homebrew.

The file that you need to edit depends on the shell. With Bash, the command is going to be:

nano ~/.bash_profile

If ZSH is used, the editing command will look like:

nano ~/.zshrc

Add the following lines to the file, at the very end.

For Intel:

# Adding Homebrew's executable directory to the front of the PATH
export PATH=/usr/local/bin:$PATH

For ARM:

export PATH=/opt/homebrew/bin:$PATH

To save the changes, use the key combination Ctrl + O, then press Return, To exit the editor, press Ctrl + X. You will return to the terminal. 

To apply the changes, you can restart the terminal or use the command below. For Bash, it will look like this:

source ~/.bash_profile

For ZSH:

source ~/.zshrc

To make sure that Homebrew is installed correctly, you can use the command:

brew doctor

If everything works as it should, you will see the message: 

Your system is ready to brew

Installing, updating, and removing packages

Now you can use Homebrew to manage packages.

For example, let's install the tree package to view a graphical directory tree. 

brew install tree

The application will automatically update the list of packages and install tree. You will see a similar message on the screen:

Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/tree-1.8.0.catalina.bottle.tar.gz
################################################## ###################### 100.0%
==> Pouring tree-1.8.0.catalina.bottle.tar.gz
   /usr/local/Cellar/tree/1.8.0: 8 files, 117.2KB

The default installation path is the /usr/local directory. This way, the files do not interfere with the installation of operating system updates. 

You can verify that the tree package is available on your computer using the command:

which tree

The system shows the directory in use:

/usr/local/bin/tree

To check the version of the application, use:

tree -version

The screen output will look like this:

tree v1.8.0 (c) 1996 - 2018 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro

If the version is outdated, you can upgrade the package:

brew upgrade tree

It is important to note that the package manager keeps old file versions, which take up storage space. To remove unnecessary copies, use the cleanup command:

brew cleanup

If you no longer need the tree package, you can uninstall it from your computer:

brew uninstall tree

Installing desktop applications

The Homebrew Cask package manager can also install desktop applications. This functionality is built into the utility, so there is no need to install anything additional. As an example, let's install Visual Studio Code.

brew install visual-studio-code

You should see a similar message on the screen:

==> Downloading https://update.code.visualstudio.com/1.58.2/darwin/stable
==> Downloading from https://az764295.vo.msecnd.net/stable/a0479759d6e9ea56afa657e454193f72aef85bd0/VSC
################################################## ###################### 100.0%
==> Verifying SHA-256 checksum for Cask 'visual-studio-code'.
==> Installing Cask visual-studio-code
==> Moving App' Visual Studio Code.app' to '/Applications/Visual Studio Code.app'.
==> Linking Binary' code' to '/usr/local/bin/code'.
   visual-studio-code was successfully installed!

Once the procedure is complete, the application will appear in the Application directory. To remove software that is no longer needed, use the command:

brew uninstall visual-studio-code

The system will confirm its completion with the message:

==> Backing App' Visual Studio Code.app' up to '/usr/local/Caskroom/visual-studio-code/1.58.2/Visual Studio Code.app'
==> Removing App '/Applications/Visual Studio Code.app'
==> Unlinking Binary'/usr/local/bin/code'
==> Purging files for version 1.58.2 of Cask visual-studio-code

Before deleting the application, the system creates a backup copy of the files so that it is easy to roll back if the uninstallation fails. But after successful confirmation of the procedure, the archived copy is deleted automatically.

Uninstalling Homebrew

If the Homebrew package manager is no longer needed, it can be removed by running a special script. First, download it:

curl -fsSL -o uninstall.sh https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh

You can also review the content:

less uninstall.sh

To see the list of supported commands, use the -help flag:

bash uninstall.sh -help

The command will output the following message:

Homebrew Uninstaller
Usage: uninstall.sh [options] -p, --path=PATH Sets Homebrew prefix. Defaults to /usr/local.
--skip-cache-and-logs
Skips removal of HOMEBREW_CACHE and HOMEBREW_LOGS.
-f, --force Uninstall without prompting.
-q, --quiet Suppress all output.
-d, --dry-run Simulate uninstall but don't remove anything.
-h, --help Display this message.

Let's run the command with the -d flag, which displays the script's actions:

bash uninstall.sh -d

On the screen, you will see a list of files to be deleted by the script:

Warning: This script would remove:
/Users/brianhogan/Library/Caches/Homebrew/
/Users/brianhogan/Library/Logs/Homebrew/
/usr/local/Caskroom/
/usr/local/Cellar/
/usr/local/bin/brew -> /usr/local/bin/brew
==> Removing Homebrew installation...
Would delete:
...

If you're sure about removing Homebrew, run:

bash uninstall.sh

The command will uninstall both the Homebrew package manager and the applications that were installed using it.

Conclusions

We looked at how to install Homebrew on macOS and use the package manager to install other applications and remove unnecessary ones. Using Homebrew makes managing your macOS applications easier not only for developers but also for ordinary users.


Share