How can I run brew as root?

18,633

Solution 1

Many of them suggested that I chown the /usr/local folder directly to my own user account with root, so I can run brew without any permission problems, but it is too dangerous.

chowning all of /usr/local/ is probably a bad idea because it's not entirely under Homebrew's control. But just /usr/local/var/homebrew/locks/mysql.formula.lock should be ok.

chown $USER /usr/local/var/homebrew/locks/mysql.formula.lock

And follow the advice of brew doctor.


tried to run: sudo chown root /usr/local/bin/brew But this did not work as well.

All this accomplished was to make the file /usr/local/bin/brew owned by root. It doesn't make it run as root. You should change it back to being owned by your user else you won't be able to upgrade brew.

To make it run as root you'd use setuid. Do not do this. It's inherently dangerous, and doubly so with your current understanding of permissions. Nothing personal, permissions take a while to get used to and appreciate.


Homebrew does not run as root to limit the damage possible by a malicious package or a simple mistake. By running as your user it can only damage what is accessible to your user. This walls off important things like system files. This is the purpose of the Unix permission system: to limit how much damage is possible. If you run brew as root all bets are off.

Furthermore, if /usr/local/var/homebrew/locks/mysql.formula.lock is owned by root that means MySQL ran as root. If you run MySQL as root and it is compromised your whole system is compromised. If you run MySQL as your user or a special limited mysql user, only your user files or mysql's own files can be compromised. Run it as proscribed by homebrew.

It's very much worth it to learn to run with as few permissions as possible.

Solution 2

Using brew as root is a bad idea due security reasons. Instead you can change the brew file ownership to the current logged in user. Type this single command:

sudo chown $USER /usr/local/bin/brew
Share:
18,633

Related videos on Youtube

Aarony Jamesys
Author by

Aarony Jamesys

Hi, my name is Aaron and I am a high school student.

Updated on June 04, 2022

Comments

  • Aarony Jamesys
    Aarony Jamesys almost 2 years

    I know this is a frequently asked question, but I am really struggling.

    I have recently tried to install MySQL with Homebrew on my Mac OS El-Capitan. However, I have encountered some problems with it and I want to reinstall it. Yet whenever I try doing that it gives me:

    Error: Permission denied @ rb_sysopen - /usr/local/var/homebrew/locks/mysql.formula.lock
    

    I tried to bypass this error by simply sorting the named files out manually in the root account, but there were way too many files.

    So I tried uninstalling it with root, but it gave me this error:

    Error: 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.
    

    I looked at many articles, including this link! Many of them suggested that I chown the /usr/local folder directly to my own user account with root, so I can run brew without any permission problems, but it is too dangerous.

    I tried to run:

    sudo chown root /usr/local/bin/brew
    

    But this did not work as well.

    Is there any ways I can bypass this error, and run brew as root?

    • Gromski
      Gromski over 5 years
      Start by running brew doctor and following its advice.
    • Aarony Jamesys
      Aarony Jamesys over 5 years
      I ran 'brew doctor' but there is only warnings of problems that I need to solve. I still wouldn't be able to run brew as root or bypass the permission errors.
    • Gromski
      Gromski over 5 years
      "warnings of problems that I need to solve" – well, yes, exactly. Did you solve them?
    • bfontaine
      bfontaine over 5 years
      The solution to your permission error with MySQL is not to run Homebrew as root, which is a bad idea as the warning tells you. You should fix all the brew doctor warnings and if it’s not sufficient ensure you own /usr/local and everything under it (e.g. like this).