I've read that pip install foo allows the developers of foo to run arbitrary code on my machine. I don't mind, as long as I can use Linux user permissions to sandbox it. However, I worry that maybe pip (like apt) could be configured to run things as root.

I believe that pip was installed on my machine using apt install, but I'm not certain - it might have been part of the distro (I'm on Ubuntu).

So my question is: Is there any standard installation of pip in which package owners can run arbitrary code as anyone other than the user who invoked pip? Extra brownie points for providing a link to an authoritative source.

  • SUID binaries are normal in this kind of scenario in my book, but I think pip simply installs the package for the user and installing with sudo is not recommended AFAIK. Commented Nov 1, 2023 at 12:03

2 Answers 2


I've read that pip install foo allows the developers of foo to run arbitrary code on my machine

Well this is not specific to pip... pip is just a kind of installer able to handle dependancies and as such has no provision for priviledge escalation.

That being said, every package installed by pip is installed in 2 phases: first phase is executed at install time with the priviledges on the user running the pip command and is used to configure the package. It is generally minimal but could be arbitrary complex and is left to the discretion of the package maintainers. The second phase is then executed each and every time that you use the package from Python again with the priviledges of the user running the python command (explicitely or implicitely). Again, and by definition it will execute arbitrary code at the discretion of the authors of the software.

Long story short, using pip install means that you want to install something on your system. As soon as that thing is installed, it will use the code written by its authors. If you do not trust something you should never install it. And this rule is the same for any software, whatever the language or the installer.

  • I'm sure there's some way that a package installed by a limited user to user-packages in his own home directory can be executed by a more privileged user, but that ought to require heroic effort to accomplish, and there should be no risk of it happening accidentally. I suppose it's a risk if a more privileged user activates a python virtual environment prepared by a limited user.
    – Ben Voigt
    Commented Nov 1, 2023 at 15:47
  • @BenVoigt: You are right and it is not specific to pip, neither to Python. Every time a super user executes something that has been prepared by a more limited user, it involves a manual priviledge escalation. If an unpriviledged user prepares a shell containing rm -rf / (willingly or by accident) and a super user blindly executes it, the system has to be reinstalled from scratch... (read format C: in a batch file for Windows...) Commented Nov 1, 2023 at 17:35

The pip tool itself does not have built-in mechanisms for escalating privileges or executing commands with elevated permissions. Pip is primarily responsible for installing Python packages and does not inherently include features for managing system-level privileges or security.

Privilege escalation or the ability to execute commands with elevated permissions typically depends on the user's own permissions and system configuration.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .