How can I get a 'verbose' exit code for apt-get so that errors like this

Package google-chrome is a virtual package provided by:
  google-chrome-stable 29.0.1547.76-r223446
  google-chrome-beta 30.0.1599.47-1
You should explicitly select one to install.

E: Package 'google-chrome' has no installation candidate

have a different exit code than download errors like this

Err http://dl.google.com/linux/chrome/deb/ stable/main google-chrome-stable amd64 29.0.1547.76-r223446
  Something wicked happened resolving 'dl.google.com:http' (-11 - System error)

Currently, both errors exit with 100

Basically, I'd like to have download errors exit with a different code than errors like the first. If possible, I'd like each specific error to have a different exit code, but the above example is the minimum I need. How can I do this in a vanilla Ubuntu install without additional software (with the exception of aptitude)?

  • 1
    You could wrap the apt-get command in a script that parses the output. Or does that count as "additional software"? Also, to detect download errors, consider doing apt-get --download-only first. – Jos Sep 20 '13 at 8:24
  • Ah, that makes sense. Perhaps you could verify the existence of package X first by doing apt-cache policy X? – Jos Sep 20 '13 at 8:31
  • Again, you would need to grep the output. I assume that doing apt-cache policy google-chrome does not give you a line with the word "Candidate", whereas apt-cache policy google-chrome-stable would. – Jos Sep 20 '13 at 8:40
  • OK. I'm moving this discussion to chat and entering this suggestion as a (partial) answer. – Jos Sep 20 '13 at 8:51
  • let us continue this discussion in chat – Jos Sep 20 '13 at 8:53

Not a complete answer, but you could test for the existence of packages first by doing apt-cache policy X and grepping the output for "Candidate (none)" or "Couldn't find package X", etc.

  • Upvoted for now, but as you said, it's partial. Feel free to delete all your comments that it's in an answer now. Thanks for your help – kiri Sep 20 '13 at 9:02

I believe this is not a case of XY, and you just want to be able to debug apt-get generally speaking. Under such assumptions here is my answer.

From the man apt.conf:


Enabling options in the Debug:: section will cause debugging information to be sent to the standard error stream of the program utilizing the apt libraries, or enable special program modes that are primarily useful for debugging the behavior of apt.

Hence, you only need to activate the rules for each of the debugging behaviors of apt-get:

  • Debug::pkgProblemResolver: enables output about the decisions made by dist-upgrade, upgrade, install, remove, purge.
  • Debug::NoLocking: disables all file locking. This can be used to run some operations (for instance, apt-get -s install) as a non-root user.
  • Debug::pkgDPkgPM: prints out the actual command line each time that apt invokes dpkg(1).

Those 3 are the first that the man page recommends, and the first will help you to debug the first error you show. For the second you may need:

  • Debug::Acquire::http: Print information related to downloading packages using HTTP. There is also https, ftp, cdrom.

The man page has many more, you can list them easily using man apt.conf | grep -A5 -i debug.

All this should be written using the correct syntax and they are accept boolean values:

Debug::*::*  "true";

or if you want to use multiple lines:

Debug {
  Acquire {
    http "true";
    ftp "true";
  NoLocking "true";

If you want to run for only for a instance of apt-get you can use the -o/--option switch:

sudo apt-get -o Debug::pkgProblemResolver=true -o Debug::Acquire::http=true -f install 

Another method is creating your personalized configuration file and load it with the -c/--config-file switch:

sudo apt-get -c debug-apt.conf install hello

About changing the exit codes, I believe you can not unless you change the source code. apt-get is a state of art piece of software, as such it has advanced methods to debug the process without the need of relying of exit codes.

  • I'm actually trying to put these commands in a script which can run unattended. How would I use this in a bash script to find if the first error occurred? – kiri Sep 21 '13 at 2:54
  • I'd also like to do this only for a specific instance of apt-get, not all the time. – kiri Sep 21 '13 at 2:57
  • @minerz029 you can use the -o option to run ad-hoc commands. Updated my answer. – Braiam Sep 21 '13 at 10:10
  • About first comment, you should do something clever with parsing the output. I don't have the sightless idea how can you do that. – Braiam Sep 21 '13 at 10:16
  • I actually got it to work with grepping the output of apt-cache policy PACKAGE, as Jos said. I was trying to check if a package was installable, I may not have asked my question right but this is a good answer – kiri Sep 21 '13 at 22:26

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.