Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to sudo when running pip/easy_install?

All of the python tuts I've been reading lately tell me to do things like this:

pip install tornado
pip install requests

And every time I do this I get hit with a permission denied warning. But everything usually works when I sudo.

Is sudo required? And if so, why do so many tutorial instructions fail to mention that?

In ruby we can install using Rbenv or RVM, both of which remove the need to use sudo. Is there any equivalent in python? Or is it implied that you should always sudo?

like image 963
stephenmurdoch Avatar asked Sep 03 '14 12:09

stephenmurdoch


People also ask

Do I need to use sudo with pip?

Never use sudo to install with pip. This is the same as running a virus as root. Either add your local folder to your PATH or use a virtualenv.

What is easy_install pip?

easy_install was released in 2004, as part of setuptools. It was notable at the time for installing packages from PyPI using requirement specifiers, and automatically installing dependencies. pip came later in 2008, as alternative to easy_install, although still largely built on top of setuptools components.

Should pip be run as root?

So when you invoke pip as root, it will more than likely overwrite Python modules that were installed via system packages. The result of running pip as root, would be a dirty mix of Python modules installed via yum package management, and pip installed Python modules.

What is the difference between sudo pip and pip?

Usually the system packages are installed without write privileges for normal users so you must use sudo to elevate the privilege so pip can install to system packages. You can install a local copy of packages, ideally using virtualenv , where you wouldn't need elevated privileges.


1 Answers

pip requires permission to write the libraries to whichever directory it is using. This problem occurs when you do not have permission as a user and so pip fails. Using sudo gets around this problem but is not ideal.

You should not ever run code using sudo as you don't know what is inside the library, if it contains malicious code you could cause serious damage to your computer.

You can fix this problem by ensuring that you have permissions to write to the directory that pip is set up to use.

A better alternative (as you've suggested in your comment) is to use a virtualenv, this will allow you to use pip without the need of sudo. Make sure you do not create this virtualenv using sudo though as then you won't have permissions to write to it as a normal user.

like image 55
Ffisegydd Avatar answered Oct 20 '22 00:10

Ffisegydd