How to find a Homebrew package prefix

Homebrew's prefix is the directory tree used by the active brew command for packages, symlinks, completions, service files, and support data. Finding that path avoids mixing Apple Silicon, Intel, Linux, WSL, or custom Homebrew installs when another command needs an absolute package location.

Running brew --prefix without a package name prints the active Homebrew installation prefix. Adding a formula name prints that formula's opt prefix, usually under the active prefix's /opt directory, so scripts and config files can point at the current linked version instead of a versioned Cellar path.

Use the same brew binary that will run the later install, service, or config command. On Apple Silicon, a native Homebrew under /opt/homebrew can coexist with an Intel Homebrew under /usr/local, so shell architecture, startup files, and hard-coded paths can change which prefix is actually in use.

Steps to find a Homebrew package prefix:

  1. Print the active Homebrew install prefix.
    $ brew --prefix
    /opt/homebrew

    Default supported prefixes are /opt/homebrew on Apple Silicon macOS, /usr/local on Intel macOS, and /home/linuxbrew/.linuxbrew on Linux.

  2. Print the formula prefix for the package.
    $ brew --prefix jq
    /opt/homebrew/opt/jq

    The formula prefix points at Homebrew's opt prefix for the active version. Replace jq with the formula whose headers, libraries, binaries, or config path you need.

  3. Require an installed package when a script will consume the prefix.
    $ brew --prefix --installed jq
    /opt/homebrew/opt/jq

    --installed prints nothing and exits with a failing status if the formula is not installed, which prevents scripts from accepting a future install location as if it were present.

  4. Confirm that the formula prefix resolves to an installed path.
    $ ls -ld /opt/homebrew/opt/jq
    lrwxr-xr-x@ 1 user  admin  18 Jun 22 08:28 /opt/homebrew/opt/jq -> ../Cellar/jq/1.8.2

    The opt path is normally a symlink to the active versioned keg under the Cellar. Use the printed path from your own prefix rather than copying /opt/homebrew blindly.

  5. Check the file path that the next command will use.
    $ ls -ld /opt/homebrew/opt/jq/bin/jq
    -r-xr-xr-x@ 1 user  admin  71952 Jun 22 08:28 /opt/homebrew/opt/jq/bin/jq
  6. Run the package through the resolved prefix when the path points to a command-line tool.
    $ /opt/homebrew/opt/jq/bin/jq --version
    jq-1.8.2

    For library-only formulae, replace the smoke test with the header, library, pkg-config, or config path that required the prefix.