0

I am wondering what is the difference between these two commands (I have the feeling that they are identical):

  • sudo -H pip install <package>
  • pip --user install <package>

More informations:
From the sudo manpage:

-H, --set-home
    Request that the security policy set the HOME environment
    variable to the home directory specified by the target user's
    password database entry. Depending on the policy, this may be
    the default behavior.

And the pip user guide: https://pip.pypa.io/en/stable/user_guide/


Related questions:
What is the difference between pip install and sudo pip install?
What is the purpose of "pip install --user ..."? and
sudo pip install VS pip install --user
But none of them talk about the sudo -H option or the precise difference between the two.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
swiss_knight
  • 5,787
  • 8
  • 50
  • 92

2 Answers2

0

sudo is short for 'superuser do'. It simply runs the command with root privileges, which may be useful if you are installing to a directory you normally wouldn't have access to.

However, in the examples you have given the two commands would function identically, as you don't need root privileges to pip install --user

notjoshno
  • 337
  • 2
  • 4
  • 15
0

The difference comes down to the permissions that are given to the package, and the location where the package is installed. When you run a command as root, the package will be installed with root permissions.

Here's an example:

Running sudo -H pip3 install coloredlogs results in the following:

$ sudo pip3 show coloredlogs | grep Location
Location: /usr/local/lib/python3.8/dist-packages

$ ls -l /usr/local/lib/python3.8/dist-packages
drwxr-sr-x 4 root staff 4096 Feb 25 01:14 coloredlogs

$ which coloredlogs
/usr/local/bin/coloredlogs

Running pip3 install --user <package> results in the following:

$ pip3 show coloredlogs | grep Location
Location: /home/josh/.local/lib/python3.8/site-packages

$ ls -l /home/josh/.local/lib/python3.8/site-packages
drwxrwxr-x 4 josh josh 4096 Feb 25 01:14 coloredlogs

$ which coloredlogs
coloredlogs not found

Notice the location differences between the two, and also notice that the package isn't installed on PATH when installed using the --user flag. If for some reason I wanted to directly call the package, I would need to add /home/josh/.local/bin to my PATH.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50