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 install --user <package>
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 from the pip install --help
:
--user Install to the Python user install directory for your platform.
Typically ~/.local/, or %APPDATA%\Python on Windows.
(See the Python documentation for site.USER_BASE for full details.)
And the pip online user guide: https://pip.pypa.io/en/stable/user_guide/
especially: https://pip.pypa.io/en/stable/user_guide/#user-installs
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.
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.