Package Managers are a great feature of every Linux system. In Windows, we usually install software by downloading and running executable installers. apt and yum in linux allow us to easily handle the installation, removal, and inspection of software packages. Let's see how to use two among the most famous ones: APT and YUM.
1. APT (Advanced Packaging Tool) and YUM (Yellowdog Updater, Modified)
APT, on the other side, is a collection of different tools used for managing software in DPKG-based distributions. DPKG means Debian PacKaGe and is the package manager at the core of systems like Debian and Ubuntu. APT wraps the low-level calls to DPKG to provide the users with a user-friendly interface.
YUM is a package management
utility for RPM-based distributions. RPM (Red Hat Package Manager) is
the package manager that systems like RHEL and CentOS are based on. YUM
uses RPM under the hood, hiding its complexity through a high-level
abstraction. Fedora 22 and RHEL 8 introduced a rewrite of YUM, called
DNF, which is likely to become the new standard for RPM-based systems.
1.1 apt
apt is the newest tool of the APT package manager. It’s often a source of confusion because users are not sure whether using apt or apt-get for their operations and which are the differences. Let’s clear them out here: apt is the right tool when manually handling a Debian-based system. We should always prefer it over apt-get and apt-cache, as it unifies and simplifies the main operations of the other two. It also provides a useful progress bar along with some other small perks, as colored output and additional information.
1.2 apt-get
apt-get, on the other side, is the right choice if we’re going to script our management operations. It provides a more stable interface, more functionalities, and also allows for deeper customization. It was the default tool for every kind of operation up until Ubuntu 16.04. In Ubuntu 16.04, apt became the preferred option for human usage.
1.3 apt-cache
apt-cache, finally, is a tool that complements apt-get, providing information on installed software and available software as well.
1.4 snappy
Canonical, the company behind Ubuntu, has recently released a new distribution-agnostic packaging system called Snappy. snap packages are self-contained and can be installed in Ubuntu as well as in Fedora, CentOS, Arch, Gentoo, and so on. While the latest Ubuntu versions distribute some packages mainly through Snappy, APT remains the official way to manage software packages, at least for now.
2. Queries
Let’s now see how we can use these tools to query our package managers.
2.1 List Packages
We can ask YUM to list our packages with the following commands:
# List installed and available packages from repositories:
yum list all
# List only installed packages:
yum list installed
# List only available packages:
yum list available
# List installed and available kernel packages:
yum list kernel
With APT, instead, we can list our packages:
# List installed and available packages from repositories:
apt list
# List only installed packages:
apt list --installed
In case we want to see only the available packages, we can resort to some grep since there’s no APT command to do that out of the box:
# List only available packages:
apt list | grep -v installed
For scripting, we could also consider resorting to the nearest solution to apt list available from apt-cache:
apt-cache search .
Beware that the result will differ both in the number of occurrences and in their returned order, though.
2.2. Search Packages
With YUM, we can look for a package containing a specific term in its name or description:
yum search SOMETHING
This time, in APT it’s identical:
apt search SOMETHING
apt-cache search SOMETHING
2.3. Get Package Details
Once we know which package we’re interested in, we might want to check its details.
In YUM:
yum info MY_PACKAGE
And similarly, in APT:
apt show MY_PACKAGE
apt-cache show MY_PACKAGE
3. Package Management
Now that we’ve seen how to search and inspect software packages, let’s see how to manage them:
3.1. Update of Packages Information
First of all, we need to update our package index.
These commands don’t update any installed package, they just download the latest information about the packages that can be installed or upgraded.
n YUM it’s:
yum check-update
In APT, instead, it’s simply:
apt update
apt-get update
We should always run apt update before any other operations.
3.2. Installation of a Package
To install a package in YUM:
yum install MY_PACKAGE
Again, in Debian systems it doesn’t differ at all:
apt install MY_PACKAGE
apt-get install MY_PACKAGE
3.3. Upgrade of a Package
Upgrading a package can be done in different ways.
In YUM, the command yum update internally runs the yum check-update, which means that we don’t need to run the latter unless we want to avoid installing anything after updating the package index. We can upgrade all or some packages as follows:
# Update the package index and every package:
yum update
# Update only the package MY_PACKAGE:
yum update MY_PACKAGE
# Apply security-related package updates:
yum update --security
Or:
# Update every package:
apt upgrade
apt-get upgrade
# Update every package along with their dependencies:
apt full-upgrade
apt-get dist-upgrade
# Update only the package MY_PACKAGE:
apt-get install --only-upgrade MY_PACKAGE
# Apply security-related package updates:
unattended-upgrade --dry-run -d
t’s important to know that upgrading the packages along with their dependencies potentially implies uninstalling existing software and installing new software as well if this is required by the upgrade process.
Standard upgrade commands, on the other side, will never uninstall anything. However, differently from apt-get upgrade (which also doesn’t install anything), apt upgrade might install new software if needed.
3.4. Removal of a Package
Sometimes we need to remove a software package. Let’s explore the different ways of doing this then, from shallow and soft to deep and final.
To get rid of an installed package and possibly its dependencies in YUM we can do one of two equivalent commands:
yum erase MY_PACKAGE
yum remove MY_PACKAGE
In RHEL7 and higher, it’s possible to erase also additional unneeded packages with autoremove:
yum autoremove MY_PACKAGE
The Debian ways to delete a package instead are:
apt remove MY_PACKAGE
apt-get remove MY_PACKAGE
However, if we want to remove the package’s configuration too, completely purging the system from it, then we can exploit purge:
apt purge MY_PACKAGE
apt-get purge MY_PACKAGE
3.5. Clean Up
Sometimes, our system will be polluted by orphaned packages, which are not needed anymore but are still occupying space.
We can remove these unwanted packages in YUM through autoremove, without any package name:
yum autoremove
This also works in the same way on Debian distributions:
apt autoremove
apt-get autoremove
4. Repository Management
Both packaging systems start with a set of official repositories to query for fetching packages.
However, the community is thriving, and often the package we need is missing in the official repositories, or is there but in a version too old to fit our needs.
In these cases, we might want to add unofficial repositories to the package manager list (always paying attention to the fact that it might represent a security issue).
4.1. Addition of a Repository
Adding a repository in YUM is a manual operation, which consists in creating a file with the .repo extension under the folder /etc/yum.repos.d.
The file must contain all the information about the custom repository that we are connecting to.
Let’s try adding the AdoptOpenJDK repository:
# /etc/yum.repos.d/adoptopenjdk.repo
[AdoptOpenJDK]
name=AdoptOpenJDK
baseurl=http://adoptopenjdk.jfrog.io/adoptopenjdk/rpm/centos/7/$(uname -m)
enabled=1
gpgcheck=1
gpgkey=https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public
In APT, though, things are quite different. The GPG key of the repository must be downloaded and added to the APT keyring with apt-key add:
wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -
Then, at this point, the repository can be added through add-apt-repository –yes followed by the URL:
add-apt-repository --yes https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/
Contrary to YUM, all the repositories are saved in a single file, /etc/apt/sources.list.
4.2. Removal of a Repository
Removing a repository in YUM is performed differently depending on how it’s been installed.
We can run the following command and analyze its output:
rpm -qa | grep -i CUSTOM_REPOSITORY
If the repository’s RPM package is found, it means it’s been installed through RPM, and we can remove it using -e:
rpm -e CUSTOM_REPOSITORY_RPM_PACKAGE
Otherwise, we can simply delete the repository file:
rm /etc/yum.repos.d/CUSTOM_REPOSITORY.repo
We can also disable it without deleting it, by simply turning enabled=1 to enabled=0 in the repository file.
In APT, on the other hand, we can simply do:
add-apt-repository --remove ppa:CUSTOM_REPOSITORY/ppa
Alternatively, we can comment out the rows relative to the repository in the /etc/apt/sources.list file.
5. Other Operations
These two do a lot of other operations. Let’s cover some of them.
5.1. Reinstalling a Package
In case of a corrupted package, like some files are missing, we can reinstall it with:
yum reinstall MY_PACKAGE
apt reinstall MY_PACKAGE
apt-get reinstall MY_PACKAGE
5.2. Installing a Specific Version of a Package
Sometimes, instead, we might need to install a specific version of a package.
We can check the available versions of a package with:
yum list MY_PACKAGE --showduplicates
apt-cache showpkg MY_PACKAGE
Then we can target the wanted version for a new installation:
yum install MY_PACKAGE-WANTED_VERSION
apt install MY_PACKAGE=WANTED_VERSION
apt-get install MY_PACKAGE=WANTED_VERSION
YUM also allows us to downgrade from an existing one to one of the previously installed versions:
yum downgrade MY_PACKAGE-WANTED_VERSION
5.3. Installing a Group of Packages
Some packages are combined in a group for a common purpose and can be installed all at once.
Let’s target another real-world example and think of installing the graphics environment in a Linux server.
Installing the X Window System and GNOME groups will save us the hassle of installing hundreds of packages by hand:
yum groupinstall 'X Window System' 'GNOME'
The Debian package manager, however, handles them as simple packages, so the classic apt install command is enough:
apt-get install xorg
0 comments:
Post a Comment