Nono.MA

[Solved] Homebrew Error: Could not cleanup old kegs! Fix your permissions on:

AUGUST 31, 2022

I run the following command to clean up unneeded old Homebrew kegs on macOS.

brew cleanup --prune=all

But often get this error.

Error: Could not cleanup old kegs! Fix your permissions on:

Here's the tail of what the command returns when there are permissions issues.

==> This operation has freed approximately 395MB of disk space.
Error: Could not cleanup old kegs! Fix your permissions on:
  /opt/homebrew/Cellar/dnsmasq/2.85
  /opt/homebrew/Cellar/php/8.1.1
  /opt/homebrew/Cellar/php/8.1.1.reinstall
  /opt/homebrew/Cellar/php/8.1.3_1
  /opt/homebrew/Cellar/php/8.1.4.reinstall
  /opt/homebrew/Cellar/php@7.4/7.4.21_1
  /opt/homebrew/Cellar/php@7.4/7.4.27.reinstall
  /opt/homebrew/Cellar/php@7.4/7.4.28_1.reinstall
  /opt/homebrew/Cellar/php@8.0/8.0.14

The fix

brew won't use root permissions for any operations.

This is what you'll see if you try to cleanup with root.

sudo brew cleanup --prune=all                                                   
# Error: Running Homebrew as root is extremely dangerous and no longer supported.
# As Homebrew does not drop privileges on installation you would be giving all
# build scripts full access to your system.

What you have to do is to change the ownership of the problematic directories to your username instead of root.

You can do that with sudo chown -R "$USER":admin /conflicting/dir.

To solve the issues shown in my error above, I ran the following commands.

sudo chown -R "$USER":admin /opt/homebrew/Cellar/dnsmasq/2.85
sudo chown -R "$USER":admin /opt/homebrew/Cellar/php/8.1.1
sudo chown -R "$USER":admin /opt/homebrew/Cellar/php/8.1.1.reinstall
sudo chown -R "$USER":admin /opt/homebrew/Cellar/php/8.1.3_1
sudo chown -R "$USER":admin /opt/homebrew/Cellar/php/8.1.4.reinstall
sudo chown -R "$USER":admin /opt/homebrew/Cellar/php@7.4/7.4.21_1
sudo chown -R "$USER":admin /opt/homebrew/Cellar/php@7.4/7.4.27.reinstall
sudo chown -R "$USER":admin /opt/homebrew/Cellar/php@7.4/7.4.28_1.reinstall
sudo chown -R "$USER":admin /opt/homebrew/Cellar/php@8.0/8.0.14

Then run brew cleanup --prune=all again and everything should work.

BlogCodeBrewMacos