How to find out if a specific package is installed on Debian?

apt-cache showpkg shows detailed information about potentially installable packages. It does indicate whether the package is installed, kind of, but not in a very readable way:

Versions:
2:3.6.6-6+deb7u5 (/var/lib/apt/lists/mirrordirector.raspbian.org_raspbian_dists_wheezy_main_binary-armhf_Packages)

If the package was installed, you'd see (/var/lib/dpkg/status) at the end of the line. However, this isn't fully reliable, because you'd also see this indication if the package was known to your system but not fully installed, e.g. if it was in the “package uninstalled but configuration files left over” state.

A more useful apt-cache subcommand is apt-cache policy. It clearly shows the installed version (if any) and the available version(s). For example, here's output from a machine which has samba installed but not samba-dev:

samba:
  Installed: 2:4.1.17+dfsg-2
  Candidate: 2:4.1.17+dfsg-2
  Version table:
 *** 2:4.1.17+dfsg-2 0
        500 http://ftp.fr.debian.org/debian/ jessie/main amd64 Packages
        100 /var/lib/dpkg/status
samba-dev:
  Installed: (none)
  Candidate: 2:4.1.17+dfsg-2
  Version table:
     2:4.1.17+dfsg-2 0
        500 http://ftp.fr.debian.org/debian/ jessie/main amd64 Packages

Alternatively, you can use the dpkg command to get information about your current system. APT is the software that manages the download of packages, dependency analysis, etc. Dpkg is the low-level software that carries out the actual installation of a package file.

dpkg -l samba

This shows a line beginning with i if the package is installed, and a line beginning with u or p or nothing at all if the package is not installed.

$ dpkg -l samba samba-dev
Desired=Unknown/Install/Remove/Purge/Hold
| 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
+++-==============-============-============-=================================
ii  samba          2:4.1.17+dfs amd64        SMB/CIFS file, print, and login s
dpkg-query: no packages found matching samba-dev

(dpkg-query is the dpkg subcommand that returns information about the package database.)

Note that if you just want to ensure that a package is installed, you can simply run

apt-get install samba

This won't do anything if the latest version of the package that's available in your distribution is already installed. It will install the package if it isn't installed yet, and it will upgrade it if you have an older version.


dpkg -l | grep -e package1 -e package2 ....

Will list packages you are interested with their current insstallation stauts. The output will be something like this

bala@bala-laptop:~$ dpkg -l | grep apache2
ii  apache2                                2.4.10-10                            amd64        Apache HTTP Server

The first 2 characters tell the status - ii here means First i - marked for installation Second i - Successfully installed

There are other status codes like r meaning marked for removal, p meaning marked for purging etc.