Skip to content

Python pip: How to Resolve "Permission denied' and 'Consider using the --user option"

How to fix pip install permission errors using virtual environments, the --user option, and other safe approaches, plus why using sudo is usually not recommended.

When installing a package using pip, you may encounter a permission error, often accompanied by the suggestion to Consider using the --user option:

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

This error occurs when pip tries to install a package into a system-wide directory that your current user account does not have permission to modify. This is a common issue on Linux and macOS when using the system's default Python installation.

While the error message helpfully suggests using the --user option, the modern and most recommended solution is to use a virtual environment. This guide will cover the best practice first, followed by the other available solutions.

Understanding the Error: System vs. User Site-Packages

Operating systems protect their core directories to maintain stability. The default Python interpreter is often part of the system, and its site-packages directory (where packages are installed) is likewise protected. When you run pip install <package>, it attempts to install into this global, protected location. The Permission denied error is the OS stopping pip because your user account lacks the necessary administrative privileges.

Reproducing the Permission denied Error

Example of command causing the error:

pip install requests

Output:

Collecting requests
...
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/lib/python3.8/site-packages/requests'
Consider using the `--user` option or check the permissions.

Solution 1: Use a Virtual Environment (Best Practice)

The most robust and recommended way to manage Python packages is with a virtual environment. A virtual environment is an isolated project directory containing its own Python interpreter and site-packages folder, which your user account owns. This completely avoids permission errors and prevents package conflicts between projects.

Solution:

python3 -m venv venv

source venv/bin/activate


pip install requests

Output:

Collecting requests
...
Successfully installed requests

Solution 2: Use the --user Flag (Good Alternative)

If you do not wish to use a virtual environment, you can follow the error message's suggestion and use the --user flag. This tells pip to install the package in your user's home directory (e.g., ~/.local/lib/python3.8/site-packages), which you have permission to write to.

Solution:

pip install requests --user

This is a simple and effective fix for the permission error if you want the package to be available globally for your user account.

You can force a system-wide installation by running pip with sudo (on Linux/macOS) or by running your terminal as an Administrator (on Windows). This executes the command with administrative privileges.

The Command:

sudo pip install requests

Conclusion

Your Goal Recommended Solution
Isolate project dependencies (Best Practice). Use a virtual environment (python3 -m venv venv).
Install a package globally for your user only. Use the --user flag (pip install <package> --user).
Force a system-wide installation (Use with caution). Use sudo on Linux/macOS or Run as Administrator on Windows.

The Permission denied error is a common hurdle, but it's also an opportunity to adopt best practices. Using virtual environments is the standard, professional way to manage Python projects, as it solves this permission issue while also preventing many other potential dependency problems.

Navigation