I just switched from Ubuntu to Fedora14. In Ubuntu we had the apt-get autoremove
thing to remove orphan/unused packages. What is the counterpart in Fedora?
Fedora – How to remove Orphan Packages in Fedora
dnffedorayum
Related Solutions
It's not easy. How do you differentiate between "a file that was required by something I have since removed" from "a file that is not required by anything else that I really want"?
You can use the package-cleanup
command from the yum-utils
package to list "leaf nodes" in your package dependency graph. These are packages that can be removed without affecting anything else:
$ package-cleanup --leaves
This will produce a list of "libraries" on which nothing else depends. In most cases you can safely remove these packages. If you add --all
to the command line:
$ package-cleanup --leaves --all
You'll get packages that aren't considered libraries, also, but this list is going to be so long that it probably won't be useful.
Since Fedora 26, the Dnf repoquery
subcommand supports has a new option for listing all user-installed packages:
$ dnf repoquery --qf '%{name}' --userinstalled \
| grep -v -- '-debuginfo$' \
| grep -v '^\(kernel-modules\|kernel\|kernel-core\|kernel-devel\)$' > pkgs_a.lst
In contrast to other methods, it also lists all debuginfo packages. The additional grep in the above example filters them out.
To install the list on host B:
$ < pkgs_a.lst xargs dnf -y install
Dnf API
With recent Dnf versions (e.g. Fedora >= 23), the package database can be queried for user installed package names via the Dnf Python API:
$ python3 -c 'import dnf; b = dnf.Base(); b.fill_sack(); \
l = sorted(set(x.name for x in b.iter_userinstalled() \
if not x.name.endswith("-debuginfo") \
and x.name not in \
["kernel-modules", "kernel", "kernel-core", "kernel-devel"] )); \
print("\n".join(l)) ' > pkgs_a.lst
# dnf install $(cat pkgs_a.lst) # on host_b
By default, dnf install
aborts if one or more packages aren't available anymore. Alternatively, dnf can be forced to install all remaining ones:
# dnf install --setopt=strict=0 $(cat pkgs_a.lst) # on host_b
PS: Put the above code and more into user-installed.py
that also supports other distributions.
history userinstalled
On Fedora 23 and later, Dnf provides the
# dnf history userinstalled
command that lists all user installed packages. As of 2016-11, its usefulness is limited because there is no way to control its output and it prints packages fully qualified (i.e. including version information).
userinstalled Limitations
Note that the marking of packages as user-installed has some limitations on some Fedora versions, for Fedora 23-ish era systems (from around 2015-11) the following issues are relevant):
- packages installed via the GUI are not included
- packages installed via the command-not-found handler are not included
- some packages installed by default (by anaconda) are included
Repoquery
On older Fedora systems, where Dnf, the Dnf API and dnf history userinstalled
aren't available, one can use repoquery instead, e.g.:
$ repoquery --installed \
--qf '%{n} | %{yumdb_info.reason} | %{yumdb_info.installed_by}' --all \
| awk -F'|' ' $2 ~ /user/ && ($3 != 4294967295) { print $1 }' \
| sort -u > pkgs_a.lst
The second awk condition is used to exclude packages that were installed by the installer. The installer's user-id was apparently stored as 4294967295 - alternatively you can write something like ($3 == 0 || $3 == your-user-id)
.
Note that this command works on Fedora up to release 21 - but e.g. not on release 23, because the command repoquery
was replaced with dnf repoquery
. And dnf repoquery
does not understand the %{yumdb_info.reason}
tag.
Best Answer
package-cleanup --quiet --leaves
from the yum-utils package will list the library packages which aren't relied upon by other packages. Unfortunately it tends to be a bit overeager. On my system, for example, it suggested removing libvirt. Adding the--exclude-bin
argument helps. If you're happy with it's suggestion, then to clean up the packages, do: