1. Introduction

In software administration, it’s crucial to ensure that all necessary packages are correctly installed to maintain a stable and functional system. In general, managing packages is an important operating system component.

In this tutorial, we’ll discuss how to check whether a package is installed through a package manager.

First, we’ll discuss the dpkg and apt commands and their uses in Linux-based systems. Next, we’ll explore dpkg and its flags to check the installation status of a package, and whether it’s present in our system. Lastly, we’ll look at the apt command with different options to verify the status of a package.

2. Overview of the APT Package Manager

The apt command, or Advanced Packaging Tool, is a package management tool used in Debian-based Linux distributions like Ubuntu. It simplifies the installation, updating, upgrading, and removal of software packages. Hence, apt streamlines software management tasks, enhancing the efficiency of package handling on Linux systems.

The dpkg command is a low-level package management tool in Debian-based Linux distributions. In addition, it provides direct handling of individual Debian packages. It’s for the installation, removal, and purging of packages.

However, despite its capabilities, dpkg doesn’t handle dependencies automatically. Thus, we often use it in conjunction with higher-level tools like apt for more comprehensive package management.

3. Using the dpkg Command

In this section, we’ll discuss the approaches using the dpkg command to check package installation status.

3.1. Using dpkg -query

To begin with, we can use the dpkg-query to check the installation status of a package and whether it’s present in our system.

Let’s use the cat command to take a look at the sample file we’ll work on:

$ cat dpkg_01.sh
if dpkg-query -W -f='${Status}' $package 2>/dev/null | grep -q "install ok installed"; then
    echo "Package exits!"
    echo "Package is not installed."

Then, we make the script executable via the chmodhttps://www.baeldung.com/linux/chown-chmod-permissions#chmod command and run it:

$ chmod +x dpkg_01.sh
$ ./dpkg_01.sh curl
install ok installed
Package exists!

In the above script, we verify whether the curl package is installed on a system using the Debian package management system (dpkg).

Let’s break down each part of the if condition to understand the script:

  • dpkg-query -W -f=’${Status}’ curl queries information about the package named curl and specifies the format for the installation status
  • ${Status} is the placeholder for the actual status of the curl package as we can see it via dpkg-query
  • 2>/dev/null redirects error messages to /dev/null to ignore them
  • grep -q “install ok installed” searches for the string install ok installed in the previous command’s output

Notably, the inclusion of -q ensures that grep operates silently and terminates promptly after encountering the initial match.

Following the execution of the if condition, assuming it’s satisfied, the echo command displays Package exists! Otherwise, it proceeds to the else statement, and outputs Package is not installed.

3.2. Checking the dpkg Status File

In the same way, we’ll use the dpkg command with the -s flag to verify the installation status of the package.

Let’s suppose we want to check the status of the Docker package. For this, we’ll write a script using dpkg -s:

$ cat dpkg_status.sh
if [ -n "$(dpkg -s $package 2>/dev/null | grep 'Status: install ok installed')" ]; then
    echo "Status installed."
    echo "Status not installed."

Now, let’s make the script executable and check its output:

$ chmod +x dpkg_status.sh
$ ./dpkg_status.sh docker
Status installed.

To begin with, we can see a preview of the output piped to the grep command:

Package: adduser Status: install ok installed
Priority: important
Section: admin
Installed-Size: 608
Maintainer: Ubuntu Developers

We use the above script to check whether the Docker package exists on a system by using dpkg -s. To elaborate, the -s flag provides a detailed status report for the specified package.

Next, we use >/dev/null 2>&1 to redirect both the standard output (stdout) and standard error (stderr) to /dev/null. This silences the command and prevents it from producing visible output.

After that, we pipe the output to grep to search for the specific string in the output obtained from the dpkg command. Moreover, the command substitution replaces the entire command with its output.

Lastly, the code verifies whether the condition is true, and prints the output accordingly.

4. Using the apt Command

Now, we’ll explore methods for checking package installations using the apt command.

4.1. Using apt list

In this method, we’ll use the apt list command to check if the package is installed on a Debian-based Linux system.

For instance, to check the status of the curl package, we pair apt list with grep:

$ cat aptList.sh
if apt list --installed | grep -q "^$pattern/"; then
    echo "Package exits!"
    echo "Package is not installed."

Let’s make the script executable and run it:

$ chmod +x aptList.sh
$ ./aptList.sh curl
Package exists!

The above script uses the apt list command within the if condition to check the installation of a package. Next, we pipe the output of the first command to the second command as an input.

The grep command searches for lines that start with curl/ in the output of apt list. Lastly, if the condition is true, the echo command prints the statement Package exists!

To summarize, the script utilizes apt list –installed to list installed packages, uses grep to check for a line starting with curl/, and prints the respective output.

4.2. Using apt-mark

Alternatively, we can use the apt-mark command to check package status.

We use the apt-mark command in Linux distributions to manipulate package installation states. Moreover, it enables us to mark packages as manually or automatically installed.

Furthermore, we can use this command to check the installation state and clear installation marks. This way, package managers may be aware of packages that weren’t installed through them.

Now, let’s check the aptMark.sh script:

$ cat aptMark.sh
if apt-mark showmanual | grep -q "^$package"; then
     echo " The Package exists!"
     echo "The package isn't installed."

Next, we make the script executable and run it:

$ chmod +x aptMark.sh
$ ./aptMark.sh curl
The Package exists!

In this Bash script, we again create a pipeline using the grep command.

The first part consists of an if condition that lists packages that were manually installed using apt or related package management tools. For this step, we use the apt-mark showmanual command. After that, we direct the result of the initial command as an input to the subsequent command.

Subsequently, the second part uses the grep command to look for lines exclusively containing curl in the output. Lastly, the echo command prints the statement according to the result of the given condition.

5. Conclusion

In this article, we gained insight into checking a package installation through a package manager to understand where a package came from.

Initially, we explored dpkg, along with its -s flag to assess a package’s installation status and verify its presence on our system. We also explored the apt command, leveraging different options to confirm the package’s status.

Comments are closed on this article!