Homebrew: Uninstalling a package and its dependencies

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

Homebrew is a popular package manager for macOS, and increasingly Linux, which simplifies the process of installing, managing, and uninstalling software packages. While adding packages is straightforward, removing them – particularly when dealing with dependencies can be more complex. In this guide, we’ll walk through the steps required to efficiently uninstall a package and its dependencies using Homebrew.

Understanding Homebrew and its Package Management

Before we delve into the uninstallation process, it’s important to understand how Homebrew handles packages and dependencies. When you install a package with Homebrew, it also installs any required dependencies if they aren’t already present on the system. These dependencies are separate packages that are necessary for the primary package to function correctly.

Listing Installed Packages

Firstly, you’ll want to know what’s installed. Use the following command:

brew list

This command displays all the packages you have installed via Homebrew.

Uninstalling a Single Package

To uninstall a package without removing its dependencies, use the uninstall command followed by the package name:

brew uninstall packageName

Replace packageName with the name of the package you want to remove. For example, to remove wget:

brew uninstall wget

Output:

Uninstalling /usr/local/Cellar/wget/... (50 files, 3.9MB)

Checking for Unused dependencies

After removing a package, you might be left with dependencies that are no longer needed. To find out which dependencies are no longer required by any installed package, Homebrew provides the cleanup command:

brew cleanup --dry-run

The --dry-run flag shows what would be removed, without actually doing it.

Uninstalling a Package and Its Unused Dependencies

If you want to remove a package and its now-unnecessary dependencies, you can combine the uninstall and cleanup commands:

brew uninstall packageName
brew cleanup

This will first remove the specified package, followed by a cleanup of any unused dependencies.

Using the zap Command for a More Thorough Uninstallation

For cask-installed applications (mainly GUI apps), Homebrew includes a zap command, which not only uninstalls the application but also removes related configuration files and preferences:

brew uninstall packageName --cask
brew cleanup
brew zap packageName

This ensures that all traces of the application are removed from your system.

Advanced Uninstallation using brew bundle dump and brew bundle cleanup

For users with many packages who’d like a more controlled method of removing software, the brew bundle commands are very useful. First, dump all installed packages into a Brewfile:

brew bundle dump

Then, manually edit your Brewfile to retain only packages you wish to keep, and let Brew cleanup the rest:

brew bundle cleanup --force

Output:

Would uninstall formulae:

The --force flag carries out the action rather than just showing what would happen.

Ensuring a Clean Environment

To ensure there are no leftovers taking up space, make it a habit to run cleanup regularly:

brew cleanup

This will remove outdated versions of packages and clear cache that’s no longer required.

Diagnosing Issues with Uninstalling Packages

Sometimes you may run into issues when attempting to remove a package. The brew doctor command is helpful for diagnosing such problems:

brew doctor

Follow the instructions provided to get your Homebrew installation back to a healthy state.

Automating Package Management

Advanced users may want to look at scripting regular cleanup and maintenance tasks using a simple bash script or a more complex automation tool like Ansible, which can also manage Homebrew packages.

Simple Bash Script for Cleanup and Maintenance

This Bash script will update Homebrew, upgrade all packages, and then perform cleanup:

#!/bin/bash

# Update Homebrew
echo "Updating Homebrew..."
brew update

# Upgrade any installed packages
echo "Upgrading installed packages..."
brew upgrade

# Perform cleanup
echo "Cleaning up..."
brew cleanup

echo "Maintenance tasks complete."

How to use:

  1. Save this script as homebrew_maintenance.sh.
  2. Give it execute permissions with chmod +x homebrew_maintenance.sh.
  3. Run the script using ./homebrew_maintenance.sh.

Ansible Playbook for Homebrew Maintenance

Ansible can be used for more complex automation. Here’s a basic Ansible playbook that performs similar tasks:

---
- name: Homebrew Maintenance Playbook
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Update Homebrew
      community.general.homebrew:
        update_homebrew: yes

    - name: Upgrade all Homebrew packages
      community.general.homebrew:
        upgrade_all: yes

    - name: Clean up Homebrew
      community.general.homebrew:
        cleanup: yes

How to use:

  1. Save this as homebrew_maintenance.yml.
  2. Run the playbook with ansible-playbook homebrew_maintenance.yml.

Note: The Ansible playbook requires Ansible to be installed and the community.general collection, which includes the Homebrew module. You can install this collection with ansible-galaxy collection install community.general.

Conclusion

Mastering package uninstallation through Homebrew can keep your system clean and well-organized. Regular use of the cleanup command and understanding how to remove unwanted dependencies will ensure a tidy workspace with only the tools you need.