If you are getting the following brew cleanup error “Permission denied @ apply2files”, the top solution is to reset permissions to /user/local. We are going to rewrite permission to the current user. In your terminal copy and paste below.
sudo chown -R $(whoami):admin /usr/local/* \ && sudo chmod -R g+rwx /usr/local/*
Another solution is to completely reinstall brew. User the code below to uninstall and reinstall.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)" ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
For Mac OS X Mojave and above