Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: September 2, 2024
Unlike other Linux distributions, Arch Linux doesn’t have preinstalled packages. Instead, the user explicitly installs each package they need.
While this provides less bloat and more customization, it also means that it is difficult to determine which packages were installed as part of the base system, and which ones were installed later by the user.
In this tutorial, we learn how to list user-installed packages in Arch.
Arch’s default package manager is pacman. Let’s begin by using pacman to get a list of explicitly installed packages on our system:
$ pacman -Qqe
acpi
...
Running pacman with the -Q flag will query the local database. We use -e to output only packages explicitly installed and -q to output only package names.
Importantly, base system packages show up in this output, since they are explicitly installed during Arch installation.
Therefore, getting only the user-installed packages requires a few more steps.
Next, we must get all the dependencies of the packages from our base system. Luckily, pactree is a tool that is made just for this purpose.
The pacman-contrib package provides pactree. Let’s install this package using pacman:
# pacman -S pacman-contrib
Beyond pactree, the pacman-contrib package also provides tools such as pacsearch, paccache, and pacsort.
The pactree tool lists the recursive dependencies of one or more packages. Let’s see how this works by running pactree with the base package:
$ pactree base
base
├─filesystem
...
The output of pactree is a tree structure with each dependency as a separate branch. However, this isn’t an easily machine-readable format.
Furthermore, dependencies are output multiple times when shared by more than one package.
Luckily, pactree can also return a more machine-friendly output:
$ pactree -u base
base
filesystem
...
We use the -u flag with pactree, to output a recursive list of unique dependencies for the base package.
Note, pactree searches the local database by default, but using -s/–sync flag will search the sync (external) database.
The last step is to filter out all packages of our base system, leaving just a list of user-installed packages.
The basic packages for an Arch install typically include base, linux, and linux-firmware. Filtering these out from our system packages, results in a list of user-installed packages.
Now, let’s create a basic script for outputting user-installed packages:
#!/bin/bash
# get_user_installed.sh
comm -23 <(pacman -Qeq | sort -u) <(xargs -n 1 pactree -u <<< "${@}" | sort -u)
First, we output packages explicitly installed on our system. Next, we output dependencies of a list of packages passed as script arguments.
Finally, we redirect these outputs as files to comm, using the -23 flag to list only lines unique to the first output.
Let’s break this down a little further:
Since pactree takes only one package as an argument, we use xargs to pass each script argument to a new pactree call.
Importantly, comm only compares sorted files. This is why we pipe to sort in our script before redirecting to comm.
To run our script, we first need to make it executable:
$ chmod +x get_user_installed.sh
We use chmod with +x to enable execute permission to the file get_user_installed.sh.
Now, let’s run our script:
$ ./get_user_installed.sh "base" "linux" "linux-firmware"
acpi
...
We call our script using ./ and the path to the script file. The script arguments are the packages of our base system.
Additionally, we can pass optional system packages to our script (for example, base-devel, grub, util-linux, amd-ucode, and intel-ucode).
In this article, we learned how to list user-installed packages in Arch. First, we output explicitly installed packages using pacman. Next, learning how to use pactree to list a package’s recursive dependencies.
Finally, we created a script that outputs user-installed packages by filtering out base system packages from explicitly installed packages.