Linux package managers: Learning apt and yum

Linux package managers: Learning apt and yum

If you are coming from the desktop side of computing, especially from Windows, then the notion of package management may seem a little odd to you. However, it is easy to understand the functionality and the advantages of using Linux package managers.

On the server side of computing, or on Linux in genreal, your applications run on top of layers of other software. A simple blog built using WordPress may depend on an Apache web server, PHP modules, and a MySQL database. These packages themselves depend on lower-level libraries. As these are independent projects, if there’s an update released for Apache web server, there’s no guarantee that, say, MySQL will work perfectly with the newer version.

To make matters worse, even the base operating system is constantly evolving and receiving updates that might be crucial but can potentially break the applications running on top of it. To solve these, and many other complications, we are going to familiarize ourselves with package managers.

What are Package managers?

  • Package managers are responsible for making sure you don’t get drowned in a complex list of inter-dependent software that you require for your application to run. They perform the following tasks.
  • Keep a list of trusted remote repositories from where the packages can be downloaded and installed.
  • Maintain a local index of all the available and installed packages.
  • Keep track of what packages are needed as dependencies for a particular package.
  • Perform package installations including bringing in the dependencies, adding binaries to PATH and making system-wide changes.
  • Update packages when a newer version comes in the remote repository.

So let’s get started with the two package managers that you are most likely to cross paths with, namely apt and yum.

Note: If you are planning on using Ubuntu or Debian, follow the apt package manager section. On the other hand, if you use CentOS you can skip ahead to yum package manager instead.

What's the BEST DEAL in cloud hosting?

Develop at hyperspeed with a Performance VPS from SSD Nodes. We DOUBLED the amount of blazing-fast NVMe storage on our most popular plan and beefed up the CPU offering on these plans. There's nothing else like it on the market, at least not at these prices.

Score a 16GB Performance VPS with 160GB of NVMe storage for just $99/year for a limited time!

Get limited-time deals!⚡

APT package manager

Advanced Packaging Tool (or APT) is the defacto package manager for many variants of Debian and Ubuntu operating systems. It installs packages system-wide and therefore requires root privileges to carry out the task. That’s why most commands below begin with sudo. If you are already logged in as the root user, you may skip the sudo part of the command.

Install a package

To install a package named PACKAGE_NAME, all you need to do is run:

$ sudo apt-get install PACKAGE_NAME

Or you can use a shorter command:

$ sudo apt install PACKAGE_NAME

What happens here is that the apt-get utility consults a local index of package names sees a match for PACKAGE_NAME and gets the package and necessary dependencies for it from a list of trusted URLs. It then installs the package and its dependencies and makes a note about these newer additions that are made to the system.

The command may nag you to confirm that you want to install the package, by entering y or yes if you want to avoid that, use the -y flag.

$ sudo apt-get install -y PACKAGE_NAME

You can also use the -qq flag which updates the system without printing anything to the terminal. This is useful when you are writing shell scripts to automate any apt related task.

Install packages from your filesystem

Debian and Ubuntu packages typically come as files with .deb extension. If you have one of these in your local filesystem you can install it by first traversing to the directory where the file is located and then running:

$ sudo dpkg -i FILENAME

dpkg is the utility that is responsible for the actual installation and configuration in the backend. apt interfaces with it every time it needs to modify the local installation of packages.

Upgrade installed packages

Once you have your VPS up and running, the most common task is going to involve checking if the package maintainers have released a new version of a package or if there are minor patches and fixes available for ones you currently have installed on your system.

Running the command sudo apt-get update updates the local index of packages and brings it up to speed with the remote sources from where the packages come. For example, if you are using Python 3.5 on your Ubuntu 16.04 system and the people who are responsible for testing the stability of Python 3.6 on Ubuntu 16.04 confirm that this newer version works perfectly, they will make it a part of the official repo.

To install the updates, you will have to run the following:

$ sudo apt-get upgrade

Together, the apt-get update and apt-get upgrade command can bring your system up to speed with the latest release of all the available packages. It is customary to run these on any new VPS before you start working with it.

List all available updates

If you are worried that the updates might mess up your pristine server you can issue the apt-get update and follow it with:

$ sudo apt list --upgradable

This would list all the installed packages that would get updated the next time you run apt-get upgrade. Notice, that this one uses apt and not apt-get.

Remove packages

To remove an installed package, just run:

$ sudo apt-get remove PACKAGE_NAME

To remove a package along with config files and other modifications, add a --purge flag.

$ sudo apt-get remove --purge PACKAGE_NAME

It is likely that despite removing a package, there might still be other dependencies left over in the system. Apt-get command installed these dependencies as they were needed for the main package that you have now removed, and they are no longer needed. To remove them, run:

