11

I have been trying to install a Python module which relies in setuptools, and apparently the module was installed using apt. However, it seems to be the wrong version, so I tried to install it using pip just to see if anything changes (spoiler: nothing changes - apparently).

What are the differences (if any) between the following?

pip3 install setuptools

and:

apt install python3-setuptools

1 Answer 1

16

I will appreciate if someone could explain the differences (if any) between:

Highest level: you never should use pip install to install to system (--system, or on Linux distros where --user isn't the default, omitting --user) when things might conflict with your system whereas apt install is pretty safe.

Explanation:

apt is the package installation tool of your Linux distro. A Linux distribution these days is mostly the effort to offer a way to install packages in a way that works with each other without breaking – for example, if you're trying to install a library that libreoffice uses, but in a version incompatible with your libreoffice, your linux distro tool will tell you that sadly, to fulfill your command, it will have to uninstall libreoffice, because it wouldn't work with that version you're requesting.

The fact that you very rarely see that happen is an indication of how well modern Linux distros are doing here: typically, most of software that you can install using apt works well together.

pip, on the other hand, has no notion of what other software you have on your machine, which you might need. You tell pip to install something in a version that breaks your ability to even boot your system – it will go ahead and do that.

pip is python-specific. It assumes that all there is on that machine that has something to do with Python is kind of "fair game" and can be dealt with arbitrarily. Frankly, that is almost never the case – for example, on Fedora (another Linux distro that you're not using), you can easily break the installation tool dnf (Fedora's apt, if you will) with pip.

So, why does pip still exist? Well, there are situation where it's OK for pip to assume every bit of python it sees is under its control: Python brings a mechanism called virtual environments. In these, no python modules are per se installed, and they don't conflict with other software on your machine – simply because other software isn't aware of the environment.

Using that is quite straightforward. You can set up such an environment using

python3 -m venv ~/bertsexperiment

That sets up a folder ~/bertsexperiment for Python stuff to be installed into. You can then, from anywhere you like, "activate" that environment (what that really does is just change a few environment variables) – but that only affects the current process and things started from it. Try it:

source ~/bertsexperiment/bin/activate

will set up this shell in a way that all future python tooling will work with that folder as "prefix".

For example, if you wanted to have an updated setuptools in that shell, you could, after sourceing the activaton script as shown above, run pip3 install --upgrade setuptools, and they would be installed into the virtualenv.

In short:

  • if in doubt, use apt, because it's your distro's job to keep your software stack working together
  • Never use pip unless you intend to install something into a folder only used for your current project and not by anything else on your system.
    Hence, the only realistic time you would want to use it is when you're using a Python virtualenv.
5
  • 4
    Using pip in this case is akin to make install: it just moves files around. The problem with both pip and make install is that it's not the installer that you're supposed to use for your root filesystem. That's what the package manager is for. So using any installer to install things into the root filesystem is a bad idea, pip being one of many examples. Of course, if your Python installation is not in /, but rather in some other location like /opt/foobar/python3.10, then using pip is perfectly fine. This is what a Python venv does, so using pip in a venv is safe. Feb 8 at 23:11
  • 1
    This answer is both factually incorrect and unduly alarmist. Using pip to install Python modules on your own account is fine. You don't have to compartmentalize everything. And on Debian and derivatives (so most distributions using apt), pip install defaults to user mode. So pip install foo will not mess the user's system: only pip install --system foo would do that. Feb 9 at 11:13
  • 1
    @Gilles'SO-stopbeingevil' thanks for reminding me I simplified a bit, too much maybe. Yes, --user installation is sensible, in many cases (not all, to be honest, and if you can get it via apt…). I wasn't aware that debian has patched pip to default to --user! Interesting! Re: Alarmism: I don't think so. I've done a lot of software support, and "how did you install this? Oh, you did a pip system install, and now the binary part of numpy that your C++ libraries linked against is not matching!" is a very common theme, in a few variations (throw in CUDA/TensorFlow/PyTorch, pyuhd, …). Feb 9 at 16:55
  • @Gilles'SO-stopbeingevil' until very recently pip install used --system by default, which would fail unless the user was running as root. Any reckless novices have broken things by running sudo pip install, because that makes the permission errors go away. It's absolutely not alarmist, it's just that Pip has recently taken steps to prevent users from doing this so easily. Feb 9 at 17:25
  • I would recommend these two recent articles on LWN which describe the somewhat unsettling state of Python packaging: <lwn.net/Articles/920132> and <lwn.net/Articles/920832>
    – kostix
    Feb 9 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.