At our university we can get almost any ubuntu package installed we want, but we are not superusers ourselves (we need to request packages being installed).

With some libraries it is not always easy to know whether the package is already installed or not. Is there a simple way/command to check this?

9 Answers 9

apt-cache policy <package name>
  • 8
    It's important to know that it supports tab completion in the package name. So it's quite useful even when you don't know the exact package name. Aug 23, 2010 at 16:28
  • 1
    Shame that still exits with status 0 even if the package is not present. dpkg -s does however: askubuntu.com/a/1108801/52975 Jan 11, 2019 at 9:18
  • With friendlier apt: apt policy <package name> or apt show <package name> for more info. Jan 25, 2019 at 3:55

I always just use this from the command line:

dpkg -l | grep mysql

so the above asks dpkg to list all the installed packages and then I grep for only those that have mysql in the name.

  • 3
    dpkg -l "*mysql*" also works and does not hide the explanatory lines in the output. Aug 9, 2012 at 15:37
  • dpkg -l "*mysql*" (or dpkg -l '*mysql*') also makes special formatting of the output to fit each package on one line of the terminal window (grep doesn't). And this form allows you to choose the wildcard format (prefixes or suffixes), and whether to use wildcards at all (compared to grep). But grep gives nice coloring.. ))
    – vstepaniuk
    Feb 22, 2018 at 16:07
  • This is not correct. dpkg -l does not ask dpkg to list all installed packages. The output of dpkg -l will also include packages that you installed but then removed. The correct way to do this is by either checking that the first column of the dpkg -l output is equal to ii or by running: dpkg-query --showformat '${Status}\n' --show $pkg which will show install ok installed for packages that are actually installed.
    – josch
    Feb 15 at 12:40

One more variant, using aptitude this time:

aptitude show <package>

Tab completion works here as well.


You can use dselect. It provides non-su read-only access.

Also, dpkg -s <package name> provides a lot of details related to a package. Eg"

userme:~$ dpkg-query -s sl
Package: sl
Status: unknown ok not-installed
Priority: optional
Section: games
  • 1
    This is also available as just dpkg -s . And conversely, dpkg-query -l works just as well as dpkg -l or dpkg --list
    – belacqua
    Feb 9, 2011 at 8:35

You may use dpkg-query -s <package> 2>/dev/null | grep -q ^"Status: install ok installed"$ in scripts, since it returns exit code 1, if the <package> is not installed, and 0 if the <package> is installed.

  • 5
    Be careful: If dpkg -s returns 0, it doesn't necessarily mean that the package is fully/correctly installed. dpkg -s also returns 0 if the package is in half-configured or in config-files state (and I guess also in half-installed, but I didn't check that). See [the man page of dpkg(manpages.ubuntu.com/manpages/oneiric/man1/dpkg.1.html) for further "incomplete" states.
    – Ignitor
    Jan 17, 2014 at 15:13
  • 1
    @Ignitor, good point. My answer was wrong. The package could even be removed, but not purged. So I think you have to examine the output to check, if the package is installed or not.
    – jarno
    Feb 6, 2014 at 21:48
  • 2
    I edited the answer. Now it relies on output of dpkg-query. I don't know how portable this solution is; for example, may the text be displayed in another language in some system?
    – jarno
    Feb 6, 2014 at 22:09

Simpler solution:

There is now an apt list command that lists installed packages. You can also search for a specific package with

apt list <package>

See man apt for more information.

  • 1
    And apt show for more details
    – wjandrea
    Jan 25, 2019 at 1:07

dpkg-query --showformat='${db:Status-Status}'

This produces a small output string which is unlikely to change and is easy to compare deterministically without grep:

status="$(dpkg-query -W --showformat='${db:Status-Status}' "$pkg" 2>&1)"
if [ ! $? = 0 ] || [ ! "$status" = installed ]; then
  sudo apt install $pkg

The $? = 0 check is needed because if you've never installed a package before, and after you remove certain packages such as hello, dpkg-query exits with status 1 and outputs to stderr:

dpkg-query: no packages found matching hello

instead of outputting not-installed. The 2>&1 captures that error message too when it comes preventing it from going to the terminal.

For multiple packages:

pkgs='hello certbot'
for pkg in $pkgs; do
  status="$(dpkg-query -W --showformat='${db:Status-Status}' "$pkg" 2>&1)"
  if [ ! $? = 0 ] || [ ! "$status" = installed ]; then
if "$install"; then
  sudo apt install $pkgs

The possible statuses are documented in man dpkg-query as:

   n = Not-installed
   c = Config-files
   H = Half-installed
   U = Unpacked
   F = Half-configured
   W = Triggers-awaiting
   t = Triggers-pending
   i = Installed

The single letter versions are obtainable with db:Status-Abbrev, but they come together with the action and error status, so you get 3 characters and would need to cut it.

So I think it is reliable enough to rely on the uncapitalized statuses (Config-files vs config-files) not changing instead.

dpkg -s exit status

This unfortunately doesn't do what most users want:

pkgs='qemu-user pandoc'
if ! dpkg -s $pkgs >/dev/null 2>&1; then
  sudo apt-get install $pkgs

because for some packages, e.g. certbot, doing:

sudo apt install certbot
sudo apt remove certbot

leaves certbot in state config-files, which means that config files were left in the machine. And in that state, dpkg -s still returns 0, because the package metadata is still kept around so that those config files can be handled more nicely.

To actually make dpkg -s return 1 as desired, --purge would be needed:

sudo apt remove --purge certbot

which actually moves it into not-installed/dpkg-query: no packages found matching.

Note that only certain packages leave config files behind. A simpler package like hello goes directly from installed to not-installed without --purge.

Tested on Ubuntu 20.10.

Python apt package

There is a pre-installed Python 3 package called apt in Ubuntu 18.04 which exposes an Python apt interface!

A script that checks if a package is installed and installs it if not can be seen at: https://stackoverflow.com/questions/17537390/how-to-install-a-package-using-the-python-apt-api/17538002#17538002

Here is a copy for reference:

#!/usr/bin/env python
# aptinstall.py

import apt
import sys

pkg_name = "libjs-yui-doc"

cache = apt.cache.Cache()

pkg = cache[pkg_name]
if pkg.is_installed:
    print "{pkg_name} already installed".format(pkg_name=pkg_name)

    except Exception, arg:
        print >> sys.stderr, "Sorry, package installation failed [{err}]".format(err=str(arg))

Check if an executable is in PATH instead

See: https://stackoverflow.com/questions/592620/how-can-i-check-if-a-program-exists-from-a-bash-script/22589429#22589429

See also

  • 1
    No. dpkg -s also exits with zero if the package is Status: deinstall ok config-files. To try this out yourself, run dpkg -l on your system and pass a package to dpkg -s which has an rc in the first column of your dpkg -l output. Even though the package is not installed, dpkg -s will exit with success.
    – josch
    Feb 15, 2021 at 9:49
  • @josch I have discussed possibly related issues further at: stackoverflow.com/questions/1298066/… BTW. I don't have a perfect solution for it yet. Feb 15, 2021 at 9:55
  • Why are the solutions that use dpkg-query -W --showformat='${Status}\n' ... not perfect in your opinion?
    – josch
    Feb 15, 2021 at 13:25
  • @josch have they clearly documented that the exact string output is a stable API? Just cautious since install ok installed is a bit long to grep for. I haven't really extensively researched this though. Feb 15, 2021 at 13:28
  • But true, dpkg-query -W --showformat='${db:Status-Status}' does seem decent, outputs only installed Feb 15, 2021 at 13:37

You need to check the status printed by dpkg -l, example :

$ dpkg -l firefox-esr vim winff
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                 Version                 Architecture            Description
hi  firefox-esr                          52.9.0esr+build2-0ubunt amd64                   Safe and easy web browser from Mozilla
ii  vim                                  2:8.1.1198-0york0~14.04 amd64                   Vi IMproved - enhanced vi editor
rc  winff                                1.5.3-3                 all                     graphical video and audio batch converter using ffmpeg or avconv

Here both vim and firefox-esr are installed, therefore you can type :

$ dpkg -l firefox-esr | grep -q ^.i && echo This package is installed. || echo This package is NOT installed.
This package is installed.
$ dpkg -l vim | grep -q ^.i && echo This package is installed. || echo This package is NOT installed.
This package is installed.
$ dpkg -l winff | grep -q ^.i && echo This package is installed. || echo This package is NOT installed.
This package is NOT installed.

Example to use specific value as var in shell scripts (eg test.sh)

INSTALLED=$(dpkg -l | grep ${PACKAGE} >/dev/null && echo "yes" || echo "no")

echo "${PACKAGE} is installed ... ${INSTALLED}"

Make it executable and go start with:

test.sh openssh-server

Or do whatever you want with in your scripts

  • This is incorrect. dpkg -l will also list packages that you once installed but then removed. Also, grepping dpkg -l output as you did will also match on all the text in the package descriptions. The right way is to use dpkg-query -W --showformat='${db:Status-Status}' $package.
    – josch
    Feb 15 at 12:43

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.