How to List Installed Packages on Ubuntu 20.04
With Ubuntu, we can install several add-on packages to our VPS based on our needs. Packages such as Apache, PHP, and MySQL do not come pre-loaded with the Linux operating system. These additional features, however, can be easily installed using the respective packages. You may also choose to have a specific version installed on your Linux OS.
There may be certain instances where you will want to replicate the environment from one machine to another. In such cases, you will need to list installed packages and their versions.
Similarly, you may want to reinstall the Linux operating system on your machine, which will require you to install the packages again.
If you are planning to install, uninstall, or reinstall packages, the first step should always be to first list the installed packages. This lets you verify the installed version of the package and cross verify in case a specific package needs to be updated.
Listing the installed packages on Ubuntu can be done in several ways. This article will show you a number of ways to do it using your terminal.
Using APT to List Installed Packages on Ubuntu
Ubuntu uses apt which is a command-line tool to help package management. apt was introduced in Ubuntu 14.04. Following this version, the apt command can be used to list the installed packages.
The command to list installed packages will be:
sudo apt list –-installed
This will list all installed packages and their versions. The complete installed packages list will most likely be very long, exceeding your scrollable screen.
You can restrict the list installed output by using less. This will show a smaller output. The Linux command for this will be separated by a pipe (|). An example of this is as shown below:
sudo apt list –-installed | less
While less does compress your output, it will still provide a list of the installed packages. To search for any specific package, you can use grep along with the package listing.
For instance, if you want to list all the PHP installed packages, use the following command:
sudo apt list –-installed | grep PHP
Then, to view more information about a specific package, use the following command:
sudo apt packageName
Using dpkg-query to List Installed Packages on Ubuntu
In case you have an older version of Linux Ubuntu installed and can’t use the apt tool, use the dpkg-query command. This also lists the installed packages. The basic dpkg command would be:
sudo dpkg -l
This output will similarly provide package names and their versions with a brief description of the package.
You can use less with this command to show a smaller output. The dpkg command for this would be:
sudo dpkg -l | less
With grep included in this command, you can search for specific packages. The command would look like this in the command line:
sudo dpkg -l | grep packageName
For example, to list installed PHP packages, simply type:
sudo dpkg -l | grep PHP
Create a List of Installed Packages on Ubuntu
There may be a need to create a complete list of installed packages and have that package list saved in a file. The command below saves a complete output to a file:
sudo dpkg-query -f '${binary:Package}\n' -W > completePackage.txt
Alternatively, you can also try another command:
sudo dpkg --get-selections > completePackage.txt
While this helps you track the packages installed on your machine, it can also be used to replicate the installation on other machines. You can use this to install the same packages to a new server. To do that, use the following command after placing completePackage.txt on the new machine:
sudo xargs -a completePackage.txt apt install
This can also be done using apt. To get all the installed packages in a file you can use the command shown below:
sudo apt list --installed | awk -F/ -v ORS=" " 'NR>1 {print $1}' > completePackage.txt
Once we have this in a file, we can install everything on another server using the command:
sudo apt-get install < completePackage.txt
Count the Installed Packages on Ubuntu
You may need to count the number of installed Linux packages. This can be done similarly to the command above. Not by redirecting the output to a file, but by using wc to get the count.
An example of such command is as shown below:
sudo dpkg-query -f '${binary:Package}\n' -W | wc -l
The output will be the total number of installed packages. Similarly, you can use the command below with apt to get the total number of installed packages.
sudo apt list –-installed | wc -l
How to List Upgradeable Packages
Using apt, you can also check newer package versions that are available for installation. This can be done using the command below:
sudo apt list –upgradeable
List All Versions
In case you have multiple versions installed, you can list all the versions installed using apt. This can be done using the below command:
sudo apt list --all-versions
Within /var/log/apt folder, you can list the history.log file to check information about what package has been removed, updated, and deleted, and at what time and which day.
Snap and Flatpak Packages
Since apt and dpkg commands will not be able to list Linux Snap and Linux Flatpak packages, we will need to use slightly different commands.
To list the Linux Snap packages installed, use the following command:
snap list
Similarly, to list the Linux Flatpak packages installed, use the following command:
flatpak list
Wrapping Up
You now know some basic Linux commands to see what packages are installed on your Linux Ubuntu 20.04 machine. You can list all installed packages by using different commands. Give it a try and let us know how it goes in the comments below.