dpkg-query: Usage, Options & Common CI Errors
dpkg-query reports what Debian packages are installed and which files they own.
dpkg-query is the scriptable way to check installed state on Debian/Ubuntu - handy in CI to assert a package is present or to find which package owns a file.
What it does
dpkg-query reads the dpkg database and reports installed packages, their versions, status, and the files they ship. It is read-only and is the precise tool for "is this installed?" checks in a pipeline (more scriptable than dpkg -l, which is paginated/formatted).
Common usage
dpkg-query -W -f='${Version}\n' curl # print just the version
dpkg-query -l 'nginx*' # list matching packages
dpkg-query -L curl # files installed by curl
dpkg-query -s curl # full status block
dpkg-query -S /usr/bin/curl # which package owns a fileCommon errors in CI
dpkg-query exits 1 when no package matches, e.g. "dpkg-query: no packages found matching X" - under set -e that aborts the step, so guard it (dpkg-query -W X 2>/dev/null || echo missing). The -f / --showformat string uses fields like ${Version} and ${Status}; an unescaped $ gets eaten by the shell, so single-quote the format. dpkg-query -S only finds files that belong to an installed package (not files you created).
Options
| Flag | What it does |
|---|---|
| -W / --show | Show packages, formatted with -f |
| -f / --showformat | Custom output format (${Version}, ${Status}) |
| -l / --list | List installed packages (paginated table) |
| -L / --listfiles | List files a package installed |
| -s / --status | Show the full status entry |
| -S / --search | Find which package owns a path |