I have seen some posts on Stack Overflow advising against running sudo pip install
[1]. However, if I have already activated a virtual environment, does that change anything?

- 9,872
- 7
- 57
- 95
3 Answers
If you have already activated a virtual environment, running sudo pip install is generally not recommended or necessary.
Using sudo pip install
can install the package globally, which may lead to conflicts or inconsistencies with the packages installed within the virtual environment.
So, when you're working within a virtual environment, you can simply use pip install
without sudo to install packages. This will ensure that the packages are installed only within the virtual environment and won't affect the system-wide Python installation.

- 23,227
- 3
- 22
- 38
If you are in virtualenv, there's no reason to install packages with admin permission, because the packages should've been installed into a local directory.
The only reason why a package might need to be installed with sudo while being in a virtualenv is if that package would install something outside of the virtualenv. You'll want to be really really really careful about that since the component that's been installed outside virtualenv would almost certainly stay around even after you've removed the virtualenv.
Also, depending on how your sudo had been setup, running sudo pip
might actually not install to your virtualenv instead it'll call the global system pip, because sudo
normally would reset $PATH
.
If you want to be sure that you're using the virtualenv pip, call sudo path/to/venv/bin/pip
instead. This is still problematic because your virtualenv directory would likely now be owned by a mixed user and you'll likely have problems installing/upgrading packages in that virtualenv in the future, but if you absolutely know what you're doing, and you know how to fix the permissions directory, that's how you'd do it.
TL;DR no, it's not ok to use sudo pip
with virtualenv

- 62,238
- 13
- 100
- 144
After messing around with my server some more and some more Googling I think I've figured out the problem with it:
The reason you still want to avoid running sudo pip install
even when you've activated a virtual environment is that if you run sudo pip install
all of the packages it installs will have an owner of root
, which will prevent non-root users from working with them.
This led to a problem in one case where I was a non-root user and was trying to run pip install
and it was failing, saying I didn't have permission to access a particular downloaded package file, because that file had been downloaded previously when I ran pip install
as sudo
.
You could run chown -R
to change the owner of the installed packages to a non-root user, but you're better off just installing the packages as a non-root user in the first place.

- 9,872
- 7
- 57
- 95