43

I installed a JDK package on my Ubuntu 14.04 a few months ago, but I don't know how to check which version it is.

So how to do I find whether a package is installed on Ubuntu and which version of it is installed?

4
  • @muru Although some of the answers on that question seem like attempts to answer what's been asked here, most of that question seems to be asking how to find out what tasks have been installed with tasksel (which runs during the server installation). I cannot be sure precisely what information that OP wanted--and it's true they did accept an answer that was about checking what packages were installed, though not about checking for a single named package--but I expect most users who find that question by searching will want to know about tasksel. Feb 5, 2017 at 16:33
  • @EliahKagan since both the question, and some of the answers, cover the relevant parts of this one, I don't see much of a point in that distinction (and no, I don't hold that expectation - I got that by Googling "ubuntu check package installed", where it was the top result for me)
    – muru
    Feb 5, 2017 at 16:37
  • 1
    @muru How do you feel about How can I check if a package is installed (no superuser privileges)? There seem to be several other related questions closed as duplicates of that already, (And maybe answers from some of these could be merged into whichever is selected as the dupe target?) Feb 5, 2017 at 16:45
  • @EliahKagan yes, that one's good
    – muru
    Feb 5, 2017 at 16:47

4 Answers 4

53

TL;DR: For a simple command (no | grep) with compact output, you can use

  • apt list package, which is usually what you want (compare to apt-cache policy package), or
  • dpkg-query -W package as a less cluttered version of dpkg -l package.

The apt command (for apt list package) is available in Ubuntu 14.04 and later.

apt list tells you if your package is installed.

apt list package shows the version of a package that is or would be installed, along with the names of the repository components that provide it and the version. When the package is installed, [installed] appears prominently at the end of the line.

ek@Io:~$ apt list firefox
Listing... Done
firefox/xenial-updates,xenial-security,now 51.0.1+build2-0ubuntu0.16.04.1 amd64 [installed]
N: There is 1 additional version. Please use the '-a' switch to see it

In addition to [installed] at the end, now in the list of repository components indicates that the package is currently installed. This is what apt list's output looks like for a package that isn't installed:

ek@Io:~$ apt list dolphin
Listing... Done
dolphin/xenial 4:15.12.3-0ubuntu1 amd64

Sometimes you'll see [installed,automatic], which means the package was installed as a dependency of some other package. Since it was automatically rather than manually installed, it can be removed after all the packages that depend on it are removed (which is what the apt autoremove command does).

ek@Io:~$ apt list zeitgeist-core
Listing... Done
zeitgeist-core/xenial,now 0.9.16-0ubuntu4 amd64 [installed,automatic]

You can use apt list --installed to show only installed packages.

One advantage of seeing a listing even when the package isn't installed is that you know there is a package of the name you specified. Otherwise, if you spell it wrong by accident, you might think the package you meant isn't installed.

ek@Io:~$ apt list firefoz
Listing... Done

But if you really do prefer that nothing appear if you don't have a package, use apt list --installed package:

ek@Io:~$ apt list --installed firefox dolphin
Listing... Done
firefox/xenial-updates,xenial-security,now 51.0.1+build2-0ubuntu0.16.04.1 amd64 [installed]
N: There is 1 additional version. Please use the '-a' switch to see it

Globbing helps you list packages whose names you don't know or want to type.

As you've just seen, you may pass multiple package names to apt list. The other thing you can do is pass it a glob, where a * represents zero or more characters and ? represents one character. There are a few things to keep in mind:

  • Your shell also expands globs into filenames, but that is not what you want to happen here, so you should always quote them (in ' ' or " ", or with \) when you pass them to apt list.
  • Although both globs and regular expressions both support character classes (e.g., [csz]sh matches csh, ssh zsh), they are not the same and * and ? mean different things in a glob than in a regular expression. This is important because some other commands used to get information about packages, such as apt-cache policy package, use regular expressions rather than globs. You should check if a command uses globs or regular expressions before relying on either feature.
  • When you are actually adding and removing packages, you must not confuse globs with regular expressions. apt list 'wine*' lists all the packages whose names start with wine, but apt remove 'wine*' removes all the packages with win (even without the e) anywhere in their name!

Suppose, for example, that I know the package I'm interested in has soap in its name somewhere and ends in cil, but that's all I remember:

ek@Io:~$ apt list '*soap*cil'
Listing... Done
libmono-system-runtime-serialization-formatters-soap4.0-cil/xenial,xenial,now 4.2.1.102+dfsg2-7ubuntu4 all [installed,automatic]

Globbing is also handy when you do know the full name of the package you're interested in, but don't want to type it. Or when you're interested in multiple packages that match the glob:

