How to check if a package is installed in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a specific software package is installed on your Linux system using various command-line tools. You will explore the apt list --installed command to get a comprehensive list of installed packages and how to filter this list using grep.

Furthermore, you will discover how to verify package installation and retrieve detailed information about installed packages using the dpkg -l and apt policy commands, providing you with essential skills for managing software on Debian-based Linux distributions.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/PackagesandSoftwaresGroup -.-> linux/apt("Package Handling") subgraph Lab Skills linux/grep -.-> lab-558678{{"How to check if a package is installed in Linux"}} linux/apt -.-> lab-558678{{"How to check if a package is installed in Linux"}} end

Check package status with apt list --installed

In this step, you'll learn how to check which software packages are installed on your system using the apt list --installed command.

Linux systems, especially Debian-based ones like Ubuntu (which you are using in this lab), manage software using package managers. apt is a powerful command-line tool for this purpose.

The apt list command is used to list packages based on criteria. Adding the --installed option filters the list to show only packages that are currently installed on your system.

Open your terminal if it's not already open. Remember, you can find the Xfce Terminal icon on the left side of your desktop.

Type the following command and press Enter:

apt list --installed

You will see a long list of packages. This list includes the package name, version, and architecture.

Listing...
accountsservice/jammy-updates,now 0.6.55-0ubuntu12~22.04.5 amd64 [installed]
acl/jammy,now 2.3.1-1 amd64 [installed]
adduser/jammy,jammy,now 3.118ubuntu8 all [installed]
...
zlib1g/jammy-updates,now 1:1.2.11.dfsg-2ubuntu1.6 amd64 [installed]
zsh/jammy-updates,now 5.8.1-1ubuntu1.4 amd64 [installed]

This command is useful for getting a comprehensive overview of the software installed on your system. Because the list can be very long, you might want to scroll up to see the beginning of the output.

To make the output more manageable, you can combine apt list --installed with other commands using a pipe (|). For example, to search for a specific package like htop (which you installed in the previous lab), you can use grep:

apt list --installed | grep htop

This command takes the output of apt list --installed and "pipes" it as input to the grep command, which searches for lines containing "htop".

You should see output similar to this, confirming that htop is installed:

htop/jammy,now 3.0.5-1build2 amd64 [installed]

Using grep with apt list --installed is a quick way to check if a specific package is present on your system.

Click Continue to proceed to the next step.

Verify package installation with dpkg -l

In this step, you'll use the dpkg command to verify the installation status of packages. While apt is a higher-level tool for managing packages (installing, removing, updating), dpkg is the underlying tool that handles the actual installation and removal of .deb package files.

The dpkg -l command lists packages in a similar way to apt list --installed, but it provides slightly different information and is often used for more detailed status checks.

Open your terminal. Type the following command and press Enter:

dpkg -l

Again, you will see a long list of packages. The output format is a bit different from apt list --installed.

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  accountsservice                     0.6.55-0ubuntu12~22.04.5    amd64        query and manipulate user account information
ii  acl                                 2.3.1-1                     amd64        Access control list utilities
ii  adduser                             3.118ubuntu8                all          add and remove users and groups
...
ii  zlib1g:amd64                        1:1.2.11.dfsg-2ubuntu1.6    amd64        compression library - runtime
ii  zsh                                 5.8.1-1ubuntu1.4            amd64        shell with lots of features

Let's break down the first few columns:

  • Desired: What the user wants to do with the package (e.g., i for install, r for remove).
  • Status: The current status of the package (e.g., i for installed, c for configuration files only, p for purged).
  • Err?: Indicates if there's an error with the package.

The ii at the beginning of most lines means "Install Ok, Installed". This indicates the package is installed correctly.

Similar to apt list, you can use grep to find a specific package. Let's check for htop again:

dpkg -l | grep htop

You should see a line like this, confirming htop's status:

ii  htop                                3.0.5-1build2               amd64        interactive processes viewer

The ii at the start of the line for htop confirms it is installed correctly.

While apt list --installed is often quicker for a simple list, dpkg -l provides more detailed status flags which can be helpful for troubleshooting installation issues.

Click Continue to move on.

Inspect package details using apt policy

In this step, you'll use the apt policy command to get detailed information about a package, including its installed version and available versions from different repositories. This command is particularly useful for understanding where a package came from and which version would be installed if you were to upgrade or reinstall it.

Open your terminal. Let's use apt policy to inspect the htop package. Type the following command and press Enter:

apt policy htop

You will see output similar to this:

htop:
  Installed: 3.0.5-1build2
  Candidate: 3.0.5-1build2
  Version table:
 *** 3.0.5-1build2 500
        500 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages
        100 /var/lib/dpkg/status

Let's break down the output:

  • Installed: This shows the version of htop that is currently installed on your system.
  • Candidate: This shows the version that would be installed if you were to run sudo apt install htop or sudo apt upgrade htop. In this case, the installed version is also the candidate version.
  • Version table: This lists the available versions of the package from the configured software repositories.
    • The line starting with *** indicates the installed version and its priority.
    • The line starting with 500 shows the version available from the Ubuntu jammy/universe repository and its priority (500).
    • The line starting with 100 refers to the installed version from the dpkg status file.

The numbers (like 500 and 100) represent the priority of the package source. Higher priority sources are preferred when installing or upgrading.

apt policy is a powerful command for debugging package issues, understanding why a specific version of a package was installed, or seeing which versions are available from your configured sources.

You can also run apt policy without a package name to see the policy for all packages, which includes information about your configured repositories and their priorities. However, that output is very long.

You have now learned three different ways to check the status and details of installed packages: apt list --installed, dpkg -l, and apt policy. These commands are essential tools for managing software on a Debian-based Linux system.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check if a package is installed on a Linux system using three different command-line tools. You first used apt list --installed to get a comprehensive list of all installed packages and learned how to filter this output using grep to find a specific package.

You then explored dpkg -l, another command-line tool for managing Debian packages, to verify the installation status of a package and view its details. Finally, you used apt policy to inspect the installation details of a package, including its version and origin, providing a deeper understanding of how packages are managed on your system.