Skip to content
Latchkey

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.

Homebrew
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

  1. Identify the prefix: /usr/local on Intel, /opt/homebrew on Apple silicon.
  2. Chown the affected directories back to the current user.
  3. Re-run the brew command without sudo.
Terminal
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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →