Homebrew can't be installed, prompting that it can't be used in the root directory.
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.
The solution is to add directory permissions to the current user:
sudo chown -R $(whoami) /usr/local
Then install the software you need by: brew install softname;
However, if you still encounter the situation that cannot be installed, give the current user the corresponding directory permissions again according to the prompt content (the system will prompt here), you just need to operate according to the prompt.
After I execute the above command, I want to install the software:
Another error was prompted:
Error: /usr/local/Cellar is not writable. You should change the
ownership and permissions of /usr/local/Cellar back to your
user account:
sudo chown -R $(whoami) /usr/local/Cellar
Error: Cannot write to /usr/local/Cellar
I execute the command again as prompted:
sudo chown -R $(whoami) /usr/local/Cellar
Then, installed the software I needed again and the problem was solved.
If there is an error message later, follow the prompt again. until the problem is resolved.