Managing Software Packages on macOS
9 min read·Jan 1, 2025
Homebrew is a free and open-source software package management system that simplifies the installation of command-line binaries and graphical applications on macOS.
Installing Homebrew
To install Homebrew on your machine, you can run the following command:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
This command will install the brew utility and its underlying dependencies, as well as two directories named homebrew-cask and homebrew-core, that contain all of the scripts required for installing packages.
Searching for packages
To search for available packages (formulae) and macOS applications (casks) in the Homebrew repositories, you can use the brew search command:
$ brew search [--formula|--cask] <pattern>|/<pattern>/
Where:
- The
--formulaflag is used to only search for CLI packages. - The
--caskflag is used to only search for GUI applications. - The
<pattern>|/<pattern>/argument is either a literal string or a regular expression.
Note: Homebrew will by default search for both CLI and GUI packages if no flags are present.
Example
This command will list all the CLI packages that contain the 'tree' expression:
$ brew search --formula tree
as-tree datree dutree otree pstree tree-sitter ✔ treefmt tre
cherrytree dep-tree erdtree pipdeptree tree ✔ treecc treefrog
Note: All the packages marked with a check mark (✔) are already installed in your system.
Getting information about a package
To get more information about a package, such as its description, version number, maintainer, etc, you can use the brew info command:
$ brew info [--formula|--cask] <package>
Example
This command will list the information related to the tree package:
$ brew info --formula tree
==> tree: stable 2.1.3 (bottled)
Display directories as trees (with optional color/HTML output)
https://oldmanprogrammer.net/source.php?dir=projects/tree
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/t/tree.rb
License: GPL-2.0-or-later
==> Analytics
install: 20,040 (30 days), 78,857 (90 days), 286,145 (365 days)
install-on-request: 19,626 (30 days), 76,910 (90 days), 279,139 (365 days)
build-error: 0 (30 days)
Installing packages
To install one or more packages on your system, you can use the brew install command:
$ brew install <package ...>
Note: It might happen for some reason that a package stops working. To reinstall it, you can run the
brew reinstallcommand.
Example
This command will install the tree package:
$ brew install tree
==> Downloading https://ghcr.io/v2/homebrew/core/tree/manifests/2.1.3
Already downloaded: /Users/rludosanu/Library/Caches/Homebrew/downloads/d172fe2c6917a9de6f84beb8e4e20e5d8fb608c5d09524f28840d5105afb7b02--tree-2.1.3.bottle_manifest.json
==> Fetching tree
==> Downloading https://ghcr.io/v2/homebrew/core/tree/blobs/sha256:3a7806b8309c92599eaeb275b11d872048dd19d572f461c1fe28005873fb9389
Already downloaded: /Users/rludosanu/Library/Caches/Homebrew/downloads/092a6765e5bba3614b75d18cc1380509b46a7530fc486abef688a3f1767e1585--tree--2.1.3.ventura.bottle.tar.gz
==> Pouring tree--2.1.3.ventura.bottle.tar.gz
🍺 /usr/local/Cellar/tree/2.1.3: 9 files, 165.1KB
==> Running `brew cleanup tree`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Listing installed packages
To get the list of formulae and casks installed on your system, you can use the brew list command:
$ brew list [--formula|--cask]
Updating and upgrading packages
To ensure that the brew binary is up-to-date and that the Homebrew repositories have the latest information about available formulae and casks, you can use the brew update command:
$ brew update
Note: Homebrew will automatically run this command when installing new packages.
Once the Homebrew binary and scripts updated, to upgrade one or more installed packages to a greater or latest version number, you can use the brew upgrade command:
$ brew upgrade [<package ...>]
Note: Running the
brew upgradecommand without arguments will upgrade all installed packages at once.
Pinning package version numbers
To prevent certain packages from being upgraded, you can lock their current version using the brew pin command:
$ brew pin <package ...>
On the other hand, you can unpin packages using the brew unpin command:
$ brew unpin <package ...>
Uninstalling and cleaning up packages
To uninstall packages you don't need anymore, you can use the brew uninstall command:
$ brew uninstall <package ...>
Note: Running this command on packages that were not previously installed with Homebrew will result in an error.
Removing unused dependencies
Packages often require one or more other packages referred to as dependencies to function properly.
When installing a package with Homebrew, its dependencies are automatically installed with it.
However, when removing a package, only the specified package is removed but not its dependencies, as they may also be used by other locally installed packages.
To free up disk space and remove the dependencies that are no longer required by any package, you can use the brew autoremove command:
$ brew autoremove
Removing residual files
To free up even more space and remove Homebrew's residual files, such as logs, locks, or links, you can use the brew cleanup command:
$ brew cleanup
Note: By default, this command will remove all downloads that are older than 120 days, which can be changed using the
HOMEBREW_CLEANUP_MAX_AGEenvironment variable.
Summary
Here's a summary of what you've learned in this lesson:
- The
brew searchcommand is used to search for available formulae and casks in the Homebrew repositories. - The
brew infocommand is used to get information about packages. - The
brew installcommand is used to install packages. - The
brew listcommand is used to list installed packages. - The
brew updatecommand is used to update the Homebrew binary and repositories. - The
brew upgradecommand is used to upgrade the version number of packages. - The
brew pincommand is used to lock the version number of packages. - The
brew unpincommand is used to unlock the version number of packages. - The
brew uninstallcommand is used to remove installed packages. - The
brew autoremovecommand is used to remove unused dependencies. - The
brew cleanupcommand is used to remove Homebrew's residual files.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery