I was also getting the same error when I was using brew to install something.
Then searched about the problem on internet and got to know that it is something to do with the ownership. In my mac there are two administrative accounts one which I am using to install is one of them even then I was facing the same problem so I decided to shift another administrative account to standard account but doing that doesn't solved this problem then I decided to use chown command to change my ownership but got chown: /usr/local: Operation not permitted
There a point that you know keep in mind to avoid getting this problem ie: You should be using Adminstrative account. If you are getting this problem with Administrative account please reinstall homeBrew using this command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
everything will work now. It worked for me My macos is BigSur