What it is
Homebrew is a package manager for macOS and Linux that simplifies the installation of software, libraries, and command-line tools.
Installation
macOS
Open Terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Linux
Open Terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Follow the on-screen instructions, which will likely involve adding Homebrew to your PATH.
Windows (via WSL)
Install Windows Subsystem for Linux (WSL) first. Then, within your WSL distribution (e.g., Ubuntu), follow the Linux installation instructions.
Core Concepts
- Formulae: These are Ruby scripts that define how to build and install a particular piece of software. They specify dependencies, source code locations, and build instructions.
- Casks: These are used for installing macOS GUI applications that don’t have a command-line interface. They typically install
.appbundles. - Taps: These are Git repositories that contain formulae and casks. Homebrew’s default tap is
homebrew/core. You can add third-party taps for software not included in the default set. - Cellar: This is the directory where Homebrew installs packages (usually
/usr/local/Cellaron macOS or/home/linuxbrew/.linuxbrew/Cellaron Linux). Each package is installed in its own versioned subdirectory.
Commands / Usage
Finding and Installing Software
-
Search for a formula or cask:
brew search wgetSearches for formulae and casks related to "wget".
-
Install a formula:
brew install gitInstalls the "git" command-line tool.
-
Install a cask (GUI application):
brew install --cask google-chromeInstalls the Google Chrome application.
-
Install multiple packages:
brew install node python3Installs both "node" and "python3".
-
Install a specific version of a formula:
brew install node@16Installs version 16 of Node.js (if available as a versioned formula).
Managing Installed Software
-
List installed formulae:
brew listLists all installed command-line tools.
-
List installed casks:
brew list --caskLists all installed GUI applications.
-
Upgrade all outdated formulae:
brew upgradeUpgrades all installed command-line tools to their latest versions.
-
Upgrade a specific formula:
brew upgrade gitUpgrades only the "git" command-line tool.
-
Upgrade all outdated casks:
brew upgrade --caskUpgrades all installed GUI applications to their latest versions.
-
Upgrade a specific cask:
brew upgrade --cask firefoxUpgrades only the "firefox" application.
-
Uninstall a formula:
brew uninstall nodeRemoves the "node" command-line tool.
-
Uninstall a cask:
brew uninstall --cask visual-studio-codeRemoves the Visual Studio Code application.
-
Uninstall all unused dependencies (cleanup):
brew autoremoveRemoves formulae that were installed as dependencies but are no longer needed by any installed formulae.
-
Clean up old versions of installed formulae:
brew cleanupRemoves old versions of installed formulae, keeping only the latest.
-
Get information about a formula or cask:
brew info nodeDisplays detailed information about the "node" formula, including its version, dependencies, and installation path.
brew info --cask firefoxDisplays detailed information about the "firefox" cask.
-
Reinstall a formula:
brew reinstall gitUninstalls and then reinstalls the "git" formula.
Updating Homebrew Itself
-
Update Homebrew and formulae lists:
brew updateFetches the latest version of Homebrew and updates the formulae and casks from all tapped repositories.
-
Check for issues and suggest fixes:
brew doctorRuns a series of checks to diagnose potential problems with your Homebrew installation.
Managing Taps
-
List all installed taps:
brew tapLists the Git repositories (taps) that Homebrew is tracking.
-
Add a third-party tap:
brew tap jesseduffield/lazygitAdds the "lazygit" formula from the "jesseduffield/lazygit" tap.
-
Remove a tap:
brew untap jesseduffield/lazygitRemoves the "jesseduffield/lazygit" tap and its associated formulae.
Advanced Usage
-
Install from a local formula file:
brew install ./my-formula.rbInstalls a formula defined in a local Ruby file.
-
Install from a URL:
brew install https://example.com/path/to/my-formula.rbInstalls a formula directly from a URL.
-
Link a formula (if it wasn’t linked automatically):
brew link nodeMakes the executables of the "node" formula available in your PATH. Use
brew link --overwrite nodeif there’s a conflict. -
Unlink a formula:
brew unlink nodeRemoves the symlinks for the "node" formula from your PATH.
-
Get the installation path of a formula:
brew --prefix gitPrints the installation prefix for the "git" formula (e.g.,
/usr/local/Cellar/git/2.38.0). -
Get the installation path of a cask:
brew --caskpath visual-studio-codePrints the installation path for the "visual-studio-code" cask (e.g.,
/Applications/Visual Studio Code.app). -
Generate a formula for a given URL:
brew creategit@2.38.0 https://github.com/git/git/archive/v2.38.0.tar.gzAttempts to automatically generate a formula file for Git version 2.38.0.
Common Patterns
-
Update Homebrew, upgrade all packages, and clean up:
brew update && brew upgrade && brew cleanupA common maintenance command to keep everything up-to-date and tidy.
-
Install a tool and then check its version:
brew install jq && jq --versionInstalls the
jqJSON processor and immediately verifies the installation. -
Find where a command is installed:
brew --prefix nodeShows the directory where the
nodeexecutable is located within the Homebrew cellar. -
Install a development tool and its dependencies:
brew install cmake autoconf automake pkg-configInstalls common build tools often needed for compiling software from source.
-
Install a specific version of Python and create a virtual environment:
brew install python@3.9 python3.9 -m venv my_project_env source my_project_env/bin/activateInstalls Python 3.9 via Homebrew, then creates and activates a virtual environment.
Gotchas
- Permissions Issues: On macOS, if you installed Homebrew with
sudo(which is not recommended), you might encounter permission errors. The standard installation avoids this by using your user’s permissions. If you run into issues,brew doctoris your first stop. - PATH Configuration: Homebrew usually adds itself to your PATH automatically. If commands aren’t found after installation, check your shell configuration files (
.zshrc,.bash_profile, etc.) to ensure Homebrew’sbindirectory is included. The output ofbrew doctoroften points this out. - Cask vs. Formula: Remember that
brew installis for command-line tools (formulae) andbrew install --caskis for GUI applications. Mixing them up will lead to errors. brew updatevs.brew upgrade:brew updateonly updates Homebrew’s list of available formulae and casks.brew upgradeactually downloads and installs newer versions of your installed packages. You need to run both to get the latest versions.- Linking Issues: Sometimes, especially with multiple versions of a package installed, Homebrew might not automatically link the desired version to your PATH. You may need to use
brew link <formula>orbrew unlink <formula>to manage this. Usebrew link --overwrite <formula>with caution if you have conflicting files. - Rosetta 2 on Apple Silicon: When running Homebrew on Apple Silicon Macs, some older formulae might still be compiled for Intel architecture. Rosetta 2 handles this translation, but performance might be impacted. Native Apple Silicon (ARM64) versions are preferred when available.