pip install failing with: OSError: [Errno 13] Permission denied on directory
Option a) Create a virtualenv, activate it and install:
virtualenv .venv
source .venv/bin/activate
pip install -r requirements.txt
Option b) Install in your homedir:
pip install --user -r requirements.txt
My recommendation use safe (a) option, so that requirements of this project do not interfere with other projects requirements.
Rather than using sudo
with pip install
, It's better to first try pip install --user
. If this fails then take a look at the top post here.
The reason you shouldn't use sudo
is as follows:
When you run pip with sudo
, you are running arbitrary Python code from the Internet as a root user, which is quite a big security risk. If someone puts up a malicious project on PyPI and you install it, you give an attacker root access to your machine.
You are trying to install a package on the system-wide path without having the permission to do so.
In general, you can usesudo
to temporarily obtain superuser permissions at your responsibility in order to install the package on the system-wide path:sudo pip install -r requirements.txt
Find more about
sudo
here.Actually, this is a bad idea and there's no good use case for it, see @wim's comment.
If you don't want to make system-wide changes, you can install the package on your per-user path using the
--user
flag.All it takes is:
pip install --user runloop requirements.txt
Finally, for even finer grained control, you can also use a virtualenv, which might be the superior solution for a development environment, especially if you are working on multiple projects and want to keep track of each one's dependencies.
After activating your virtualenv with
$ my-virtualenv/bin/activate
the following command will install the package inside the virtualenv (and not on the system-wide path):
pip install -r requirements.txt