299

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?

1
  • 6
    $ apt-cache policy <package-name>, e.g. $ apt-cache policy dillo
    – Daniel
    Apr 10, 2019 at 12:21

14 Answers 14

351

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
9
  • 1
    What is the diference between this and dpkg-query -l "packagename" May 24, 2015 at 17:56
  • 14
    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 Oct 25, 2016 at 18:08
  • 1
    @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..." Nov 25, 2019 at 22:19
  • 1
    What the answer does not mention is: which output of dpkg -l {package_name} would inform that the package is installed?
    – Marinos An
    Apr 15, 2021 at 9:21
  • 2
    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
    Feb 15 at 12:48
101

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
6
  • 3
    You can put a star on the end of the package name for a wildcard.
    – Brannon
    Oct 22, 2016 at 3:09
  • 7
    You can also use the --installed option for that command to limit to matches which are actually installed.
    – mtalexan
    Oct 11, 2017 at 19:17
  • 7
    What's the -qq for? It seems to work identically without it, and that option isn't in the manual man apt Mar 8, 2018 at 0:09
  • 12
    @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. Apr 3, 2018 at 11:39
  • 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. Nov 25, 2019 at 18:43
55

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 
1
  • 1
    Also apt-cache show <pkg_name> to show detailed information about installed and installable versions of a package.
    – Rockallite
    Jan 16, 2020 at 0:56
19

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.

2
  • pipes in ansible can be used with the shell module.
    – sjas
    Aug 27, 2017 at 9:51
  • 1
    This is the most elegant way to check
    – vkozyrev
    Nov 18, 2021 at 10:47
18

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.

6
  • 1
    This has been mentioned by Victor two years ago. Nov 25, 2019 at 21:57
  • Really is apt list --installed | grep <package>, the --installed part is mandatory Mar 24, 2021 at 19:08
  • 1
    @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. Apr 1, 2021 at 0:50
  • Agree, 'grep' is a secondary filter .... would be nice indicate the explicit difference between list and search ... Apr 1, 2021 at 16:39
  • 1
    @ManuelJordan Added :-) Apr 2, 2021 at 15:44
9

I am writing a new answer as Tasksel 'Task names' DO NOT show up in dpkg or apt queries. Meaning the other answers are incomplete (or wrong).

"I can't remember If I have checked the 'virtual machine host' checkbox.."

Check if there is an asterisk by the name after re-running sudo tasksel (or an ' i ' if running tasksel --list-tasks).

If you do not see this, then you probably do not have the 'tasksel meta-package' installed.


"..How do I check if my server has the corresponding packages?"

If you want to find out what is installed under the Virtual Machine Host tasksel meta-package:

tasksel --list-tasks

(in your case tasksel --list-tasks | grep virt is enough)

This gives the proper task name and an indication as to whether it is installed.

In your case this tasksel name is virt-host.


"What packages come with the virtual machine host?"

From the last command, find the tasksel task name. This is needed for the next command:

tasksel --task-packages virt-host

This lists the individual Tasksel meta-package (denoted by appended '^') that make up the name. In your case it's virt-host^.

Once you find proper meta-name you can use apt:

 apt show virt-host^ | grep "Package:"

