Can't restart PHP with homebrew on macOS
You are encountering a classic permission conflict between Homebrew's default installation path and the macOS Monterey security model. The error Permission denied @ rb_sysopen - /Users/USER/Library/LaunchAgents/homebrew.mxcl.php.plist is not a bug in your PHP installation; it is a symptom of how LaunchAgents handle file ownership when services are managed by Homebrew.
When you install a formula like php@8.1 using standard Homebrew instructions, the package manager places the necessary launch agent plist files in /Users/USER/Library/LaunchAgents. These files are owned by your user account but often have restrictive permissions because they contain configuration data that could theoretically be exploited if writable by others. When you attempt to run brew services restart php without elevated privileges, Homebrew tries to update or replace this file. If the file's permissions are read-only (often 444) or if the current process lacks write access for some reason, the system blocks the operation immediately.
The warning you see when using sudo, stating that "php must be run as non-root to start at user login," highlights a critical architectural constraint of macOS LaunchAgents. Unlike SystemLaunchDaemons, which are designed to run with specific permissions and often require root access, User LaunchAgents are strictly intended to operate within the context of a single user session. They rely on the user's environment variables, home directory paths, and group memberships. Running them as root breaks this isolation, leading to unpredictable behavior or immediate rejection by the system security daemon.
To resolve the permission error without resorting to running everything as root, you need to adjust the file permissions of the specific LaunchAgent plist. Since Homebrew installs these agents in your user library, you have ownership but may lack write permission due to the installation script's default behavior or a previous failed update attempt.
Execute the following command to grant read and write access specifically to the LaunchAgents directory for your user group:
chmod u+w /Users/USER/Library/LaunchAgents/homebrew.mxcl.php.plist
Replace USER with your actual username. After adjusting the permissions, retry the restart command:
brew services restart php@8.1
Note that you must specify the full formula name including the version (php@8.1) rather than just php, as Homebrew treats them as distinct entities in its service manager.
If the file ownership is incorrect or if the plist file itself was created with root ownership by a previous failed sudo attempt, simply changing permissions might not suffice. You may need to reset the launch agent entirely. First, stop and remove the existing service:
brew services stop php@8.1
brew services delete php@8.1
Then, reinstall the formula if necessary, or re-run the installation script which will overwrite the plist file with correct permissions. However, be cautious of chmod usage in general; while this is a valid troubleshooting step for Homebrew services, permanently modifying system files to allow broader write access can introduce security risks. The intent here is strictly to restore the expected state where your user owns and manages the configuration they installed.
For production environments or if you plan to manage multiple PHP versions, consider using brew services list to verify which agents are currently loaded. You might also find it useful to check the status of the service before attempting a restart:
brew services info php@8.1
This output will confirm whether the plist file is being read correctly and if there are any lingering state issues preventing the service from transitioning states. By keeping LaunchAgents within their intended user context and ensuring correct ownership, you avoid the need for sudo entirely, maintaining the security boundaries macOS expects for user-level applications.