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?

apt-cache policy <package name>
| improve this answer | |

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.

| improve this answer | |
  • 3
    dpkg -l "*mysql*" also works and does not hide the explanatory lines in the output. – Philipp Wendler Aug 9 '12 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 '18 at 16:07

One more variant, using aptitude this time:

aptitude show <package>

Tab completion works here as well.

| improve this answer | |

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
| improve this answer | |
  • 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 '11 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.

| improve this answer | |
  • 4
    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 '14 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 '14 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 '14 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.

| improve this answer | |
  • 1
    And apt show for more details – wjandrea Jan 25 '19 at 1:07

dpkg -s programmatic usage

I like dpkg -s as it exits with status 1 if any of the packages is not installed, making it easy to automate it:

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

See also:

Tested on Ubuntu 18.10.

| improve this answer | |

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

| improve this answer | |

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.
| improve this answer | |

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.