When running pip install without a virtual environment, you may encounter a Permission denied error trying to write to /usr/lib/python3/dist-packages. This happens when pip tries to install to the system-wide Python directory without proper permissions.
This error occurs when pip attempts to install a Python package to the system-wide site-packages directory (typically /usr/lib/python3.x/dist-packages or /usr/lib/python3.x/site-packages), but the current user does not have write permissions to that location. The system-wide Python installation is protected to prevent accidental corruption of your operating system's Python environment, which many system tools depend on. The [Errno 13] is the POSIX permission denied error code. Pip attempts to write package files to the site-packages directory during installation, and when it lacks the necessary permissions, the installation fails.
The recommended solution is to use a virtual environment, which creates an isolated Python installation in your home directory where you have full permissions:
# Create a virtual environment
python3 -m venv myenv
# Activate it on Linux/macOS
source myenv/bin/activate
# On Windows
myenv\Scripts\activate
# Now pip will install to your local environment
pip install your-package-nameThis approach is preferred because it:
- Isolates project dependencies
- Avoids system-wide pollution
- Works without elevated permissions
- Allows different projects to use different package versions
If you don't want to use a virtual environment, you can install packages to your user home directory:
pip install --user package-nameThis installs packages to ~/.local/lib/pythonX.Y/site-packages instead of the system directory. You have full write permissions to your home directory. To verify this worked:
pip list --userIdentify which Python and pip you're using to understand the issue better:
# Show which pip executable is being used
which pip
which pip3
# Show the Python version and installation location
python3 --version
python3 -c "import sys; print(sys.prefix)"
# List where pip would install packages
pip show -f pipIf the output shows /usr/lib or /usr paths, you're using the system Python. Consider using a virtual environment or Python version manager instead.
For a more professional setup, use pyenv to install Python in your home directory:
# Install pyenv (on macOS with homebrew)
brew install pyenv
# On Linux, follow: https://github.com/pyenv/pyenv#installation
# Install a specific Python version
pyenv install 3.11.0
# Set it as your local or global Python
pyenv local 3.11.0
pyenv global 3.11.0
# Now pip will use your user-owned Python
pip install package-nameThis approach gives you:
- Complete control over Python versions
- Per-project Python version selection
- No permission issues
- Easy switching between versions
If you have system access and need to allow your user to install packages system-wide (not recommended for security reasons), you can check and fix permissions:
# Check current permissions on site-packages
ls -ld /usr/lib/python3/dist-packages
# See which group owns the directory
stat /usr/lib/python3/dist-packages
# If your user should have access, you might add them to a group
# This is a system administration task and varies by distribution
sudo usermod -a -G python-group $USERIMPORTANT: Modifying system Python permissions can break system tools that depend on it. Only do this if you fully understand the implications.
While it might seem like using sudo pip install will solve the problem, this creates serious issues:
# DON'T do this:
sudo pip install package-name # ❌ BAD
# WHY it's bad:
# - Installs packages as root, creating permission mismatches
# - Can break system tools that depend on system Python
# - Can corrupt your Python installation
# - Mixes your user code with system codeIf you're tempted to use sudo, it's a sign you should use a virtual environment or --user flag instead. The only legitimate case for sudo with pip is system administration on a clean system Python installation.
Modern Python best practices strongly discourage system-wide package installation. PEP 668 (https://peps.python.org/pep-0668/) formalizes the principle that system-provided Python should not be modified with pip, to prevent conflicts with the system package manager. Some Linux distributions (Debian, Ubuntu, Fedora) now enforce this with a EXTERNALLY-MANAGED environment marker, forcing users to use virtual environments or the --user flag. When working with multiple Python versions or complex dependency trees, consider using tools like Poetry or pdm which manage virtual environments automatically and lock dependencies for reproducibility. For CI/CD pipelines, always use virtual environments in your deployment scripts to ensure consistency across environments.
ValueError: math domain error
How to fix "ValueError: math domain error" in Python
ERROR: Could not install packages due to an OSError: [Errno 30] Read-only file system: '/usr/lib/python3/dist-packages'
How to fix "Read-only file system" error when installing packages with pip
socket.gaierror: [Errno -2] Name or service not known
How to fix "socket.gaierror: [Errno -2] Name or service not kn" in Python
pydantic_core._pydantic_core.ValidationError: 1 validation error
How to fix "pydantic_core._pydantic_core.ValidationError: 1 va" in Python
pydantic.errors.PydanticUserError: `@validator` is deprecated, use `@field_validator`
How to fix "pydantic.errors.PydanticUserError: `@validator` is" in Python