How do I check if a package is installed on my server?
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.
Related videos on Youtube
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, 2022Comments
-
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:
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 about 5 years
$ apt-cache policy <package-name>
, e.g.$ apt-cache policy dillo
-
-
D.Zou over 10 yearsthanks for telling me the name of the task, that was one of my problems, wasn't sure which package to look for.
-
heypaleblue about 9 yearsWhat is the diference between this and dpkg-query -l "packagename"
-
Brannon over 7 yearsYou can put a star on the end of the package name for a wildcard.
-
Viktor Benei over 7 yearsDo 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 about 7 years
dpkg -l | cut -d " " -f 3 | grep "^vlc"
to show all package names that start withvlc
-
sjas almost 7 yearspipes in ansible can be used with the
shell
module. -
mtalexan over 6 yearsFYI, 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 over 6 yearsYou can also use the --installed option for that command to limit to matches which are actually installed.
-
Gabriel Staples over 6 yearsWhat's the
-qq
for? It seems to work identically without it, and that option isn't in the manualman apt
-
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 over 4 yearsEven on Bionic,
apt list
still seems to be "WIP". Anyway for interactive use it does the job. I think in generalapt
family isn't meant for scripting, in which case you would still wantdpkg
. -
Franklin Yu over 4 yearsThis has been mentioned by Victor two years ago.
-
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 encountereddpkg
just runsdpkg-deb
ordpkg-query
with the parameters given to it..." -
Rockallite over 4 yearsAlso
apt-cache show <pkg_name>
to show detailed information about installed and installable versions of a package. -
Smar about 4 years
apt
shouldn’t be used in scripts, the command always says. -
Manuel Jordan over 3 yearsReally is
apt list --installed | grep <package>
, the--installed
part is mandatory -
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 thegrep
is excessive since you can specify the package to the command directly. -
Manuel Jordan over 3 yearsAgree, 'grep' is a secondary filter .... would be nice indicate the explicit difference between
list
andsearch
... -
sk8forether over 3 years@ManuelJordan Added :-)
-
Manuel Jordan over 3 yearsIs interesting how these two options "search" and "list" are not merged in one command, something like
list --remote
(same assearch
to search in the repositories and without matter if the<package>
is complete or not) andlist --install
(to search in the local computer) all the currently installed -
Marinos An about 3 yearsWhat the answer does not mention is: which output of
dpkg -l {package_name}
would inform that the package is installed? -
Treviño about 3 yearsWhile this is fine for informative version its return value isn't indicative
-
vkozyrev over 2 yearsThis is the most elegant way to check
-
js.mouret over 2 yearscareful as this will not work if the package was previously installed then removed.
-
Ciro Santilli OurBigBook.com over 2 years@js.mouret yes, updated.
-
josch over 2 years@MarinosAn The first column of the
dpkg -l
output has to sayii
for the package to be installed. If it says anything else (likerc
) then it is not fully installed. -
josch over 2 yearsHow 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 ofdpkg -l
has to sayii
or otherwise the package is not installed. -
josch over 2 yearsThis will be wrong for packages that have the string
installed
anywhere in their description. For example try runningdpkg -s debsums | grep installed
. -
Marinos An over 2 years@josch Thank you! Created an answer based on your feedback.
-
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 usedpkg-query --show --showformat='${db:Status-Status}\n'
or similar but notdpkg -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 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 outdpkg-query
is better so I have updated my answer. -
josch over 2 yearsThanks. 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 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 about 2 yearsCLI warning: apt does not have a stable CLI interface. Use with caution in scripts