193

If I use apt-get install -qq mono-devel, I expect it to be quiet except for errors, according to the help:

-qq No output except for errors

Instead I get:

Extracting templates from packages: 100%
Selecting previously unselected package binfmt-support.
(Reading database ... 84711 files and directories currently installed.)
Unpacking binfmt-support (from .../binfmt-support_2.0.8_i386.deb) ...
Selecting previously unselected package cli-common.
Unpacking cli-common (from .../cli-common_0.8.2_all.deb) ...
Selecting previously unselected package libgdiplus.
Unpacking libgdiplus (from .../libgdiplus_2.10-3_i386.deb) ...
Selecting previously unselected package libmono-2.0-1.
Unpacking libmono-2.0-1 (from .../libmono-2.0-1_2.10.8.1-1ubuntu2.2_i386.deb) ...
Selecting previously unselected package libmono-2.0-dev.
Unpacking libmono-2.0-dev (from .../libmono-2.0-dev_2.10.8.1-1ubuntu2.2_i386.deb) ...
Selecting previously unselected package libmono-corlib4.0-cil.
Unpacking libmono-corlib4.0-cil (from .../libmono-corlib4.0-cil_2.10.8.1-1ubuntu2.2_all.deb) ...
Selecting previously unselected package libmono-system-xml4.0-cil.
Unpacking libmono-system-xml4.0-cil (from .../libmono-system-xml4.

and more...

In fact, a couple hundred lines worth of output. This does not appear to match up with no output except for errors.

How do I actually get apt-get install to print out only when there are errors keeping it from installing?

jbtule
  • 2,030
  • Have you tried using -q=# where # is a quiet level? (It's in the manual.) You may want to raise a bug report against this. – Paddy Landau Feb 26 '13 at 09:26
  • @PaddyLandau I did indeed. I don't know why it's talking about selecting previously unselected package either or why that would be important, and I'm not sure if it's related to the state of the vms I'm running this on these on either, travis-ci. But the answer worked well. – jbtule Feb 26 '13 at 13:38
  • "Selecting previously unselected package" simply means that the package manager is including a package required to satisfy dependencies. As I previously wrote, you may want to raise a bug report about the --quiet option appearing not to be working correctly. – Paddy Landau Feb 26 '13 at 17:35
  • I'm unhappy to report that same symptom in Ubuntu 15.04 20150709. – Lloyd Dewolf Aug 07 '15 at 21:02
  • 2
    Looks like this relates to 2009 dpkg issue report: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=539617 . – Lloyd Dewolf Aug 07 '15 at 21:18
  • Related: https://serverfault.com/questions/227190/how-do-i-ask-apt-get-to-skip-any-interactive-post-install-configuration-steps – Jesse Nickles Feb 22 '23 at 15:13

7 Answers7

137

The man page for apt-get is as follows:

NAME
       apt-get - APT package handling utility -- command-line interface

SYNOPSIS
       apt-get [-asqdyfmubV] [-o=config_string] [-c=config_file] [-t=target_release]
               [-a=architecture] {update | upgrade | dselect-upgrade | dist-upgrade |
               install pkg [{=pkg_version_number | /target_release}]...  | remove pkg...  |
               purge pkg...  | source pkg [{=pkg_version_number | /target_release}]...  |
               build-dep pkg [{=pkg_version_number | /target_release}]...  |
               download pkg [{=pkg_version_number | /target_release}]...  | check | clean |
               autoclean | autoremove | {-v | --version} | {-h | --help}}

The -q or -qq flag should go before the command, like so:

apt-get -qq upgrade

muru
  • 197,895
  • 55
  • 485
  • 740
Mike
  • 1,554
  • 1
  • 10
  • 8
  • 3
    This is the correct answer! Any practical implementation needs to preserve prompts. Used correctly, -q absolutely works (no "animated" output) as well as -qq (no output except errors). Please upvote! – Charney Kaye Aug 19 '15 at 19:18
  • 71
    No, it doesn't work. Even with -qq before the install command I still get tons of junk everything from reading database to unpacking and setting up messages. – CrazyCasta Jun 20 '16 at 23:55
  • @CrazyCasta what command are you using? – Mike Jun 21 '16 at 07:34
  • 2
    Well, I've tried sudo apt-get -qq -y install mercurial, sudo apt-get install -qq -y mercurial, sudo apt-get -qq install -qq -y mercurial and other variations involving even more q's (though I don't see any documentation that suggests this would work. I'm running Debian Jessie on google cloud btw (8). I've also tried Goetz's answer and it doesn't seem to work either. By doesn't seem to work I mean I don't notice a difference. – CrazyCasta Jun 22 '16 at 15:49
  • This works for me on Ubuntu 14.04 - are you sure that there hasn't been a change in behaviour for Debian Jessie? Does the apt-get manpage look like the above? – Mike Jun 24 '16 at 22:51
  • 1
    -qq doesn't wrok for me. apt-get -qq install build-essential still prints too much output. I'd like something like CentOS's yum install -y -q $package, which is totally silent and great to prevent noise in automation scripts. – Stefan Lasiewski Dec 07 '16 at 21:01
  • in Ubuntu 16.04.1 the -qq option is entirely silent. There is zero output. What are you seeing and what exactly is the command you are using @StefanLasiewski? – Mike Dec 07 '16 at 21:12
  • 4
    This is Ubuntu 16.04.1, running as a Docker container. My guess is that the messages are actually from dpkg, which is called by apt-get. The command is apt-get -qq update && apt-get -qq upgrade && apt-get -qq install build-essential. See https://gist.github.com/stefanlasiewski/be6776296e07e709e9d978d36c3681c5 – Stefan Lasiewski Dec 08 '16 at 19:13
  • Interesting @StefanLasiewski - I ran the Xenial docker image and ran your command exactly, but got a lot of output. Then I ran apt-get -qq update and it was silent...then apt-get -qq update && apt-get -qq upgrade (still silent) and finally the full command which was silent. I scrubbed that container and started a new one and ran apt-get -qq update alone, followed by the full command. The update was silent, but the full command wasn't. Following the process I did before worked. This seems like a bug to me, this is all very unexpected but fairly deterministic. – Mike Dec 09 '16 at 08:43
  • 4
    In Docker you have 2 alternatives: (1) run each command separately rather than with && joining them (might work, but it's not the "docker way") (2) redirect the output to /dev/null like in the other answer. Option 2 is probably your best bet while there is this bug. – Mike Dec 09 '16 at 08:44
  • 2
    @Mike: I noticed, that You are not running the same command as the OP - it was install and not update or upgrade. Those get silenced, but as in other posts, install uses dpkg underneath, which generates the output. This answer is not answering the original question. – MrMeszaros Aug 06 '18 at 08:19
  • 3
    This answer is wrong. -qq does not accomplish what OP wants, regardless of where or with which command it's used. – weaver Mar 29 '19 at 02:09
  • 4
    Same here: I tried putting the -qq before install as well as after install, and both scenarios still resulted in the Unpacking ..., Selecting ..., Preparing ..., etc. messages that I do not want to see. I am using the ubuntu:20.04 Docker container – Niema Moshiri Feb 09 '22 at 00:15
102

We faced the same problem. apt-get install -qq removes most of the outputs but annoying "(Reading database ..." still persist.

We took a look in the source of apt and discover that the output is produced by dpkg that was forked by apt. Then the source of dpkg shows that the annoying soutput is only issued when isatty(1) is true. This is only the case when the fork uses pty instead pipe. Back to apt, there is a undocumented configuration variable that allows to use pipe instead pty which then solve the problem:

apt-get install -qq -o=Dpkg::Use-Pty=0 <packages>

Expecting that can help others.

Philippe Goetz
  • 1,031
  • 1
  • 7
  • 2
  • 4
    Actually this is full correct answer. It's also allow to run apt-get with single -q and see some output and don't see annoying "Reading database" – valodzka Sep 30 '15 at 20:26
  • @valodzka Which ubuntu version are you on? On CircleCI (with Ubuntu 14.04) it steall reads (Reading database ... 625142 files and directories currently installed.). – koppor Jan 23 '16 at 12:25
  • I tested it on Debian 8 – valodzka Jan 24 '16 at 07:11
  • I tried this with Ubuntu 14.04 based Docker build and it doesn't work for me. – Cameron Taggart Mar 10 '16 at 00:44
  • 2
    I'm trying this on gcloud debian jessie (8) and I still get reading database lines and unpacking/setting up lines. – CrazyCasta Jun 21 '16 at 00:09
  • This works for me. If you aren't using -q or -qq then you still see the '(Reading database..' lines but they are put through pipe (like all the other output) instead of bypassing it. Perfect. – scoobydoo Oct 11 '17 at 06:28
  • This answer might not be accepted, but it definitely deserves the most upvotes since it is really the only answer that works with Ubuntu 16.04. – Giacomo1968 Apr 06 '18 at 13:59
  • 6
    This answer does not work for Ubuntu 18.04. – Amedee Van Gasse May 10 '18 at 16:06
  • 2
    There's still a bunch of Selecting/Preparing/Setting up lines... – hayd Jan 05 '19 at 23:03
  • 6
    The -o=Dpkg::Use-Pty=0 worked perfect on Ubuntu 18.04. It only shows one "Reading database" instead of multiple ones with progress updates every 5%. – wisbucky Jul 18 '19 at 22:24
  • 1
    I can specifically report that apt-get -o Dpkg::Use-Pty=0 install (in that exact order) is acceptably quiet on Ubuntu 18.04. I don't want to know why there's all that machinery in there to use a pty in the first place; it must be pretty horrific… – Donal Fellows Dec 18 '20 at 15:00
  • 4
    It seems that -o=Dpkg::Use-Pty=0 doesn't affect the output in Ubuntu 20.04. – alfC Mar 15 '21 at 09:43
  • This is the correct answer! Works on ubuntu 20.04 where -qq and other apt.conf options do not – Erin Schoonover May 30 '21 at 14:19
  • 2
    Adding -o=Dpkg::Use-Pty=0 seems to have successfully suppressed the messages in apt-get update and apt-get upgrade, but I'm still getting the Unpacking ..., Selecting ..., Preparing ..., etc. messages. I'm using the ubuntu:20.04 Docker container – Niema Moshiri Feb 09 '22 at 00:17
  • Can that also be set via an environment variable or config file? – OrangeDog Sep 20 '23 at 20:00
67

A simple redirection could do this. It's not exactly what you had in mind, I'm sure, but it sure as hell works :)

In short, just whack > /dev/null on the end of any command where you want to redirect all the stdout into nothingness. Things outputted on stderr will still show in the console.

$ sudo apt-get update > /dev/null
[sudo] password for oli: 
$ 

No junk! And here's what happens if we're silly and break something:

$ apt-get cheese > /dev/null
E: Invalid operation cheese
$
Oli
  • 293,335
  • 10
    That's a bit of a problem if you get a prompt... – l0b0 Feb 19 '13 at 16:44
  • 1
    If this is part a script, you wouldn't put a sudo in front of apt-get. You would just sudo when running the script instead. – Xion Feb 19 '13 at 16:49
  • 1
    @l0b0 Hmm, yeah it is. Don't really know how to suggest splitting out prompts other than knowing when to expect them (removals and unverified packages are the only ones in there IIRC). Or, add -y into the mix (with caution!) – Oli Feb 19 '13 at 16:53
  • 1
    The OP was fine with using -qq which implies -y. So adding a -y or just running his original apt-get upgrade -qq > /dev/null give the same result – Wulfhart Feb 19 '13 at 19:59
  • 7
    @Oli If you're absolutely sure you're not going to make your system catch on fire, you could always sudo apt-get upgrade -qq --force-yes > /dev/null. -qq implies -y, as WulfHart said, and --force-yes makes it plow through just about anything. – JamesTheAwesomeDude Feb 26 '13 at 13:46
  • @Oli, as far as I understand, your solution redirects both stdout and stderr to /dev/null. Am I correct or am I missing something? – Vorac Mar 27 '13 at 13:21
  • 2
    @Vorac No it's just redirecting stdout (seeing the errors is a desirable thing IMO). – Oli Mar 27 '13 at 13:34
  • @JamesTheAwesomeDude The man page says "you should never use -qq without a no-action modifier...as APT may decided to do something you did not expect". – Mike R Apr 24 '14 at 15:56
  • 6
    This is not the answer to the question; it's a workaround. – Charney Kaye Aug 19 '15 at 19:19
6

As you can see here and here
You can do:

export DEBIAN_FRONTEND=noninteractive
apt-get -yq install [packagename]
export DEBIAN_FRONTEND=dialog

Or one line:

DEBIAN_FRONTEND=noninteractive apt-get -yq install [packagename]
Mark
  • 61
3

With apt-get -qq install -y PACKAGE 2> /dev/null you can:

  1. Show only the errors with -qq.
  2. Send the errors to /dev/null (not show) so with that you can install a package with zero output.
Eliah Kagan
  • 117,780
trxgnyp1
  • 131
1

For me this works perfectly for fully silent/zero output while installing cause NULL STDIN and STDOUT.

DEBIAN_FRONTEND=noninteractive apt-get install -qq [packagename] < /dev/null > /dev/null

Hope it helps!!

-1

You can use a python script to do literally anything with the stdout and stderr of every command. For example, if you want to see a dynamic output including only the last line of the output, you can use the following script :

#!/usr/bin/python3
import subprocess, sys
process = subprocess.Popen(sys.argv[1:], stdout=subprocess.PIPE)
while process.poll() is None:
  stdout = process.stdout.readline().decode()[:-1]
  print(stdout,end='\r')
print()

If you know how to work with strings in python, you can tweak the script to print only the lines you want to be printed, in whatever format you wish.

Save the above code in a file such as run.py. Then you can run any command to see only the last line of the output dynamically. For example:

./run.py sudo apt-get install <package>

or:

./run.py sudo apt-get update
Note 1: Don't forget to give exec permission to your script:
chmod +x run.py
Note 2: You can make a proper symlink for a more convenient usage (or alternatively add the script path to $PATH environment variable which I do not suggest):
sudo ln -s <path/to/yourscript.py> /usr/bin/run

Afterwards, you can only use:

run <command-with-desired-output-format>