$ sudo apt-get autoremove

Search packages

If you are unsure about what package you need, you can search for it in the local index.

$ sudo apt-cache search KEYWORD

This might result in a long list of results. Most of which might not even be related to what you are looking for, but could be an additional plugin for it with just the KEYWORD mentioned in its description. To narrow the search down to only the names of the available packages use the -n flag.

$ sudo apt-cache -n KEYWORD

Now the search result will be much more manageable.

Listing the dependencies

To list all the dependencies that a package may bring along with it, use the command:

$ sudo apt-cache depends PACKAGE_NAME

To list all the reverse dependencies, that is to say, to list all the packages that depend on one particular package, use the command:

$ sudo apt-cache rdepends PACKAGE_NAME

Add another repository

Sometimes the official repositories maintained by Debian or Ubuntu are far behind from what the package vendor can offer. In these cases, vendors offer their packages which you can add to the list of sources.

Usually, although not always, you begin by adding the cryptographic keys that the vendor has provided (generally as a URL):

$ sudo apt-key add GPG_KEY_URL

To add a repository, use the command:

$ sudo add-apt-repository REPO_URL

The REPO_URL part is formatted differently for different vendors. Some use PPA (Personal Package Archive) which is an Ubuntu-specific way for allowing vendors to build and publish apt packages. Also, there are fields with the codename for the OS version (trusty, xenial, bionic) and what kind of support the package receives (main, restricted, universe and multiverse). These are not the details that you have to worry about, for now, just know that they might be a part of various installation procedures and official docs.

The next time you run apt-get update apt will index the packages made available by this newer repo after which you are free to install them.

Apt, Apt-get and Apt-cache

Commands which involve apt-get typically involve querying remote repositories and fetching data from them, whereas, apt-cache related commands refer to the local index of packages. If you have trouble remembering which one is used where you can use apt instead. It’s shorter to type, and it unifies both the apt-get and apt-cache into a single command.

YUM package manager

The Red Hat family of Linux uses a different package manager, but it works similarly.

Install packages

To install any package, use the command:

$ sudo yum install PACKAGE_NAME

Install packages from your filesystem

Packages for CentOS, Red Hat, and Fedora family of operating systems have an extension of .rpm. If an .rpm file is present on your server that you wish to install, you can traverse to the directory where the file lives and run:

$ sudo yum localinstall FILENAME

Alternatively, you can use yum localinstall /PATH/TO/FILE to tell yum where the file is located.

Check for Updates

To check for the updates available for the installed packages, use the command:

$ sudo yum check-updates

Update installed packages

To update all the installed packages, run:

$ sudo yum update

Of course, it’s recommended that you check for the updates before installing them. You can also use the -y flag so you won’t be prompted to say yes as the installation progresses, or you can use the --quiet flag which prints no output and runs the update quietly in the background. The latter is useful if you are writing scripts to automate certain tasks. These flags work similarly for all the yum commands.

An alternative to update is the command yum update-minimal which only updates your packages from one revision to another. Which is handy in case there’s a bug fix that you need to apply. But it defers the feature updates where complete rework of packages may take place breaking the software running on your system in unexpected ways.

Remove packages

To remove a given package from your system, run:

$ sudo yum erase PACKAGE_NAME
### Or you may run
$ sudo yum remove PACKAGE_NAME

This would get rid of the specified package from your system. To get rid of the dependencies that were installed along with PACKAGE_NAME, which are no longer required, run:

$ sudo yum autoremove

Searching for packages

To install a package, you first need to know its proper name as it appears in the package index. The yum search utility can help you narrow your search down:

$ yum search KEYWORD

It searches the package index and displays results where KEYWORD has appeared in the name or summary of the package description. To do a more thorough search you can use:

$ yum search all KEYWORD

Listing all the Dependencies

To list all the dependencies that were (or will be) installed along with a specified package use the command:

$ yum deplist PACKAGE_NAME

Add another repository

To add repositories which are not included by default in the system, you can use a utility yum-config-manager. However, there’s a chance that this might not be available out of the box, either! You can install it by running:

$ sudo yum install yum-utils

After the installation finishes, adding new repositories is as easy as running:

$ sudo yum-config-manager --add-repo REPO_URL

You can now install the packages available in this new repository.

Summary of Linux package managers

Action yum Command apt Command
Install yum install apt install
Local packages yum localinstall dpkg -i
Check for updates yum check-updates apt update
Update Installed Packages yum update apt upgrade
Remove Packages yum erase [or] yum remove apt remove
Remove Orphaned Dependencies yum autoremove apt autoremove
Search Packages yum search apt search
List Dependencies yum deplist apt depends
Add repositories yum-config-manager --add-repo add-apt-repository