Fix: pip Install Permission Denied — User Installs and Virtualenv

Problem

You try to install a Python package and pip fails with a permission error:

ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '/usr/local/lib/python3.11/site-packages/requests'
Consider using the `--user` option or check the permissions.

Symptoms

  • pip install <package> fails with “Permission denied”
  • The error points to /usr/local/lib/ or /Library/Python/
  • Using sudo pip install works but feels wrong
  • The error started after a macOS or Python upgrade
  • pip install --user doesn’t help because the package is required globally

Root Cause

pip tries to install packages into system-wide directories owned by root. On macOS and Linux, /usr/local/lib/python3.x/site-packages/ is a protected path. Writing to it requires elevated privileges.

Why sudo pip is dangerous:

  • Mixes system-managed files with pip-managed files
  • Can break your OS Python installation (macOS relies on it for system tools)
  • Installs packages globally for all users — may conflict with other projects
  • Hard to audit and clean up

Solution

Option 1: Use --user install (simplest)

pip install --user requests

Packages install to ~/.local/lib/python3.x/site-packages/. Python automatically includes this in sys.path — no PATH modification needed for imports.

Note: Executables install to ~/.local/bin/. Add this to your PATH if you need them:

# Add to ~/.zshrc or ~/.bashrc
export PATH="$HOME/.local/bin:$PATH"
python -m venv venv
source venv/bin/activate
pip install requests   # No sudo, no permission errors

Each project gets its own isolated Python environment. This is the gold standard for Python development.

Option 3: If you must install globally (e.g., CLI tools)

Use pipx for isolated global tool installation:

# Install pipx
brew install pipx
pipx ensurepath

# Install tools in isolated environments
pipx install black
pipx install poetry

pipx installs each tool in its own venv and makes the CLI available globally.

Verification

# Verify --user install
pip show requests | grep Location
# Location: /Users/you/.local/lib/python3.11/site-packages

# Verify venv install
source venv/bin/activate
pip show requests | grep Location
# Location: /Users/you/project/venv/lib/python3.11/site-packages

# Verify import works
python -c "import requests; print('OK')"

Prevention

  • Never use sudo pip — if you need it, your environment is misconfigured
  • Use venv for every project — it’s the standard Python workflow
  • Use pipx for global tools — black, poetry, httpie, etc.
  • Check pip config list — ensure global.target isn’t pointing to a system directory
  • On fresh macOS: Python 3 ships with the system. Use Homebrew’s Python instead: brew install python

References


Advertisement