Homebrew: How to Check if a Package is Installed

Updated: January 29, 2024 By: Guest Contributor Post a comment

Introduction

Homebrew, often touted as the missing package manager for macOS (and Linux), streamlines the process of installing and managing software on your operating system. Understanding how to interact with Homebrew is essential for efficient software management. This tutorial will focus on how to verify whether a package is installed on your system using Homebrew, guiding you through basic to more advanced techniques.

Before we dive into checking for installed packages, let’s ensure we have a foundational understanding of Homebrew. It’s a free and open-source software package management system that simplifies the installation of software on Apple’s macOS operating system and Linux. A ‘package’ in Homebrew vocabulary is a piece of software that Homebrew can install for you.

Installation of Homebrew

If you do not have Homebrew installed on your system, open the Terminal and run the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Once you have Homebrew installed, you can proceed with checking for installed packages.

Checking for Installed Packages

Basic Package Check

To check if a specific package is installed, you can use the following command:

brew list | grep 'package-name'

If the package is installed, you’ll see the package name in the output. If there is no output, the package is not installed. Here’s an example using ‘wget’ as the package name:

$ brew list | grep 'wget'
wget

Checking with Brew Commands

Another way to check for an installed package is to use the brew list command along with the package name:

$ brew list 'package-name'

If the package is installed, Homebrew will output the list of files associated with that package. If the package is not installed, Homebrew will return an error message. Here’s what you might see if ‘git’ is installed:

$ brew list 'git'
/usr/local/Cellar/git/2.29.2_1/bin/git
...and so on

Advanced Package Queries

Checking Package Information

You can use brew info to get detailed information about a package, including whether it is installed:

$ brew info 'package-name'

This command provides a comprehensive report of the package, including installation paths, dependencies, and versions. An installed package will show several versions if applicable, and the path(s) where it is installed:

$ brew info wget
wget: stable 1.20.3 (bottled) [keg-only]
Internet file retriever
https://www.gnu.org/software/wget/
/usr/local/Cellar/wget/1.20.3 (48 files, 3.9MB)
Poured from bottle on 2023-04-18 at 11:33:33
...

An uninstalled package will usually have ‘Not installed’ in the summary:

$ brew info someuninstalledpackage
someuninstalledpackage: stable 1.0
Not installed
...

Scripting with Homebrew

For users looking to script the existence check of a Homebrew package, you may rely on the exit status of brew list -1. Upon running the command with a package name, you will receive an exit status of 0 if the package is installed, and 1 if it is not. This can be useful in bash scripts:

if brew list -1 | grep -q ^package-name$; then
echo "The package is installed."
else
echo "The package is not installed."
fi

Naturally, replace ‘package-name’ with the name of the actual package you are querying.

Organizing Package Lists

There might be instances when you have to handle multiple package installations. Homebrew provides a useful command that lists all installed packages. This can help organize the search for a specific package within a large list:

brew list

The resulting list will display all packages installed via Homebrew, which you can then search through manually or use tools like grep to find specific packages.

Conclusion

In this tutorial, we have learned various methods to check if a package is installed in Homebrew. Remember, knowing how to verify package installations with Homebrew is a valuable skill that aids in managing the software on your system. If the methods covered here become second nature, it will streamline your workflow and help keep your system organized.