1

I'm confused by the intended pip usage. Pip comes installed with Python, which is great, but I get the following warnings when new versions come out:

WARNING: You are using pip version 21.1.1; however, version 21.1.3 is available.
You should consider upgrading via the '/usr/local/opt/python@3.8/bin/python3.8 -m pip install --upgrade pip' command.

I follow the instructions to install it using the command they gave. But then it uninstalls my existing pip and is not able to install the new version.

Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.1.1
    Uninstalling pip-21.1.1:
ERROR: Could not install packages due to an OSError: Cannot move the non-empty directory '/usr/local/lib/python3.8/site-packages/pip-21.1.1.dist-info/': Lacking write permission to '/usr/local/lib/python3.8/site-packages/pip-21.1.1.dist-info/'.

The pip command is now unrecognized, and the official documentation for upgrading pip suggests running:

python -m pip install -U pip

which gives the same permission error.

I Google this error, and found that the community highly advises to not sudo from these questions (this and this). They also advised pip3 install --upgrade pip --user which also gave the same error. The common consensus is to only install pip packages inside virtual environments, but I'm hesitant to have pip completely uninstalled.

So I got pip to install using sudo, but it's unclear whether I've inadvertently affected (or will affect future) system-wide installations, or how I'd check for these.

I don't understand why installing pip inside /usr/local/ requires sudo, and whether I should only be using pip exclusively inside virtual environments and never outside it

humanlikely
  • 315
  • 3
  • 10
  • Because the `/usr/local` folder is owned by `root`. You aren't allowed to change it. As a rule, I tend to install most packages with `sudo`, just so they're available everywhere. – Tim Roberts Jul 15 '21 at 19:55
  • In general, /usr/local is a root owned area. It is better to keep permissions strict because you install a lot less frequently than you run. If you are using a system-wide python that all users can use, then it makes sense not to allow users to install whatever package they want. Any user can install packages / libraries locally with user. All that being said, it is fine to upgrade pip with sudo. – d_kennetz Jul 15 '21 at 19:56
  • What installed `pip` under `/usr/local/opt` in the first place? That's the only thing that should be upgrading `pip`. If you want a more up-to-date version without touching `/usr/local/opt`, create a virtual environment first. – chepner Jul 15 '21 at 20:01
  • `pip` is not part of the standard Python distribution; it's a 3rd-party tool that some particular distribution installed *along with* Python. – chepner Jul 15 '21 at 20:08
  • Did the Python you're using come with your distro? In particular is `pip` a package in your package manager? If so, upgrade `pip` using the package manager. (The upgrade might not be available right away through that channel, though. Alternatively, uninstall that and install `pip` using `python -m ensurepip` – kindall Jul 15 '21 at 21:26

2 Answers2

0

pip can be installed with sudo, into a folder that you don't have permissions to write to. However, it can install packages outside of that folder (and thus, into a folder you have write permissions). However, it is recommended that you don't install pip into a root folder, and instead install it into your home directory.

Cutewarriorlover
  • 158
  • 2
  • 11
-1

The command to install pip as root is

sudo apt-get install pip

It should then prompt you for your password. I recommend using sudo whenever you install something.