Upgrade python packages with pip: use "sudo" or "--user"?
To me the only difference is the scheme path.
Using sudo your package will be installed in:
/usr/local/lib/python2.7/dist-packages/
With --user
they are installed in your $HOME
directory in:
$HOME/.local/lib/python2.7/site-packages
For your own system, I'd say it does not really matter. Use --user
if you're not a sudoer member.
For code you publish, avoid --user
.
Related videos on Youtube
Byte Commander
Ask Ubuntu moderator♦, IT student and DevOps engineer. I love Ubuntu, Python, good music and coffee, although not necessarily in that order. You can easily contact me in the Ask Ubuntu General Room most of the time, or on Discord as @ByteCommander#2800. I'd also love to invite you to my Ubuntu Hideout Discord Server btw. PS: My profile picture is derived from "Wolf Tribals" by user HaskDitex (DeviantArt.com), which is under creative Commons 3.0 License. Currently I'm using the character "Dregg Morriss" from the game "Medieval Cop" by Vasant Jahav ("Gemini Gamer"). It can be found here.
Updated on September 18, 2022Comments
-
Byte Commander over 1 year
I have a question about installing/upgrading Python packages using the tool
pip
.I can either run
sudo pip install -U PACKAGENAME
or
pip install -U PACKAGENAME --user
What is the difference between those two commands and which one should I prefer...
- on my own machine?
- in the install script of a python program I publish?
-
Admin almost 9 yearsHave a look at stackoverflow.com/questions/21055859/…
-
Admin over 7 yearsThere are a couple ways to call
pip
viasudo
: eithersudo -H pip install...
orsudo -H python -m pip install...
(note thatpip
likes to be called usingsudo -H
). Thepip
bundled withpython
is available as of Python 2.7.9 and later (on the python2 series), and Python 3.4 and later (for Python 3). -
Admin over 7 yearsIt'd be great (and related to this question) if someone could clarify if
sudo pip
does anything w/ system dependencies as well; e.g., does asudo pip
on Ubuntu actually callapt-get
to install (or remove!) anything? For instance, if I'm installingipython
viapip
(putting it into/usr/local
by default), but an older one was already installed viaapt-get
(/usr/bin
), does my system only work correctly if/usr/local/bin
is in my path ahead of/usr/bin
? These issues seem to be resolved at run-time. Or, doessudo pip
do anything special at install time? -
Admin over 7 years@michael_n Not sure, but I strongly believe that
pip
is completely unrelated toapt
. One does not use the other and vice versa. -
Admin over 7 years@ByteCommander but pip does seem to uninstall system packages (or so it reports, when installing things); others seem to agree, but this is not clearly documented (as far as I can tell): askubuntu.com/questions/333475/…
-
Byte Commander almost 9 yearsCould you explain why to avoid
--user
, please? I only read about avoidingsudo
yet... -
Sylvain Pineau almost 9 years@ByteCommander: For a program you publish I'd not make any assumptions of the target directory of the end user. It can be run with root or not. So I'd not mess my install script with commands that would install modules in several locations.
-
Alexey Ce over 7 yearsmy inclination is that this answer is wrong. With ruby, you want that restricted to a user and not to effect the system as a whole. The same reasoning applies here. However, I'm not sure, so just making a comment. Please correct.