How to Fix macOS Homebrew Permission Denied
In this tutorial, you'll learn about How to Fix macOS Homebrew Permission Denied. We cover key concepts, practical examples, and best practices.
The Problem
You run a Homebrew command and get:
Error: Permission denied @ apply2files - /usr/local/lib/node_modules
Or:
/usr/local/bin is not writable. You should use the writable
location or change the ownership of /usr/local/bin.
Homebrew cannot write to its installation directories because they are owned by root or another user.
Quick Fix
Step 1: Check /usr/local ownership (Intel Macs)
ls -la /usr/local | head
If the owner is root, change it to your user:
sudo chown -R $(whoami):staff /usr/local
Step 2: Check /opt/homebrew ownership (Apple Silicon)
sudo chown -R $(whoami):staff /opt/homebrew
Step 3: Never run brew commands with sudo
# Wrong
sudo brew install node
# Right
brew install node
Running brew with sudo creates files owned by root, causing permission conflicts later.
Step 4: Repair Homebrew permissions
sudo chown -R $(whoami) $(brew --prefix)/*
This fixes permissions for all files under the Homebrew prefix.
Step 5: Fix specific directory permissions
# Fix Cellar (installed formulas)
sudo chown -R $(whoami) $(brew --cellar)
# Fix Caskroom (cask applications)
sudo chown -R $(whoami) $(brew --caskroom)
Step 6: Verify the fix
brew doctor
brew doctor checks for permission issues and other problems. Address any warnings it reports.
Step 7: Reinstall Homebrew if permissions are irreparable
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 8: Check for System Integrity Protection interference
csrutil status
If SIP is enabled (default), /usr/local is writable but /System, /bin, and /sbin are not. Homebrew does not need those paths.
Prevention
- Never use
sudowithbrew installorbrew update. - Run
brew doctorweekly to catch permission drift. - After macOS updates, verify Homebrew directory ownership.
Common Mistakes with homebrew permission
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world MACOS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
DodaTech Tool Reference
Durga Antivirus Pro's Permission Reporter audits Homebrew directories and alerts when system-owned files are detected in the brew prefix.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro