How do I check if a package is installed on my server?

571,086

Solution 1

Are you looking to know about all dpkg commands with options? Have a read from the below link.

15 dpkg commands to Manage Debian based Linux Servers

To List all Installed Packages

Here less is a simple text reader used to scroll through the list of packages in a new buffer that opens in the existing terminal window. The list will not be mixed with other terminal commands and output. Hit q to return to the terminal prompt. See man less for more info.

dpkg -l | less

To check whether a package is installed or not:

dpkg -l {package_name}
dpkg -l vlc

To check if the package is installed or not (for example, vlc). If installed, launch the package:

dpkg -l | grep vlc

Show the location where the package is installed. The -S (capital S) stands for "search"

sudo dpkg -S {package_name}
sudo dpkg -S skype

To use Grep to search:

dpkg -l | grep {keywords}
dpkg -l | grep pdf

Solution 2

apt -qq list PACKAGE can also be used for checking whether the PACKAGE is installed.

If installed it'll print something like (with [installed] at the end of the line):

$ apt -qq list awscli
awscli/stable,now 1.4.2-1 all [installed]

If not installed the output will be:

$ apt -qq list awscli
awscli/stable 1.4.2-1 all

Solution 3

Use dpkg

This command is the Debian package manager.

You can list all the installed packages with

dpkg -l

You can see details for a specific package with

dpkg -p packagename

And to learn if it is installed, use

dpkg -s packagename | grep Status

You can learn which package contains the software you want with

apt-cache search name*  

In your case you should use this command to search the package name you want

apt-cache search virtual machine host 

Solution 4

I've found a way to check for package installation with just dpkg-query command and no pipes.

I searched for such solution while writing a task for ansible playbook.

You can do something like this:

/usr/bin/dpkg-query --show --showformat='${db:Status-Status}\n' '<your package name>'

This is just one another way to do the thing.

Solution 5

Simpler solution:

There is now an apt list command that lists available packages, and the --installed command will show only installed packages. You can also search for a specific package with

apt list <package>

Or to see only the matching installed packages

apt list <package> --installed

There are also the --upgradeable and --all-versions flags.

If you do not know the exact package name, you can use apt search followed by part of it to see a list of related packages. This will allow you to use a partial name or regex term(s) to filter a list of available packages, but it does not have the nifty flags for filtering that apt list has.

Also see man apt for more information.

Share:
571,086

Related videos on Youtube

D.Zou
Author by

D.Zou

I like the outdoors. Hiking, camping, kayaking, scuba diving, biking, cross country skiing and photography. Basically the outdoors. and engineering and technology.

Updated on September 18, 2022

Comments

  • D.Zou
    D.Zou almost 2 years

    I recently installed Ubuntu Server edition 13.10 (Saucy Salamander). But I have run into a bit of problem.

    At the end of the installation, it asks about several extra packages whether you want them or not, like OpenSSH server and virtual machine host. Here's what I'm looking at:

    dialog offering packages

    Now, my problem is that I can't remember if I have checked the "virtual machine host" checkbox. How do I check if my server has the corresponding packages?

    What packages come with the virtual machine host?

    • Daniel
      Daniel about 5 years
      $ apt-cache policy <package-name>, e.g. $ apt-cache policy dillo
  • D.Zou
    D.Zou over 10 years
    thanks for telling me the name of the task, that was one of my problems, wasn't sure which package to look for.
  • heypaleblue
    heypaleblue about 9 years
    What is the diference between this and dpkg-query -l "packagename"
  • Brannon
    Brannon over 7 years
    You can put a star on the end of the package name for a wildcard.
  • Viktor Benei
    Viktor Benei over 7 years
    Do not depend on the exit code of dpkg -l packagename, as it might return with a 0 exit code even if the package is not fully installed - see: github.com/bitrise-io/bitrise/issues/433
  • Gopal Venu
    Gopal Venu about 7 years
    dpkg -l | cut -d " " -f 3 | grep "^vlc" to show all package names that start with vlc
  • sjas
    sjas almost 7 years
    pipes in ansible can be used with the shell module.
  • mtalexan
    mtalexan over 6 years
    FYI, this doesn't work for meta packages since they don't correlate directly to a dpkg. Your response does answer the question as asked though.
  • mtalexan
    mtalexan over 6 years
    You can also use the --installed option for that command to limit to matches which are actually installed.
  • Gabriel Staples
    Gabriel Staples over 6 years
    What's the -qq for? It seems to work identically without it, and that option isn't in the manual man apt
  • Lupen
    Lupen over 6 years
    @GabrielStaples It looks like the apt list command isn't yet fully documented. Some quick testing on 16.04 seems to indicate that -q is a synonym for --quiet option. The first -q prevents the word "Done" being printed. The second also prevents "Listing... " from being printed.
  • Franklin Yu
    Franklin Yu over 4 years
    Even on Bionic, apt list still seems to be "WIP". Anyway for interactive use it does the job. I think in general apt family isn't meant for scripting, in which case you would still want dpkg.
  • Franklin Yu
    Franklin Yu over 4 years
  • Franklin Yu
    Franklin Yu over 4 years
    @heypaleblue They are the same. From manual page: "dpkg can also be used as a front-end to dpkg-deb(1) and dpkg-query(1). The list of supported actions can be found later on in the ACTIONS section. If any such action is encountered dpkg just runs dpkg-deb or dpkg-query with the parameters given to it..."
  • Rockallite
    Rockallite over 4 years
    Also apt-cache show <pkg_name> to show detailed information about installed and installable versions of a package.
  • Smar
    Smar about 4 years
    apt shouldn’t be used in scripts, the command always says.
  • Manuel Jordan
    Manuel Jordan over 3 years
    Really is apt list --installed | grep <package>, the --installed part is mandatory
  • sk8forether
    sk8forether over 3 years
    @ManuelJordan Yes and no. Technically the --installed is needed to see only installed packages (I've updated my post to make this clearer), but the grep is excessive since you can specify the package to the command directly.
  • Manuel Jordan
    Manuel Jordan over 3 years
    Agree, 'grep' is a secondary filter .... would be nice indicate the explicit difference between list and search ...
  • sk8forether
    sk8forether over 3 years
    @ManuelJordan Added :-)
  • Manuel Jordan
    Manuel Jordan over 3 years
    Is interesting how these two options "search" and "list" are not merged in one command, something like list --remote (same as search to search in the repositories and without matter if the <package> is complete or not) and list --install (to search in the local computer) all the currently installed
  • Marinos An
    Marinos An about 3 years
    What the answer does not mention is: which output of dpkg -l {package_name} would inform that the package is installed?
  • Treviño
    Treviño about 3 years
    While this is fine for informative version its return value isn't indicative
  • vkozyrev
    vkozyrev over 2 years
    This is the most elegant way to check
  • js.mouret
    js.mouret over 2 years
    careful as this will not work if the package was previously installed then removed.
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com over 2 years
    @js.mouret yes, updated.
  • josch
    josch over 2 years
    @MarinosAn The first column of the dpkg -l output has to say ii for the package to be installed. If it says anything else (like rc) then it is not fully installed.
  • josch
    josch over 2 years
    How can an answer that suggests using dpkg -l to check whether a package is installed get over 300 upvotes without explaining which part of the output actually tells the user whether a package is installed or not? Please annotate your answer to explain that the first column of dpkg -l has to say ii or otherwise the package is not installed.
  • josch
    josch over 2 years
    This will be wrong for packages that have the string installed anywhere in their description. For example try running dpkg -s debsums | grep installed.
  • Marinos An
    Marinos An over 2 years
    @josch Thank you! Created an answer based on your feedback.
  • josch
    josch over 2 years
    "Now in case it is not installed at all, dpkg by default prints an error" this is still not true. dpkg -l lists packages that once were installed but are not installed now without an error. Also, if you start using this in a shell pipeline, dpkg -l is not the command you want to use. You want to use dpkg-query --show --showformat='${db:Status-Status}\n' or similar but not dpkg -l which produces output meant for human consumption and not scripts. The output format could change in the future and then your script breaks.
  • Marinos An
    Marinos An over 2 years
    @josch "dpkg -l lists packages that once were installed but are not installed now without an error.": In that case I guess the line would not start with ii so && echo installed which is based only on grep exit code (false when no match) will not be executed. In any case as you correctly pointed out dpkg-query is better so I have updated my answer.
  • josch
    josch over 2 years
    Thanks. One thing is still wrong though. You write that dpkg uses dpkg-query. This is not true. Why do you write down things if you don't know whether they are true or not?
  • Marinos An
    Marinos An over 2 years
    @josch You are right. I misinterpreted the manpage which says: "dpkg can also be used as a front-end to dpkg-deb(1) and dpkg-query(1)". My bad, I updated.
  • Admin
    Admin about 2 years
    CLI warning: apt does not have a stable CLI interface. Use with caution in scripts