Skip to content

Instantly share code, notes, and snippets.

@irazasyed
Last active March 23, 2024 08:34
Show Gist options
  • Save irazasyed/7732946 to your computer and use it in GitHub Desktop.
Save irazasyed/7732946 to your computer and use it in GitHub Desktop.
Homebrew: Permissions Denied Issue Fix (OS X / macOS)

Homebrew Permissions Denied Issues Solution

sudo chown -R $(whoami) $(brew --prefix)/*

@ipcm27
Copy link

ipcm27 commented Oct 18, 2021

"sudo chown -R $(whoami) $(brew --prefix)/*"
Worked just fine for me. But can Anyone explain why there's people saying that this is a terrible solution and that they are going to Macports?

@fabswt
Copy link

fabswt commented Oct 27, 2021

Trying to run a brew cleanup, I kept getting errors like this one, exclusively with .DS_Store files:

Error: Permission denied @ apply2files - /usr/local/lib/prey/versions/1.9.12/node_modules/sqlite3/lib/.DS_Store

Ended up using simply:

cd /usr/local/lib/
sudo find . -name ".DS_Store" -delete

And the next brew cleanup ran fine.

@snowquirk
Copy link

Thanks!!! @mihkell
It works for MacOS BigSur 11.4

@gord288
Copy link

gord288 commented Nov 12, 2021

Well I ran this command:

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

as well as:

chmod u+w /usr/local

And now I regret it. It's a security faux pas for the reasons others have said.

How do I change the ownership and permissions of /usr/local (and all subfolders and their contents) back to the factory defaults?

I'm running High Sierra.

Many thanks.

@rolf-langer
Copy link

Here's what I did to force Homebrew to run as root:
cd /usr/local/Homebrew/Library/Homebrew
Edit brew.sh, look for "Running Homebrew as root is extremely dangerous and no longer supported" (line 250 in my version), delete the check.

Did exactly that by temporary just returning in function check-run-command-as-root() in /usr/local/Homebrew/Library/Homebrew/brew.sh. Worked (some symlink related problems got fixed) and changed back. No more problems since then.

@tiagomatosweb
Copy link

Worked on Monterey.

Was getting

Error: Could not cleanup old kegs! Fix your permissions on:
  /usr/local/Cellar/php@7.4/7.4.16
  /usr/local/Cellar/php@7.4/7.4.24

@kareemelkasaby1
Copy link

wooooow <3

@alexweissman
Copy link

I'm really expected to recursively change ownership of my entire usr/local directory just because Homebrew told me to? A directory shared by all sorts of other applications and services? Ridiculous

@rtfmoz2
Copy link

rtfmoz2 commented Apr 4, 2022

Homebrew needs to work with the systems it is installing itself on. If there are permission issues then it needs to suggest sudo commands to the user that cover its specific requirements. Better yet provide a script for users to run under sudo to pre-create the dirs set permissions prior to install.

The closest I could come up with is the following. This make use of the users default group setting so other users can install homebrew utilities as well. Replace $GID if you want to use a different group, admin is a common one.

for name in Cellar Cask Homebrew Frameworks; do [ ! -d $name ] && sudo install -d -g $GID -m 0775 /usr/local/$name; done

@omego
Copy link

omego commented Apr 12, 2022

Thank you!

@erma4ina
Copy link

Was getting

erma4inapower@MacBook-Pro-Dmitrij  ~  brew services start php
Error: Permission denied @ rb_sysopen - /Users/erma4inapower/Library/LaunchAgents/homebrew.mxcl.php.plist

Ended up:

sudo chown -R $(whoami) /Users/erma4inapower/Library/LaunchAgents/

@mattbieber-lark
Copy link

🙏🙏🙏

@JuCarv-bit
Copy link

thanks! really helpful

@tiganabryan
Copy link

life saver!

@puchkii
Copy link

puchkii commented Jun 9, 2022

tysm🙏🏽

@xyliusun
Copy link

thank you!

@lutabordaMeli
Copy link

thank you !!! what exactly did I just do ??? 🤔🤔🤔 you saved my lifeee

@dulfrey
Copy link

dulfrey commented Jun 29, 2022

thank you 😁

@Sameto
Copy link

Sameto commented Jul 26, 2022

This just save my bacon... Thanks a lot...

@isobe-h
Copy link

isobe-h commented Aug 1, 2022

thank you!

@gustavolsilvano
Copy link

It worked! Thank you!

@EagleFace5000
Copy link

Youre a genius my friend, thank you!

@carlosmmairena
Copy link

Thank you!

@yezhengli-Mr9
Copy link

my solution with link: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

and command: sudo chown -R "$(whoami)":admin /usr/local

it ":admin" for sum in user

MacOS BigSur 11.4
Thanks~

@philipbankier
Copy link

Thank you ser!

@nnq1
Copy link

nnq1 commented Nov 10, 2022

great, you save my life

@ThorstenClaus
Copy link

In my case I had to setup a new local user on my machine and to remove the old one.
Then the folder ( defined by brew --prefix ) looses its owner.
With this the owner is set to the new local user and - it works!

@hyprcodr
Copy link

In my case, had this problem as I created a new user for development and installed brew in my original Mac OS Monterey user account. The mentioned solution solved my problem. Thanks !

@Emiya0306
Copy link

Nice~ Greate job!

@medwant
Copy link

medwant commented Jun 13, 2023

Thanks! Worked for me (macOS Ventura 13.3.1)

@emivlp
Copy link

emivlp commented Oct 9, 2023

Hi ! how can i fix this
/Users/emvlp/.zprofile:24: permission denied: /opt/homebrew/bin/brew
Could someone tell me the steps?
Thank you so much !

@Tony-MK
Copy link

Tony-MK commented Oct 28, 2023

Thanks @erma4ina

@ignoracenfly
Copy link

ignoracenfly commented Oct 29, 2023 via email

@MrMacvos
Copy link

MrMacvos commented Nov 1, 2023

and command: sudo chown -R "$(whoami)":admin /usr/local

it ":admin" for sum in user

Thanks, this worked for me!

(macOS Ventura 13.9, Intel Mac)

@AndreasMueck
Copy link

Worked for me on Intel Mac with macOS Ventura 13.5. Thank you :)

@ignoracenfly
Copy link

ignoracenfly commented Nov 16, 2023 via email

@daveqs
Copy link

daveqs commented Dec 7, 2023

Worked for me on Monterey 12.6.5! (Late 2016, Intel silicon)

@ignoracenfly
Copy link

ignoracenfly commented Dec 7, 2023 via email

@alstjd0051
Copy link

thank you

@ignoracenfly
Copy link

ignoracenfly commented Dec 24, 2023 via email

@VGerris
Copy link

VGerris commented Dec 28, 2023

Although this works, this is BAD solution.
The brew prefix $(brew --prefix) is /usr/local and that directory should be accessible for all users on the system.
With only one user, setting that to the current user is no problem, but as soons as a second user is added, this does not work and breaks all the permissions for it.
The proper way to do it is to have the installer use a group and make any user that needs access a member of it, so it works for multiple users.

The group on most folders is admin so adding a user to the admin group and making sure permissions between group and user are the same at least for directories brew uses should fix this.

The command to add user john to group admin is
sudo dseditgroup -o edit -a john -t user admin

@perth-seo-agency
Copy link

None of the above worked for me. Keep getting permissions errors when updating homebrew.

Example Permission Denied Error:

/Users/{insert username}/Library/Caches/Homebrew/api/formula_names.before.txt: Permission denied
The solution that worked was just deleting the cache files:

sudo rm -rf ~/Library/Caches/Homebrew/

Additionally, ran the following for good measure:

sudo chown -R {your username}:admin ~/Library/Caches/Homebrew/
sudo chmod -R 755 ~/Library/Caches/Homebrew/

Then reinstall Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

@gao1143632516
Copy link

be of use

@ignoracenfly
Copy link

ignoracenfly commented Feb 29, 2024 via email

@sirkev
Copy link

sirkev commented Mar 22, 2024

thank you, the first solution worked for me on my headless macOS Catalina (version 10.15) ci server

@sirkev
Copy link

sirkev commented Mar 22, 2024

thank you !!! what exactly did I just do ??? 🤔🤔🤔 you saved my lifeee

the command changes the ownership of all files and directories within the Homebrew installation directory to the current user, effectively giving the current user full control over these files and directories.

i'm just coming from linux, it didn't hit me that could be the problem. like mention above this will break when you add another user. the solution by @VGerris would be much better if potentially you'd want to have other users in the system

@ignoracenfly
Copy link

ignoracenfly commented Mar 22, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment