How to switch Homebrew formula versions

Homebrew exposes formula commands through symlinks in its installation prefix. Changing the active formula version changes which installed keg supplies a command such as node, which is useful when a project needs a supported older runtime for compatibility testing.

Supported versioned formulae use names such as node@24 and often install as keg-only packages, which means Homebrew keeps them in the Cellar without linking them into $(brew --prefix)/bin by default. Current Homebrew does not provide brew switch, so the working method is to unlink the current formula and link the target formula.

The link and unlink method fits formula versions Homebrew already provides and maintains. It does not freeze arbitrary patch releases or prevent future upgrades; use pinning or a language-specific version manager when long-term project isolation is the real requirement.

Steps to switch Homebrew formula versions:

  1. List the installed source and target formula versions.
    $ brew list --versions node node@24
    node 26.4.0
    node@24 24.18.0

    Replace node and node@24 with the formula pair being switched. If the target formula is absent, install it first.
    Related: How to install a Homebrew formula

  2. Check the Homebrew prefix that should contain the linked command.
    $ brew --prefix
    /opt/homebrew
  3. Confirm the command currently resolves from the Homebrew prefix.
    $ command -v node
    /opt/homebrew/bin/node

    If another directory appears first, fix the shell environment before changing Homebrew links.
    Related: How to configure Homebrew shell environment

  4. Preview the symlinks Homebrew would remove from the current formula.
    $ brew unlink --dry-run node
    Would remove:
    /opt/homebrew/bin/node
    /opt/homebrew/bin/npm
    /opt/homebrew/bin/npx
    ##### snipped #####

    --dry-run lists the affected links without changing the prefix.

  5. Unlink the current formula.
    $ brew unlink node
    Unlinking /opt/homebrew/Cellar/node/26.4.0... 8 symlinks removed.
  6. Link the target versioned formula into the Homebrew prefix.
    $ brew link --force node@24
    Linking /opt/homebrew/Cellar/node@24/24.18.0... 8 symlinks created.

    --force is needed for many versioned formulae because they are keg-only. Use --overwrite only after a dry run shows files that are safe to replace because it can delete existing files in the Homebrew prefix.

  7. Clear the shell command lookup cache.
    $ hash -r

    Opening a new terminal has the same effect when the current shell keeps using the old command path.

  8. Verify that the command path still points at Homebrew.
    $ command -v node
    /opt/homebrew/bin/node
  9. Verify that the command reports the target version.
    $ node --version
    v24.18.0

    To return to the previous formula, repeat the unlink and link steps with the source and target formula names reversed.