The Basics of Linux Software Management


Linux, like most other operating systems, supports various types of software. Managing that software is a basic skill that all Linux users should have. Handling it using a graphical user interface is usually intuitive and self-explanatory. With instructions provided on screen, the user simply needs to follow them. Doing the same job using a command line interface is entirely different. It’s more complicated and difficult and can be daunting for Linux newcomers.

For those who are less experienced with Linux, the following basic points about software management on Linux-based operating systems will help them to avoid difficulties with their deployments.

Software Management on Linux-based OS

Most of today’s distributions of Linux-based operating systems install software in pre-compiled packages. These are archives that contain binaries of software, configuration files, and information about dependencies. Those dependencies stem from the fact that software on Linux systems often relies on other programs and libraries in order to work properly.

In addition, package management tools automatically keep track of updates and upgrades, so users don’t have to worry about managing the details of bug fixes and security updates. Different packet management software can be found on different Linux distributions. For example, the Debian family uses dpkg (Debian Package Management System) and its front-end APT (Advanced Packaging Tool) and Aptitude tools. Red Hat-derived distributions use RPM (Red Hat Package Manager) with its YUM (Yellowdog Updater, Modified) and DNF (Dandified Yum) tools. Arch Linux uses the Pacman Package Manager. The list of distribution types and their associated tools goes on. Each packet manager and tool has its own pros and cons but all achieve similar results. Software can also be installed using newer solutions like AppImage, Flatpak and Snap. To pick one illustrative example for the purposes of this discussion, let us focus on dpkg and its front-end tools.

Before You Install…

Before installing anything onto the operating system, an important question needs to be asked. That is, where do the required packages come from? When Linux installs new programs, it does so from a repository (also known as a repo). APT includes a useful file named /etc/apt/sources.list which contains a list of the repositories Linux uses. Its functionality is quite clever. If it notices that one program depends on another, it will automatically install both simultaneously so the user doesn’t have to. It’s possible to add new repositories into that file or remove existing ones to accommodate your specific needs. Another way to add or remove repositories is to use the add-apt-repository command. This creates a new source file containing the repository information into the /etc/apt/sources.list.d/ directory. It’s also possible to add source files manually into that directory.

Before installing anything with APT, it is a good idea to update its cache with apt-get update. This will update the package database with the latest list of available packages and their versions. But this just updated the database; it does not install or upgrade any packages.

Example of /etc/apt/sources.list

deb http://us.archive.ubuntu.com/ubuntu xenial main universe
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-security main restricted universe multiverse

Examples of add-apt-repository and updating the APT cache

# Adds a repository into the file
add-apt-repository 'deb http://us.archive.ubuntu.com/ubuntu xenial main universe' 

# Removes a repository from the file
add-apt-repository -r 'deb http://us.archive.ubuntu.com/ubuntu xenial main universe'

# Updates the APT cache
apt-get update

There is one more important point to make relative to this area of software management in Linux. The apt-get package management utility uses public key cryptography to authenticate downloaded packages. apt-key is a program that is used to manage a keyring of GPG keys for securing apt. Each time a user adds another apt repository to /etc/apt/sources.list, he or she also has to give apt its key if they want apt to trust it. Once the user has obtained the key, he or she can validate it by checking the key’s fingerprint and then signing this public key with their private key. The user can then add the key to apt’s keyring with apt-key add <key>.

Installing, Upgrading, Removing and Cleaning Up

Now we move on to software installation. If a user wants to install something, he or she can simply use the command apt-get install <name-of-package>. This will search the database to determine if the needed package is present in the repositories listed in /etc/apt/sources.list. If it’s there, it will ask for a confirmation and then install the package following a positive reply. In case a package needs to be reinstalled, the --reinstall parameter can be used with the command. If the user wants to know which versions of which packages will be installed, they can obtain that information using the -s parameter with install. This will run a simulation of the installation without actually changing anything in the system. It is also possible to install a package which is already in the filesystem. For that, users cannot use the APT tool and must instead use dpkg -i <path-to-package>.

Examples of installing packages

# Installs htop from a repository in the sources.list file
apt-get install htop 

# Simulates an install of htop without changing anything in the system
apt-get install htop -s

