# How To Fix “Could not install packages due to an EnvironmentError: [Errno 13] Permission denied”

You may run into the error “Could not install packages due to an EnvironmentError: [Errno 13] Permission denied” when installing a Python package with pip. Read on to find out why the installation process fails and how to get it right.

## The Error “Could not install packages due to an EnvironmentError: [Errno 13] Permission denied”

Exceptions like ModuleNotFoundError require you to install the target module with pip. However, it may produce this error:

pip install twitter
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied

When pip installs a package, it fetches all the required files from the repository (Python Package Index) and puts them in one of the dedicated locations for Python packages in your file system. These paths, also known as site-packages directories, allow Python to find and import packages when you need them. The reason for the error above is that pip doesn’t have permission to write into the default installation path.

Usually, it occurs when you run pip to install a package into a system directory. It is owned by the superuser, such as root in Linux. As a normal user, you don’t have permission to write and delete files from it.

## Solution

### Use User Installation Directory

On operating systems like Linux, Python and many packages are typically part of the distribution. You can use the system’s package manager to manage them, including install, upgrade, and uninstall packages.

All those tasks require root access and work in a separate mechanism to pip. The package manager will put Python packages into system directories, such as /usr/lib/python3.10/site-packages. They will be available to all users in the system.

Normal users and pip aren’t supposed to interfere with these components. Doing so may create conflicts between the system’s package manager and pip. As an alternative, you can install packages into a directory of your own.

Python gives each user a user base, where you can install modules, data, C headers, and scripts. On Linux, it is ~/.local/lib/pythonX.Y ~/Library/Python/X.Y/lib/python on macOS, and %APPDATA%\Python\PythonXY on Windows (X.Y is the version of the Python installation).

You don’t need privileged access to write to this location. Your Python scripts and interpreter can discover packages installed to it because its path is always included in sys.path.

To install to your user directory, add the option –user to the pip command:

pip install --user twitter

You can verify the installation path of the package with the show sub-command:

pip show twitter
Version: 1.19.6
Author: Mike Verdone
Author-email: [email protected]
Location: /home/admin/.local/lib/python3.10/site-packages

### Use Virtual Environments

Virtual environments help you isolate a working environment to a certain extent. Each virtual environment has its own directory where you can install packages for it. You can use a different version of the same package compared to the system-wide installation.

First, set up a virtual environment (replace learnshareit with the name you want):

Linux and macOS

python -m venv learnshareit

Windows

py -m venv learnshareit

Python will create the “learnshareit” directory and copy essential management tools to it. Activate the virtual environment you just created:

learnshareit\Scripts\activate

You can now install packages with pip into this virtual environment:

pip install twitter

## Summary

Python gives you the error “Could not install packages due to an EnvironmentError: [Errno 13] Permission denied” when you don’t have permission to write into its system-wide installation path. Install to your user directory or use a virtual environment instead.

Maybe you are interested: