Fix Python Could not install packages due to an EnvironmentError: [Errno 13] Permission denied

Python shows “Could not install packages due to an EnvironmentError: [Errno 13] Permission denied” error when you try to install a package using pip, but don’t have sufficient permissions to write to the location where the package is being installed.

The easy way to fix this error is to add the --user flag to your install command. See this article for more info.

One common cause for this error is that you are trying to install a package to a system location, such as /usr/local/lib/python3.8/site-packages, and you don’t have permission to write to that location.

For example, the code below tries to install the requests package using pip:

# use pip or pip3 to install
pip install requests
Collecting requests
  Using cached requests-2.25.1-py2.py3-none-any.whl (61 kB)
# ...
Installing collected packages: requests

# ❌ ERROR
ERROR: Could not install packages due to an EnvironmentError: 
[Errno 13] Permission denied: '/usr/local/lib/python3.8/site-packages'

To fix this error, you need to ensure that you are installing the package to a location where you have the necessary permissions.

One way to do this is to use the --user flag when installing the package.

This flag tells pip to install the package to a user-specific location in your home directory, where you should have permission to write:

pip install <package> --user

The user site-packages directory is usually located at ~/Library/Python/3.8/lib/python/site-packages (might be different for your computer)

You should not see the permission denied error when installing to the user site.

In the most recent versions, Python automatically install packages to the user home directory when the system site-packages directory is not writable.

You’ll see a message as follows:

pip install <package>
Defaulting to user installation because normal site-packages 
is not writeable
# ...

This means Python recognizes that the system location is not writable with the current user, so it changes the install path.

Another way to fix this error is to use sudo to install the package as the root user.

This will give you the necessary permissions to write to system locations, including the system site-packages directory:

sudo pip install <package>

The command above would install the requests package to the system site-packages directory without the permission denied error.

But keep in mind that using sudo to install packages can have security implications, as you also need a root-level permission to run those packages later.

It’s recommended to install packages in directories accessible with user-level permissions instead of using sudo as a shortcut.

Conclusion

In conclusion, the Python error message “Could not install packages due to an EnvironmentError: [Errno 13] Permission denied” occurs when you don’t have sufficient permissions to write to the location where you are trying to install a package.

To fix this error, you can use the --user flag to install the package to a user-specific location, or use sudo to install the package as the root user.

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.