Python
EnvironmentError
Errno 13
package installation
troubleshooting

Could not install packages due to an EnvironmentError Errno 13

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

EnvironmentError: [Errno 13] Permission denied during pip install almost always means pip is trying to write to a location your current user cannot modify. The fix is usually not “run everything as administrator.” The real fix is to install into the right environment, with the right ownership, using the right Python interpreter.

What Errno 13 Usually Means

When pip installs a package, it needs to create directories, copy files, and update metadata under the target site-packages path. If that target path belongs to the system Python or another user, the install fails.

You can confirm which Python and pip you are using with:

bash
python3 -m pip --version
python3 -c "import sys; print(sys.executable)"

Those commands tell you whether the installation is targeting a global interpreter, a virtual environment, or a different Python than you expected.

The Best Default Fix: Use a Virtual Environment

For application development, a virtual environment is the cleanest solution because it gives your project its own writable package directory.

bash
1python3 -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4python -m pip install requests

Inside the activated environment, pip writes to .venv instead of the system Python directories. That avoids most permission problems and keeps project dependencies isolated.

If the environment is active but pip still resolves to a global interpreter, prefer python -m pip instead of bare pip. That forces pip to run under the interpreter you selected.

Use --user for Per-User Installs

If you are not working inside a project environment and just need a package for your own account, --user is often enough.

bash
python3 -m pip install --user black

This installs into your user site-packages directory rather than a system-managed path. It is a reasonable option for standalone tools, although virtual environments are still the better choice for project-specific dependencies.

One caveat is that scripts installed under the user site path may require updating your PATH so the commands are discoverable.

Avoid sudo pip install in Normal Development

Using sudo can make the immediate error disappear, but it often creates a mess later. Packages may end up owned by root, upgrades become inconsistent, and the global interpreter can drift away from what your projects expect.

A safer rule is:

  1. use a virtual environment for project work
  2. use --user for personal tools
  3. use the system package manager only when you truly want a system-wide package

That separation reduces both permission issues and environment confusion.

Watch for File Ownership Problems

Sometimes the error appears even inside a virtual environment because part of the environment was created or modified by another user account. That can happen after mixing sudo, CI artifacts, or copied directories.

Check ownership of the target directory:

bash
ls -ld .venv
ls -ld .venv/lib

If the environment contains files owned by a different user, the practical fix is usually to remove and recreate the virtual environment rather than trying to patch every file permission by hand.

Windows and Managed Python Installs

On Windows, the same error can come from installing under a protected directory such as Program Files, or from a Python interpreter managed by the Microsoft Store, enterprise policy, or antivirus software. The same principles still apply:

  • confirm which interpreter is active
  • prefer a virtual environment
  • avoid global installs unless you actually need them

Using the Python launcher makes interpreter selection clearer:

powershell
py -m venv .venv
.\.venv\Scripts\Activate.ps1
py -m pip install pandas

Common Pitfalls

  • Running pip install against the system Python when you meant to use a project virtual environment.
  • Using bare pip and assuming it belongs to the same interpreter as python.
  • Fixing the symptom with sudo and then creating root-owned packages that break future installs.
  • Ignoring file ownership problems inside an existing environment after mixing users or tools.
  • Installing personal tools globally when --user or an isolated environment would be safer.

Summary

  • Errno 13 during pip install means the target location is not writable by the current user.
  • The most reliable fix is to install packages inside a virtual environment.
  • 'python -m pip is safer than bare pip when multiple interpreters are present.'
  • '--user is a practical fallback for per-user tools outside project environments.'
  • Avoid sudo pip install unless you deliberately want system-wide changes and understand the ownership consequences.

Course illustration
Course illustration

All Rights Reserved.