ek@Io:~$ apt list 'firefox-locale-*'
Listing... Done
firefox-locale-af/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-an/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ar/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-as/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ast/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-az/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-be/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-bg/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-bn/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-br/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-bs/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ca/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-cak/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-cs/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-csb/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-cy/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-da/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-de/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-el/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-en/xenial-updates,xenial-security,now 51.0.1+build2-0ubuntu0.16.04.1 amd64 [installed]
firefox-locale-eo/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-es/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-et/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-eu/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-fa/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-fi/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-fr/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-fy/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ga/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-gd/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-gl/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-gn/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-gu/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-he/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-hi/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-hr/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-hsb/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-hu/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-hy/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-id/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-is/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-it/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ja/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ka/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-kab/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-kk/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-km/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-kn/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ko/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ku/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-lg/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-lt/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-lv/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-mai/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-mk/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ml/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-mn/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-mr/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ms/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-nb/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-nl/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-nn/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-nso/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-oc/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-or/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-pa/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-pl/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-pt/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ro/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ru/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-si/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-sk/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-sl/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-sq/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-sr/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-sv/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-sw/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-ta/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-te/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-th/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-tr/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-uk/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-uz/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-vi/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-xh/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-zh-hans/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-zh-hant/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64
firefox-locale-zu/xenial-updates,xenial-security 51.0.1+build2-0ubuntu0.16.04.1 amd64

That's a lot of packages. (Though not as many as if I had run apt list '*', which lists all available packages!) Fortunately globs work with --installed too:

ek@Io:~$ apt list --installed 'firefox-locale-*'
Listing... Done
firefox-locale-en/xenial-updates,xenial-security,now 51.0.1+build2-0ubuntu0.16.04.1 amd64 [installed]
N: There is 1 additional version. Please use the '-a' switch to see it

apt list vs. dpkg-query -W

Another command you can use is dpkg-query -W package. This is similar to dpkg -l, but its output is more streamlined and readable because only the package name and installed version (if any) are printed.

dpkg-query -W lists all packages dpkg knows about that match the name or glob pattern you give it. This includes packages that are installed and also packages that aren't installed but that are referred to by another package. You can tell what packages are actually installed because the installed version number/string is printed to the right of the name. For example, on my 16.04 system:

ek@Io:~$ dpkg-query -W wine-\*
wine-amd64
wine-gecko
wine-gecko2.21:amd64    2.21-0ubuntu1
wine-gecko2.21:i386     2.21-0ubuntu1
wine-i386
wine-mono
wine-mono0.0.8  0.0.8-0ubuntu1

Thus, of listed packages, wine-gecko2.21:amd64 and wine-gecko2.21:i386 (i.e., wine-gecko2.21 for both the amd64 and i386 architectures) and wine-mono0.0.8 are the ones that I currently have installed.

dpkg-query -W and apt list will both always know about installed packages, whether or not they are provided by any currently configured software source and whether or not they are referred to by other packages. However:

  • apt list also knows about packages available from any currently configured software source, but does not knows about unavailable packages referred to by other packages.
  • dpkg-query -W (like dpkg -l) does not directly reflect what packages are actually available, but does know about packages referred to by any package that is installed.

On the same machine:

ek@Io:~$ apt list wine-\*
Listing... Done
wine-development/xenial,xenial 1.9.6-1 all
wine-gecko2.21/xenial,now 2.21-0ubuntu1 amd64 [installed,automatic]
wine-mono0.0.8/xenial,xenial,now 0.0.8-0ubuntu1 all [installed,automatic]

As with dpkg -l, when you run dpkg-query -W with the name of a package you don't have (e.g., I don't have chocolate-doom installed), the output is the same as when you run it with the name of a package that doesn't exist at all (for example, due to misspelling, like firefoz):

ek@Io:~$ dpkg-query -W chocolate-doom firefoz
dpkg-query: no packages found matching chocolate-doom
dpkg-query: no packages found matching firefoz

In contrast, apt list shows explicitly that only chocolate-doom exists (but isn't installed):

ek@Io:~$ apt list chocolate-doom firefoz
Listing... Done
chocolate-doom/xenial 2.2.1-2.1 amd64
0
11

In a terminal, run dpkg -l | grep <name or pattern>. For example,

dpkg -l | grep firefox
ii  firefox       51.0.1+build2-0ubuntu0.14.04.2                      amd64 

The ii thing means it is installed. Obviously, if there is no output, the package is not installed.

1
  • 5
    Or simply dpkg -l firefox
    – lamino
    Feb 5, 2017 at 2:03
10

If you know package name, use apt-cache policy:

$ apt-cache policy firefox | grep 'Installed:'                                                                           
  Installed: 51.0.1+build2-0ubuntu0.16.04.1
$ apt-cache policy golang | grep 'Installed:'                                                                            
  Installed: (none)

If you don't , use dpkg --get-selections | grep 'jdk'

$ dpkg --get-selections | grep 'jdk'
jdk1.8.0-111                    deinstall
openjdk-7-jre-headless:amd64            deinstall
openjdk-8-jdk:amd64             install
openjdk-8-jdk-headless:amd64            install
openjdk-8-jre:amd64             install
openjdk-8-jre-headless:amd64            install
openjdk-9-jre-headless:amd64            deinstall
4

Use dpkg command, which is package manager for Debian.

>dpkg -s /package name/  

Use file /var/lib/dpkg/available to find out all package names available to you.

Or you can use following command (It lists all packages in /var/lib/dpkg/status):

>dpkg-query -l  

You can also try to match package name using wild cards:

>dpkg-query -l 'libc6*'
2
  • 1
    This is incorrect. dpkg-query -l will also list packages that you installed and then removed.
    – josch
    Feb 15, 2022 at 12:38
  • dpkg -s will also displayed uninstall packages. Oct 13, 2023 at 10:49

Not the answer you're looking for? Browse other questions tagged .