As a server administrator, understanding how to list installed packages in Ubuntu is crucial. This is useful for various tasks such as tracking software inventory, upgrading, and migrating a hosting environment.
There are several commands for managing Linux server packages. In addition to having different purposes, these commands vary depending on the version of your operating system and your package manager.
In this article, we will explain how to use commands to view installed packages in Ubuntu. We will explore different methods and command variations for different tasks.
How to list installed packages in Ubuntu
Let's go over the common methods to view installed packages on an Ubuntu 20.04 system . Before continuing, make sure your virtual private server (VPS) is running the same version or the commands will not work.
How to view installed packages in Ubuntu using apt
Ubuntu version 14.04 and later comes with the apt package management system. In these versions, you can use the apt command-line utility to list the software packages installed on your system.
Use Terminal to run Linux commands on a local system. For a remote machine like a VPS, you must connect via SSH using PuTTY or Terminal.
Users of Hostinger VPS hosting can obtain access credentials in the SSH access tab of the VPS overview.
Alternatively, Hostinger users can take advantage of the Browser terminal to view packages from the command line directly through their web browser. Once connected, run the following commands to list packages with apt .
Use the apt list command to display all available Ubuntu 20.04 packages :
sudo apt list
To list only installed packages, run the apt list command with the -installed option :
sudo apt list --installed
Add the less argument to display a smaller output. To do this, add a square ( | ) and less at the end of your command:
sudo apt list --installed | less
Although less compresses your output, it will still list all installed packages. To check if a package is installed, use the grep command with the program name:
sudo apt list --installed | grep packagename
To view more information about a specific package installed on your system, use the apt show command :
sudo apt show packagename
Remember to replace the packagename placeholder with the specific name of the package. For example, run these commands to list Vim-related packages :
sudo apt list --installed | grep vim
To display detailed information about a specific Vim package , run the following:
sudo apt show vim
How to view installed packages in Ubuntu using dpkg-query
For older versions of Ubuntu without the apt package manager, use the dpkg-query command. The use of dpkg-query is similar to that of apt , but it does not work with a remote repository.
To list only the installed packages with their versions and a brief description, run the following command:
sudo dpkg -l
Use less with the dpkg command to restrict the output as shown below:
sudo dpkg -l | less
Add grep to search for specific packages. This is what the command looks like:
sudo dpkg -l | grep packageName
Remember to replace packageName with the actual package. For example, enter the following to display installed PHP-related packages:
sudo dpkg -l | grep PHP
Additionally, you can query information about a specific package using dpkg-query . This is the command:
sudo dpkg-query -W packageName
How to create a list of installed packages in Ubuntu
In addition to listing installed software, you may need to save the results for archiving or replicating system settings. Ubuntu allows you to use the redirect exit symbol (>) to store the names of installed packages in a file.
To do this, use dpkg-query to request information from the dpkg package manager about installed applications. Then add -f '${binary:Package}\n' -W to specify the output format.
End the command with the > symbol to inform you where to store the result, that is, the completePackage.txt file . This is what the complete command looks like:
sudo dpkg-query -f '${binary:Package}\n' -W > completePackage.txt
Alternatively, use the -get-selections option to retrieve packages based on their installation status. This is the command:
sudo dpkg --get-selections > completePackage.txt
Creating package lists is also useful for replicating applications installed on other machines. To do this, move completePackage.txt to the new system and run this command:
sudo xargs -a completePackage.txt apt install
The xargs command reads the list from the completePackage.txt file . The Linux software installation command then adds the same packages to the new system.
You can also use the apt command to replicate packages on Linux. To list the installed packages in a file, use the following command:
sudo apt list --installed | awk -F/ -v ORS=" " 'NR>1 {print $1}' > completePackage.txt
After adding the file to another server, install the same packages using the apt-get commands :
sudo apt-get install < completePackage.txt
How to count installed packages in Ubuntu
You can also count the number of installed Linux packages. The syntax is similar to outputting the list to a file, but with a wc or word count command after the pleca.
This command will check the installed packages based on the specified options. In this case, we use the -l option to count the number of lines in your list of installed packages:
sudo dpkg-query -f '${binary:Package}\n' -W | wc -l
This is what the output will look like:
To count installed packages, you can also use the wc command with apt :
sudo apt list –-installed | wc -l
How to make a list of updatable packages
Add the –upgradeable option to your apt command to check for updates available in the repository. Before continuing, sync the repository using this command:
sudo apt update
Next, run the following command to detect updatable packages:
sudo apt list --upgradeable
The terminal output will be similar to this:
How to list all package versions
It is possible to use Ubuntu 's apt commands to check the version of packages. To do this, run the following:
sudo apt list --all-versions
To check the installed version of a specific package, add its name to the end of the command. Below is an example of the command to consult PHP:
Users can also list the LOG files within the /var/log/apt directory path to check which packages have been removed, updated, or deleted. To do this, use the less command:
sudo less /var/log/apt/history.log
How to check Snap and Flatpak packages
The apt and dpkg-query commands can only list an installed package from your database. To check other packages, such as Linux Snap and Flatpak , modify the command accordingly.
For example, use the snap command to list the Linux Snap packages installed on your Ubuntu system:
snap list
Similarly, use the following command to check the installed flatpak applications :
flatpak list
Conclusion
Linux users should know how to use the command line to list installed packages before upgrading and migrating their machines. To do this on an Ubuntu system, use the apt list and dpkg-query commands through the Terminal or an SSH client.
Add the standard operator to your command to save the list of packages to a file on your system. You can also add the wc or wordcount command to count installed packages.
Additionally, use apt list -upgradable to check for available package updates. If you want to list all package versions, replace the option with -all-versions .
Remember to replace apt and dpkg if you use another package manager like Flatpak or Snap .
How to view installed packages in Ubuntu – Frequently Asked Questions
In this section, we will answer common questions about how to list installed packages on an Ubuntu server.
Why list installed packages in Ubuntu?
Users often refer to installed packages if they want to migrate or replicate the environment from one system to another machine.
Additionally, it helps to know which packages users should install after formatting their Linux system.
Can I use these methods on other Linux distributions besides Ubuntu?
It depends on the distributions . For Debian-based distributions, such as Kali Linux, these methods should work.
However, for other distributions like CentOS and ArchLinux, you will need other commands depending on your Linux package management system.