# Reinstalls htop from a repository in the sources.list file
apt-get install --reinstall htop 

# Installs htop from a package in the filesystem
# Package naming convention: 
# <Name>_<VersionNumber>-<DebianRevisionNumber>_<DebianArchitecture>.deb
dpkg -i /home/student/Downloads/htop_2.0.2-1_amd64.deb 

There are two ways to upgrade installed software using the APT tool. The apt-get upgrade command upgrades all the installed packages and their versions on the operating system. Similarly, apt-get dist-upgrade also upgrades the packages and their versions, but in addition, handles changing dependencies with new versions of packages. It will automatically attempt to upgrade the most important packages at the expense of less important ones if necessary. Thus, the apt-get dist-upgrade may actually remove some packages in rare but necessary instances. To upgrade only a specific package, the apt-get install can be used to do so on any package that’s already installed.

Examples of upgrading packages

apt-get upgrade
apt-get dist-upgrade
apt-get install <name-of-package> # Upgrades the package if it already exists in the system

To delete a package the apt-get remove <name-of-package> command can be used. This will delete the package but leave all the configuration files intact and in place. If the configuration files also need to be deleted with the package, then apt-get purge <name-of-package> should be used.

Examples of deleting packages

# Deletes htop but leaves configuration files in place
apt-get remove htop 
# Deletes htop with all of its configuration files
apt-get purge htop 

It’s a good practice to clean up the filesystem regularly, getting rid of unneeded packages and the local repository from retrieved package files. apt-get clean can be used to clean the packages and install scripts, which are housed in /var/cache/apt/archives/. apt-get autoclean cleans obsolete deb-packages. The difference between apt-get clean and apt-get autoclean autoclean is that the latter only removes package files that can no longer be downloaded, and are largely useless. This allows a cache to be maintained over a long period of time without it growing out of control. apt-get autoremove is used to remove packages that were automatically installed to satisfy dependencies for some packages, but are no longer needed.

Examples of cleaning up packages

apt-get clean
apt-get autoclean
apt-get autoremove

Information Gathering

Knowing which software packages are installed on an operating system is a crucial part of software management. The apt-cache command can display information which is stored in APT’s internal database. The stats subcommand will display overall statistics about the cache. Using search with apt-cache will display a list of matched packages with a short description and using show with apt-cache will display the details of a package. Both apt-cache pkgnames and dpkg -l list all the packages which are installed in the operating system with the latter command showing more information (version number, revision number, architecture, description). There are occasions when some package dependencies become broken over time. For example, this can come up when one program depends on another, but Linux can’t find it in the repositories. In these cases, a good tool to use is apt-get check which checks the system for broken dependencies.

Examples of gathering information

# Displays info about packages
apt-cache stats
apt-cache search htop
apt-cache show htop

# Displays all installed packages
apt-cache pkgnames
dpkg -l

# Checks for broken dependencies
apt-get check

Changing package settings

There are several states that can be set for a software package. The manual setting is used to mark the package as having been manually installed. The user explicitly installed it, so the system will not remove it unless the user requests that it be removed. With the auto setting, the package is marked as having been automatically installed and will be removed automatically upon uninstallation. On the other hand, some packages are installed because they’re dependencies of a package the user manually installed. APT marks these dependencies as automatically installed, and marks them for auto-removal if they are no longer needed with apt-get autoremove. The hold setting is used when the package needs to be held back. This makes it impossible to install, upgrade, or remove the package until the hold mark is removed. There are even more interesting settings, and it is possible to change them with the apt-mark tool.

Examples of package setting commands

# Changes the setting of the package to manual, auto or hold
apt-mark manual virtualbox
apt-mark auto virtualbox
apt-mark hold virtualbox
apt-mark unhold virtualbox

# Displays a list packages which have the auto, manual or hold settings enabled
apt-mark showauto
apt-mark showmanual
apt-mark showhold

In Conclusion

So, what should you remember from all of this? Every Linux distribution has its own package management system and they all have their pros and cons. Linux packages come from different repositories and there is a dependency system between the packages. If automatic tools like Apt are used, then no dependency problems should arise. You also should know that command line interfaces aren’t scary if you get to know them a little!

Roland Kaur