Linux Commands

How can I see DPKG and apt history

This tutorial explains how to see dpkg and apt history in Debian-based Linux distributions.

After reading this tutorial, you will know how to find information about installed, removed, and upgraded packages using commands apt, dpkg-query, and reading dpkg logs. There are instructions to create a list of installed packages, install all listed packages on a new computer, and additional apt and dpkg tips.

See installed packages using apt

The command below prints a list of all software installed through the apt/apt-get packages manager.

apt --installed list

Listing installed packages with dpkg-query

dpkg-query is a command to display information on packages from the dpkg database.

sudo dpkg-query -l

Count all installed packages

Using dpkg-query, you can get a total count of all installed packages. The -f flag will list all binary packages only, then the wc command counts all listed packages:

sudo dpkg-query -f '${binary:Package}\n' -W | wc -l

As you can see, there are 2802 installed packages. 

See installed packages reading logs

You can read recent apt activity (installation, removal, upgrade) by reading the log /var/log/apt/history.log as shown below:

cat /var/log/apt/history.log

To get information on packages installed using dpkg, you need to read dpkg logs located at /var/log. You can read the current log by running the command below:

grep " install " /var/log/dpkg.log

To check the previous log,  see the file /var/log/dpkg.log.1.

grep " install " /var/log/dpkg.log.1

Reading compressed logs is the same process but with zgrep instead of grep, as shown in the example below.

zgrep " install " /var/log/dpkg.log.10.gz

If you want to see all compressed logs at once, you can use a wildcard like in the following example:

zgrep " install " /var/log/dpkg.log.*.gz

List removed packages

You also can list removed packages by reading logs by replacing “ install” with “remove”, as shown in the image below.

grep "remove " /var/log/dpkg.log

As with the previous example, you can also use the zgrep command and a wildcard to read all compressed logs, as shown in the screenshot below.

zgrep "remove " /var/log/dpkg.log.*.gz

List upgraded packages

To show upgraded packages replace “install” or “remove “ with “upgrade”, as shown in the example below.

grep "upgrade " /var/log/dpkg.log

Use zgrep to read compressed logs.

zgrep "upgrade " /var/log/dpkg.log.10.gz

As with previous examples, you also can use a wildcard.

zgrep "upgrade " /var/log/dpkg.log.*.gz

Export list of installed packages to install on a new device

You can create a list of installed packages, then import it to a new device to install the same software.

To create a list of installed packages, you can do a similar procedure as the one used to count installed packages, as shown below. Instead of counting, the following command creates a list called installedpackages.

sudo dpkg-query -f '${binary:Package}\n' -W > installedpackages

On the computer you want to install the same packages run:

sudo xargs -a installedpackages apt install

In the screenshot below, the apt was executed on the same computer the list was created; thus, it detected packages are already installed.

Updating outdated packages

You can use the command apt to check for outdated packages to update by running the command below:

sudo apt --fix-missing update

Fixing dependencies or broken packages

To fix dependencies or broken packages, after running apt-get update, execute the command below.

sudo apt-get install -f

About apt and dpkg

  • APT (Advanced Package Tool) automates packages retrieval, configuration (including dependency resolution), and installation. It can be considered the main packages manager in some Debian-based Linux distributions (including Debian itself) or a front-end for the dpkg packages manager.
    The main advantage of APT over DPKG is its ability to resolve dependencies and track updates. Apt is also used for package removal, updates, and this tutorial to retrieve information.
  • DPKG is the Debian package manager used by APT. As said previously, while it is a lot better than compilating, it has disadvantages before APT. It handles individual packages and doesn’t resolve dependencies. Still, DPKG includes interesting tools like the dpkg-query mentioned in this tutorial. Other tools include dpkg-split, dpkg-statoverride, dpkg-trigger, dpkg-divert. Dpkg include the following development tools: dpkg-source, dpkg-gencontrol, dpkg-shlibdeps, dpkg-genchanges, dpkg-buildpackage, dpkg-dist and dpkg-parsechangelog.

For additional information on the apt/apt-get packages manager, visit the man page https://linux.die.net/man/8/apt-get.

For additional information on dpkg, visit the man page https://linux.die.net/man/1/dpkg.

Conclusion

As you can see, showing installed, removed and upgraded software is an easy task any Linux user level can execute with a few commands. Linux offers a variety of versatile commands and tools to manage and administrate packages in a custom way. For example, getting the total count of installed programs in Microsoft Windows may be harder or impossible without the help of an external tool.

Learning simple tasks like the explained in this tutorial may save you a lot of time, as proven in the section describing how to export a list of packages to install on a new device, especially useful when you need to create a similar serverand cloning isn’t an option.

Learning to administer and manage packages is key and basic knowledge any user must-have.

I hope this tutorial was useful. Keep following Linux Hint for more Linux tips and tutorials.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.