4

When I use apt-get to accidentally install a package which is already installed as a dependency of another package, it breaks that "dependency" by marking the package as manual instead of auto.

I can manually fix this later using sudo apt-mark markauto package-name.

Is there a way to tell apt-get (or aptitude, dpkg, or another tool) not to break the link in the first place or to do nothing if the requested package is already installed?

I run into this a couple of different ways:

I have a list of packages to install to make something specific work (from instructions on a website like this).

I have a have a list of all the packages I've added to one install and I copy it so they will all get added to another install - e.g. when I do a clean install rather than an upgrade of my system.

  • How it breaks the dependency by marking "manual"? – Anwar Aug 30 '16 at 13:45
  • @Anwar That's why "dependency" is in quotes. I didn't know the precise term to use. When a package gets pulled in just to satisfy the needs of another package, it is marked as auto. If you uninstall everything that depends on that package, it will be automatically uninstalled - or at least - listed in "the following packages are no longer needed and can be removed with sudo apt-get autoremove' or something similar. Once it is marked as manual`, that won't happen. – Joe Aug 31 '16 at 1:47
  • Yes, but without marking manual when you write them explicitly in apt-get install line, it would have no knowledge if you actually wanted them or not? They can't know our intention, right? It must have a mechanism to automate the process. And this process though to precise, is doing job. Only downside may be, you're being in a state of some extra unneeded packages. Nothing more. – Anwar Aug 31 '16 at 3:10
  • @Anwar - Writing them explicitly in an installation command like apt-get is the definition of manual. If I install something like LibreOffice (conceptually, one package), just a few of the 30 or more packages it needs (specified somewhere inside the deb file(s) itself) will probably end up marked as manual. If I uninstall it later, it will get rid of all of them - unless another package is still using some of them. Having extra packages installed isn't a huge deal, but they still get updates for nothing and one of them might get stuck at a version that blocks something else, etc.. – Joe Sep 1 '16 at 4:08
  • A better example might be if I had gnome2 installed and wanted to try KDE (lots of debs). I might also separately install some package that used the KDE libraries. Later, if I uninstalled KDE, then the system would be able to remove the desktop itself and a bunch of other automatic parts, but it would see that some things, like the KDE libraries which were automatically installed, were still needed by my KDE application and would leave them there. Later, if I uninstalled the package which used KDE libraries, the system would know it could now get rid of the rest of those automatic packages. – Joe Sep 1 '16 at 4:22
2

You said

When I use apt-get to accidentally install a package which is already installed as a dependency of another package, it breaks that "dependency" by marking the package as manual instead of auto

I think, you got it slightly wrong. It will not break the dependency chain. The other package still depends on this marked as manual package. The only thing that is affected is that, if you now run apt-get autoremove these marked as manual packages won't get removed. Because, for apt marked as manual means user installed them deliberately, not because User wanted other packages which in-return depended on these packages.

You asked

Is there a way to tell apt-get (or aptitude, dpkg, or another tool) not to break the link in the first place or to do nothing if the requested package is already installed?

I haven't found any tools to do so automatically until now. But this doesn't sound logical either. Because, in that case it will be like every packages you're installing will be automatically installed, making them vulnerable to apt-get autoremove command.

So, If you want some package to be marked as automatically installed despite being installed manually, you must use apt-get markauto or aptitude mark-auto.

  • I knew "dependency" was not the precise term. That's why I put it in quotes. I just figured out how to do a workaround using bash - which I'll post as an answer. – Joe Aug 31 '16 at 2:03
0

If there's no way to do this with an existing tool, here's a workaround.

Run each package through a bash script like the following:

#!/bin/bash
dpkg -s "$1" &> /dev/null
(($?)) && sudo apt-get install "$1"

dpkg -s returns 0 if the package (given as the argument to the script) is installed; 1 if it is not. This script will do nothing if the package is installed, but install it if it is not - avoiding the original problem.

The dpkg command outputs a bunch of information which is not needed here so it is redirected into the /dev/null bit bucket to get rid of it.

(($?)) looks at the return code from dpkg. It works like C - opposite of normal bash tests. If $? is 1 because the package is not installed, this will evaluate as true and trigger the && clause to install the package.

Depending on what is needed, this can be made into a function and/or placed in a loop over a list of package names.

If this would be used a lot, then code would also be added to verify that the argument, $1, wasn't null and didn't contain embedded blanks or other garbage. But all of that is just normal scripting and not part of the solution itself.

I found the dpkg technique here.

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.