OSPython Error

pip permission denied

ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied

Traceback

terminal
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    # ERROR: [Errno 13] Permission denied: '/usr/lib/python3/dist-packages'
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied

What causes this error

pip does not have permission to write to the target installation directory (usually the system Python's site-packages). The installation requires elevated privileges.

How to fix it

Use a virtual environment (recommended). Use `pip install --user` for user-local installation. Never use `sudo pip install` on system Python. On Linux, install system packages via apt/dnf instead.

Code that causes this error

Broken
pip install requests
# ERROR: [Errno 13] Permission denied: '/usr/lib/python3/dist-packages'

Fixed code

Fixed
# Best: use a virtual environment
python3 -m venv myenv
source myenv/bin/activate
pip install requests

# Alternative: user installation
pip install --user requests

About pip permission denied

This error occurs when pip does not have write permissions to the target installation directory. On Linux and macOS, the system Python's site-packages directory is typically owned by root, so installing packages there requires `sudo`. However, using `sudo pip install` is strongly discouraged because it can break system tools that depend on specific package versions, create security risks, and cause conflicts between system and user packages.

The modern best practice is to always use virtual environments: `python -m venv myenv` creates an isolated environment where pip has full write permissions. If you must install outside a virtual environment, `pip install --user` installs to the user's home directory without requiring elevated permissions. On newer systems (Debian/Ubuntu 23.04+), pip refuses to install to the system environment entirely (see externally-managed-environment).

Common scenarios

1

Running scripts without sufficient file system permissions

2

Connecting to servers that are not running or are unreachable

3

Installing Python packages without proper environment setup

4

Running out of disk space or file descriptors during I/O operations

Related errors