Upgrade python packages with pip: use "sudo" or "--user"?

11,523

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.

Share:
11,523

Related videos on Youtube

Byte Commander
Author by

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, 2022

Comments

  • Byte Commander
    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...

    1. on my own machine?
    2. in the install script of a python program I publish?
    • Admin
      Admin almost 9 years
    • Admin
      Admin over 7 years
      There are a couple ways to call pip via sudo: either sudo -H pip install... or sudo -H python -m pip install... (note that pip likes to be called using sudo -H). The pip bundled with python is available as of Python 2.7.9 and later (on the python2 series), and Python 3.4 and later (for Python 3).
    • Admin
      Admin over 7 years
      It'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 a sudo pip on Ubuntu actually call apt-get to install (or remove!) anything? For instance, if I'm installing ipython via pip (putting it into /usr/local by default), but an older one was already installed via apt-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, does sudo pip do anything special at install time?
    • Admin
      Admin over 7 years
      @michael_n Not sure, but I strongly believe that pip is completely unrelated to apt. One does not use the other and vice versa.
    • Admin
      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
    Byte Commander almost 9 years
    Could you explain why to avoid --user, please? I only read about avoiding sudo yet...
  • Sylvain Pineau
    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
    Alexey Ce over 7 years
    my 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.