1. Introduction
The RPM Package Management system comprises low-level and high-level tools. While we might not be able to work without the former, the latter are non-critical and can vary. For instance, we can have yum, dnf, or even custom solutions. In particular, the yum (Yellowdog Update Modified) command is a high-level package manager, now often just a symbolic link to dnf (Dandified YUM), its successor. Still, as the main way to manage the software in a Linux environment, caution should be exercised when manipulating the package installation system.
In this tutorial, we discuss ways to safely reinstall yum and its successor – dnf. First, we go over the original yum uninstall, download, and install. After that, we cover the same procedure for Dandified YUM (dnf).
We tested the code in this tutorial on CentOS 7 with GNU Bash 4.2.46 and YUM (Yellowdog Updater Modified) 3.4.3, as well as CentOS Stream 9 with GNU Bash 5.1.8 and DNF (DaNdiFied YUM) 4.14.0. It should work in most POSIX-compliant environments unless otherwise specified.
2. Reinstall Original Yellowdog Update Modified YUM
The original yum package manager is now of limited use. In fact, only older versions of most distributions still use the original YUM:
- CentOS before and including version 7
- Fedora before and including version 21
- Oracle Linux before and including version 7
- RedHat Linux before and including version 7
Actually, it’s not recommended to use the original YUM on newer distributions due to its flaws:
- minimal API documentation
- very slow dependency resolutions
- excessive memory consumption
- stops on unresponsive repositories
- leaves unnecessary packages even if no package depends on them
- protects kernel packages
- overall poor performance
Still, we might need to install, remove, or reinstall the original yum when using an older Linux operating system (OS).
2.1. Verify Original YUM Installation
Since package managers usually ship with the distribution, let’s assume we already have the original yum installed:
$ yum list installed yum
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.uni-sofia.bg
* extras: centos.uni-sofia.bg
* updates: centos.uni-sofia.bg
Installed Packages
yum.noarch 3.4.3-168.el7.centos @anaconda
In this case, yum* itself, with its list option, shows us that we seem to have version 3.4.3-168 *installed. While this query returns a package and version, it doesn’t perform a check for the consistency of the installation.
However, we can verify a package via the low-level rpm command and its –verify or -V flag:
$ rpm --verify yum
$
No output should mean the package installation is as expected. Still, we could experience issues that necessitate a reinstall.
2.2. Acquire Original YUM Package
Before making changes to the current installation, it’s good to have the packages that we may later need handy.
As long as we don’t have critical problems, we can employ yum to find and download its own installation RPM.
First, we install the yum-utils package:
$ yum install yum-utils
After that, we leverage the yumdownloader utility to get the RPM file of yum:
$ yumdownloader yum
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.uni-sofia.bg
* extras: centos.uni-sofia.bg
* updates: centos.uni-sofia.bg
yum-3.4.3-168.el7.centos.noarch.rpm | 1.2 MB 00:00
On the other hand, we might not be able to access a current high-level package manager due to a corrupt installation. While we could check under the /var/cache/yum path for the yum package installation RPM, it most probably won’t be there since that cache is cleaned fairly often.
Because of this, it’s usually best to search for the correct package from the original distribution repository site. For example, the packages for CentOS 7 are at http://mirror.centos.org/centos/7/os/x86_64/Packages. So, we can employ curl and grep to query for the package:
$ curl --silent http://mirror.centos.org/centos/7/os/x86_64/Packages/ | grep 'yum-3.4.3'
[...]<a href="yum-3.4.3-168.el7.centos.noarch.rpm">yum-3.4.3-168.el7.centos.noarch.rpm</a>[...]
Because we know the package version and the naming format, we located the http://mirror.centos.org/centos/7/os/x86\_64/Packages/yum-3.4.3-168.el7.centos.noarch.rpm package directly. Unlike with DNF YUM, it’s relatively hard to get the wrong version of the original YUM since its development is halted and distribution repositories already have only one version – the latest. Still, caution is advised.
Next, let’s download and save it locally with its –remote-name (-O):
$ curl --silent http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-3.4.3-168.el7.centos.noarch.rpm --remote-name
Now, we have the original yum RPM package. We can proceed by removing our installation and performing a reinstall.
2.3. Uninstall Original YUM
Sometimes, we might want to remove even the package manager from a system. In fact, many reasons can lead to this:
- change package manager
- upgrade package manager, i.e., with dnf
- corruption of the package manager installation
- security
- creating static OS images
When it comes to the original yum, we usually only need to employ the lower-level RPM management:
$ rpm --erase yum
error: Failed dependencies:
yum >= 3.0 is needed by (installed) yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch
In this case, we use the –erase or -e option of rpm to remove the package. However, we get an error at the dependency check, which states yum-plugin-fastestmirror as a dependency of yum.
To get a full list of packages dependent on yum and yum-plugin-fastestmirror, we can use the –query or -q option with the –whatrequires flag of rpm:
$ rpm --query --whatrequires yum
yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch
$ rpm --query --whatrequires yum-plugin-fastestmirror
yum-3.4.3-168.el7.centos.noarch
Now, we know that these two packages are interdependent, so we can remove yum with the –nodeps flag to ignore any dependency conflicts:
$ which yum
/usr/bin/yum
$ rpm --erase --nodeps yum
$ which yum
/usr/bin/which: no yum in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
No output should mean we were successful. At this point, the yum command is no longer available.
2.4. Install or Reinstall Original YUM
After downloading the relevant version of the original YUM and uninstalling our current one, *we can once again employ rpm but this time to re[–install] the package* [–verbose]ly:
$ rpm --verbose --install yum-3.4.3-168.el7.centos.noarch.rpm
Preparing...
################################# [100%]
Updating / installing...
1:yum-3.4.3-168.el7.centos
################################# [100%]
Alternatively, we can perform an –upgrade, although we’re pointing to the same version we had before:
$ rpm --verbose --upgrade yum-3.4.3-168.el7.centos.noarch.rpm
At this point, we should again be able to use yum:
$ which yum
/usr/bin/yum
$ yum --version
3.4.3
[...]
Installed: yum-3.4.3-168.el7.centos.noarch at 2023-06-06 06:56
[...]
Notably, we don’t need to handle any dependencies, despite performing an offline RPM installation. That might not have been the case had we not downloaded the same version of yum we already removed.
Critically, any custom directories, configuration changes, and similar would have to be applied anew.
3. Reinstall DNF Dandified YUM
In contrast to the original YUM, dnf is usually the default on more recent Linux distributions:
- CentOS after and including version 8
- Fedora after and including version 22
- Oracle Linux after and including version 8
- RedHat Linux after and including version 8
Importantly, the yum command still exists on such systems but only as a link to /usr/bin/dnf:
$ ls -l $(which yum)
lrwxrwxrwx. 1 root root 5 Sep 22 2022 /usr/bin/yum -> dnf-3
Moreover, the DNF package manager has several features:
- well-documented API
- very fast libsolv dependency resolution
- much lower memory consumption
- continues even after encountering unresponsive repositories
- cleans up unnecessary packages that no other package depends on
- allows any kernel package modifications
Despite these advantages, we might want to remove or reinstall dnf. Let’s see how.
3.1. Verify DNF YUM Installation
Again, we assume dnf YUM is already present:
$ dnf list installed dnf
Installed Packages
dnf.noarch 4.14.0-1.el9 @anaconda
In this case, the list option of dnf claims dnf version 4.14.0-1 is installed.
Further, since it’s at the base of RPM distributions, we can again employ rpm to –verify (-V) the installation:
$ DNFPATH=$(which dnf)
$ mv $DNFPATH .
$ rpm --verify dnf
missing /usr/bin/dnf
$ mv dnf $DNFPATH
$ rpm --verify dnf
$
Apart from our forced one, there seem to be no issues in this environment. So, let’s get the installation RPM for our version.
3.2. Acquire DNF YUM Package
The dnf YUM package manager also has an RPM cache at /var/cache/dnf.
Since we probably won’t find the dnf package there, we turn to the original CentOS Stream 9 distribution repository at https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os/Packages/ :
$ curl --silent https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os/Packages/ | grep 'dnf-4.14.0-1'
[...]<a href=dnf-4.14.0-1.el9.noarch.rpm">dnf-4.14.0-1.el9.noarch.rpm</a>[...]
[...]<a href="python3-dnf-4.14.0-1.el9.noarch.rpm">python3-dnf-4.14.0-1.el9.noarch.rpm</a>[...]
In this case, we need the specific version of DNF YUM we had installed. Otherwise, we might need to download two other packages as well:
- python3-dnf-*
- dnf-data-*
Let’s download dnf-4.14.0-1.el9.noarch.rpm, since that’s the exact version we removed:
$ curl --silent https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os/Packages/dnf-4.14.0-1.el9.noarch.rpm --remote-name
At this point, we have the DNF YUM package. Time to install it.
3.3. Uninstall DNF YUM
To remove dnf YUM, we use rpm with its –erase or -e option:
$ rpm --erase dnf
error: Failed dependencies:
dnf = 4.14.0-1.el9 is needed by (installed) yum-4.14.0-1.el9.noarch
dnf >= 4.11.0 is needed by (installed) yum-utils-4.3.0-7.el9.noarch
As expected, the dependency check fails, since both the yum and yum-utils packages rely on the dnf package.
Let’s confirm this with an rpm –query like before:
$ rpm --query --whatrequires dnf
yum-4.14.0-1.el9.noarch
yum-utils-4.3.0-7.el9.noarch
$ rpm --query --whatrequires yum
no package requires yum
$ rpm --query --whatrequires yum-utils
no package requires yum
This time, we don’t have interdependencies. Thus, we directly –erase the dnf package with –nodeps:
$ which dnf
/usr/bin/dnf
$ rpm --erase --nodeps dnf
Removed "/etc/systemd/system/timers.target.wants/dnf-makecache.timer".
$ which dnf
/usr/bin/which: no dnf in (/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
In this output, we see side effects in the form of a cache-clearing timer removal. Of course, the dnf command is now gone.
3.4. Install or Reinstall DNF YUM
Like before, we leverage rpm to –install the dnf YUM package we just downloaded:
$ rpm --verbose --install dnf-4.14.0-1.el9.noarch.rpm
Verifying packages...
Preparing packages...
dnf-4.14.0-1.el9.noarch
Created symlink /etc/systemd/system/timers.target.wants/dnf-makecache.timer → /usr/lib/systemd/system/dnf-makecache.timer.
Like with the original yum, we can perform an –upgrade to the same version:
$ rpm --verbose --upgrade dnf-4.14.0-1.el9.noarch.rpm
Finally, we have a working dnf command:
$ which dnf
/usr/bin/dnf
$ dnf --version
4.14.0
Installed: dnf-0:4.14.0-1.el9.noarch at Wed 06 Jun 2023 00:06:56 AM GMT
[...]
These short steps apply when we have the exact version of the dnf YUM package that we had previously. Changing a version requires more steps. and packages.
3.5. Change DNF YUM Version
Importantly, selecting a different version of the DNF YUM package from the one that was last installed might necessitate installing, removing, downloading, and reinstalling the respective dnf-data and python3-dnf packages:
$ rpm --erase --nodeps dnf-data
$ rpm --erase --nodeps dnf
$ rpm --erase --nodeps python3-dnf
$ curl --silent https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os/Packages/dnf-4.14.0-6.el9.noarch.rpm --remote-name
$ curl --silent https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os/Packages/python3-dnf-4.14.0-6.el9.noarch.rpm --remote-name
$ curl --silent https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os/Packages/dnf-data-4.14.0-6.el9.noarch.rpm --remote-name
$ rpm --install --verbose \
dnf-data-4.14.0-6.el9.noarch.rpm \
python3-dnf-4.14.0-6.el9.noarch.rpm \
dnf-4.14.0-6.el9.noarch.rpm
[...]
This way, we can replace our installation with any version. Still, like with the original YUM, we need to reapply any custom changes manually.
4. YUM Debugging
In general, we might not want to reinstall YUM or DNF YUM as a whole. In such cases, we can try several steps that deal with the cache and metadata of the packaging system:
Remove packages from the cache:
$ yum clean packages
Remove metadata:
$ yum clean metadata
Remove database cache:
$ yum clean dbcache
General cleanup:
$ yum clean all
At this point, let’s try an update:
$ yum update
If the update fails, the RPM database at /var/lib/rpm might have been corrupted. In that case, as a last resort, we might try to rebuild the RPM database with –rebuilddb after deleting all db files from /var/lib/rpm:
$ rm --force /var/lib/rpm/*db.*
$ rpm --verbose --rebuilddb
[...]
$ yum update
[...]
In case the update fails again, a reinstall may be needed to resolve deeper issues.
5. Summary
In this article, we delved deep into the original and DNF YUM versions, as well as how to install, uninstall, and debug them.
In conclusion, although the original yum and dnf have many differences, their reinstallation process is fairly similar.