(or apt-cache show ... if apt show doesn't work.)

This will show all packages making up the given meta package.

Then:

 dpkg -l {each individual pkg name listed above}

This will tell you if the individual package is installed/uninstalled (noted by 2nd letter: 'i' status means installed). Do this for all packages listed under the task/meta package.

This should answer the original questions.

Side note: /usr/share/tasksel/descs/ubuntu-tasks.desc also holds descriptions of Tasksel meta-packages. If you: grep "virt" /usr/share/tasksel/descs/ubuntu-tasks.desc it will show the proper APT meta-package name. (In your case this is ubuntu-virt-server). You can also use apt commands with this meta-name.

If you are wanting to remove tasksel packages, this gets into the weeds a bit. Please consult this Ask Ubuntu answer or any number of other answers out there.

Please note that "meta" apt packages do not match Tasksel package names. Please consult the Tasksel Community Page for more info.

0
7

Use tasksel (sudo apt-get install tasksel if you don't already have it) and run tasksel --list-tasks.

tasksel will print out a list of the tasks, and there will be an i next to each task that is installed, and a u next to each task that is not installed.

If you wish to install the task, use tasksel install <task>. The name of the task you are looking for is virt-host.

The software selection items are called "tasks".

1
  • thanks for telling me the name of the task, that was one of my problems, wasn't sure which package to look for.
    – D.Zou
    Feb 20, 2014 at 22:11
3

I created a little script with which you can automatically install from a list of packages or applications or simply check if they are already installed:

#!/bin/bash
apt -qq list YOUR_LIST_GOES_HERE | grep -v "installed" | awk -F/ '{print $1}' > /root/list.txt
packages=$(cat /root/list.txt)
grep -q '[^[:space:]]' < /root/list.txt
CHECK_LIST=$?
if [[ $CHECK_LIST -eq 1 ]]; then
echo "Your packages are already installed"
else
echo "Installing packages"
apt-get  install -y $packages
fi

Hope it helps someone.

1
  • apt shouldn’t be used in scripts, the command always says.
    – Smar
    Apr 23, 2020 at 6:05
3

Check if some packages are installed and install missing ones otherwise

pkgs='hello certbot'
install=false
for pkg in $pkgs; do
  status="$(dpkg-query -W --showformat='${db:Status-Status}' "$pkg" 2>&1)"
  if [ ! $? = 0 ] || [ ! "$status" = installed ]; then
    install=true
    break
  fi
done
if "$install"; then
  sudo apt install $pkgs
fi

More info at: https://stackoverflow.com/questions/1298066/check-if-an-apt-get-package-is-installed-and-then-install-it-if-its-not-on-linu/54239534#54239534

Similar technique previously mentioned at: https://askubuntu.com/a/1102572/52975

2
  • careful as this will not work if the package was previously installed then removed.
    – js.mouret
    Jan 21 at 5:40
  • @js.mouret yes, updated. Jan 21 at 8:01
1

Another (not necessarily better) way: see whether the field APT-Manual-Installed appears in apt show <pkgname>. If it is absent then the package isn't installed. Unfortunately it's not shown in apt-cache show <pkgname>.

As always, the apt is for interactive use and not recommended for scripting.

0

For example, if I know or I want to confirm that mysql is installed use:

  • apt list --installed | grep mysql

the --installed part is mandatory to confirm that is installed

Knowing that I don't have postgresql installed, so:

  • apt list --installed | grep postgresql

returns empty/nothing

If I execute without --installed

  • apt list | grep postgresql

it returns everything about postgresql package names and repository available

In global try apt list --installed vs apt list the latter is bigger than the former about the results returned, the difference is about of course what is installed or not, and what is available

1
  • While this is fine for informative version its return value isn't indicative
    – Treviño
    Apr 20, 2021 at 20:00
0

The command (checking that python3-pip is installed):

dpkg --get-selections python3-pip

and the output (yes, is installed):

python3-pip                                     install

and with a missing package:

dpkg --get-selections some-missing-package
dpkg: no packages found matching some-missing-package
-1

In my scripts I simply use the dpkg -s <pkgname> | grep installed command then I test the exit code $?: if 0 means the package is installed otherwise if 1 the package isn't installed.

E.g.

> dpkg -s nano | grep installed
> echo $? # $? is 0, package "nano" is installed

> dpkg -s foobar | grep installed
> echo $? # $? is 1, package "foobar" is not installed
1
  • This will be wrong for packages that have the string installed anywhere in their description. For example try running dpkg -s debsums | grep installed.
    – josch
    Feb 15 at 12:54
-1

update:

Better than parsing dpkg output, is to use dpkg-query output. The command below prints installed by itself if the package is installed:

dpkg-query --show --showformat='${db:Status-Status}\n' package-name

Again to suppress errors:

dpkg-query --show --showformat='${db:Status-Status}\n' package-name 2>/dev/null

original:

Based on feedback from josch in order for a package to be fully installed it should appear with a ii next to it. So a command to find out would be:

# if it prints the package, it is installed
dpkg -l package-name | grep -E '^ii\s'

Now in case it is not installed at all, dpkg can print an error, in case it was never installed in the past.

dpkg-query: no packages found matching

We can filter appropriately to only get the information that we need.

The following prints installed if the package is installed, nothing otherwise.

( ( dpkg -l package-name 2>&1 ) | grep -E '^ii' > /dev/null ) && echo installed
4
  • "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.
    – josch
    Feb 16 at 10:48
  • @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.
    – Marinos An
    Feb 17 at 10:32
  • 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?
    – josch
    Feb 17 at 13:29
  • @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.
    – Marinos An
    Feb 17 at 16:45

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.