19

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

2 Answers 2

22

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
  • 5
    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. Commented Feb 8, 2023 at 23:11
  • 3
    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. Commented Feb 9, 2023 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, …). Commented Feb 9, 2023 at 16:55
  • 2
    @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. Commented Feb 9, 2023 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
    Commented Feb 9, 2023 at 17:41
0

It really depends on your end goal.

  • Something in another Debian package depends on this package? Definitely use apt.
  • Or conversely, you want to create a package or a set of packages for Debian or a Debian-based distro like Ubuntu, Mint, etc - again, definitely stay in apt land.
  • You want to install something which requires a newer version than you can find on Debian - you can hunt for backported .deb packages from https://backports.debian.org/ or random PPAs, but perhaps at this point it's easier and more straightforward to move to pip. (Though sometimes the packaging work adds significant value but requires nontrivial effort; then, a PPA can really save your day.)
  • You want to develop a Python script of your own and ideally have the latest and greatest features of the Python packages you depend on - usually then use pip
    • ... or even install things directly from the upstream Github project or whatever, for the really bleeding edge. But probably don't stretch too far. If you don't have a professional software development team at your disposal, stick to reasonably stable versions for all but the most valuable, most crucial one or two packages you depend on.

To recap, what makes sense ultimately depends on where in the maturity cycle you are. The benefit of official Debian packages is that they tend to be very stable and time-tested, but the drawback is then that you will not be running the latest versions with the spiffiest new features.

Also keep in mind that some Debian packages go to extra lengths to integrate the packaged software with the broader Debian ecosystem. For a random Python script this is typically unimportant, but if it's a Debian system administration tool or some sort of infrastructure project, obviously you want all the Debian parts that upstream might not have, or might not enable and configure correctly by default.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .