If you happen to have multiple accounts on your mac, chances are, your current account belongs to different user group as the primary account that originally owned /usr/local
meaning that none of the solutions above will work.
You can check that by trying to ls -la /usr/local
and see what user and group that have permissions to write on the directory.
In my case it was root wheel
. It may be root admin
.
I solved it by adding the current user to the group that primary account has by using the following command.
sudo dseditgroup -o edit -a $(whoami) -t user admin
sudo dseditgroup -o edit -a $(whoami) -t user wheel
There after it worked like a charm. Hopefully it helps someone out there.