3

I know that setting install_weak_deps=False in /etc/dnf/dnf.conf will allow you to not install the weak dependencies package when using the dnf installer later.

However, how can I list all the packages installed before setting this option?

I want to remove all the packages installed as weak dependencies in the whole system.

2 Answers 2

3

Weak dependencies may only be used in package A's specfile if package A still functions without the dependency B present. That means that package A merely "suggests" that package B be installed.

There is no way of determining reliably if package B was a weak dependency (and pulled by A in because of that) after B has already been installed. Unless you have access to full yum/dnf logfiles and are willing to plough through them!

This is because B may have been installed manually, or as a weak dependency of package C, or hard dependency ("require") of package D.

1

You can’t get an exact list.

However, you can get a reasonable approximation by elimination with some relatively simple scripting.

dnf repoquery --userinstalled will list all packages that were manually installed from DNF’s perspective (this is not the same as actually manually installed for a couple of reasons, but it’s not especially relevant for our purposes).

dnf repoquery --installed lists all installed packages.

dnf repoquery --requires pkg lists hard dependencies for pkg.

dnf repoquery --whatprovides pkg can translate from the output of the above --requires command to a specific package.

Using these four commands, you can generate an approximate list of all packages that are not manually installed and not explicitly depended on by using the following shell script:

#!/bin/bash

# Get the list of all installed packages
installed="$(dnf repoquery --installed)"

# Generate a list of all hard dependencies by iterating over each dependency of each installed package.
required=""
for pkg in ${installed}; do
    for dep in $(dnf repoquery --requires "${pkg}"); do
        required="${required}\n$(dnf repoquery --whatprovides "${dep}")"
    done
    # Remove any duplicates from the list of hard dependencies (done each time we finish a package’s dependencies to minimize memory usage).
    required="$(echo "${required}" | sort -u)"
done

# Iterate over each manually installed and explicitly required package, removing them from the list of all installed packages
notrequired="${installed}"
for pkg in $(dnf repoquery --userinstalled) ${required}; do
    notrequired="$(echo "${notrequired}" | grep -v "${pkg}")"
done

# Print the final list of packages that are not required.
echo "${notrequired}"

This will, unfortunately, take a long time to run (on the order of multiple minutes even on a clean install), and it may use quite a bit of memory. The list of packages it eventually prints out are everything that DNF does not think you installed yourself which is also not a hard dependency of any installed package. This is not exactly the same as a list of packages installed as soft dependencies because:

  • Something may have originally been pulled in as a soft dependency, but was either manually installed later, or is a hard dependency of a package that was installed later.
  • You can actually change whether DNF thinks a given package was installed manually or automatically.
  • This may result in packages that were pulled in by installing a group being listed as well (I’m not sure because I just don’t use groups).
1
  • A nice answer and a fair approximation of what the OP was looking for. Stressing the fact that it is indeed an approximation and not an exact list :-)
    – Edward
    Commented Nov 27, 2022 at 12:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .