How to Fix Permissions on Home-brew on MacOS High Sierra

When I tried to install python onto homebrew it downloaded it and then an error message popped up at the end that stopped it from completing. When I try to do it again it asks me to do:

$ brew link python

After entering that the same error message appears:

permission denied @ dir_s_mkdir - /usr/local/lib

I have tried to do:

$ sudo chown -R $(whoami) /usr/local

And I get an error message that reads:

chown: /usr/local: Operation not permitted


sudo mkdir /usr/local/Frameworks
sudo chown $(whoami):admin /usr/local/Frameworks    
brew link python3

sudo mkdir /usr/local/Frameworks

sudo chown $USER /usr/local/Frameworks

And then try re-installing python. This worked absolutely fine for me.