macOS runner: Homebrew "Permission denied @ apply2files" in CI
Homebrew tried to chmod or chown files in its prefix and the OS refused. Ownership of /usr/local (Intel runners) or /opt/homebrew (Apple silicon) drifted so the runner user no longer controls those files.
What this error means
A brew install or upgrade fails with "Error: Permission denied @ apply2files - /usr/local/Cellar/..." or a similar path, usually after a step ran brew under sudo.
Error: Permission denied @ apply2files - /usr/local/lib/node_modules/npm/...Common causes
Homebrew was run under sudo earlier
Running sudo brew ... makes root own new files in the prefix; the next non-sudo brew run cannot modify them, raising apply2files permission errors.
Prefix ownership drifted from the runner user
A step changed ownership of files under the Homebrew prefix, so the current user lacks write or chmod rights on them.
How to fix it
Restore prefix ownership to the runner user
- Identify the prefix:
/usr/localon Intel,/opt/homebrewon Apple silicon. - Chown the affected directories back to the current user.
- Re-run the brew command without sudo.
sudo chown -R "$(whoami)" "$(brew --prefix)"/*
brew install <formula>Never run brew with sudo
Homebrew is designed to run as the normal user. Dropping sudo from brew steps prevents root-owned files that later break apply2files.
How to prevent it
- Never prefix brew commands with sudo.
- Chown the prefix back to the runner user if ownership drifts.
- Keep brew operations in steps that run as the default runner user.