Linux Software Management Essentials

Linux, like most other operating systems, supports various types of software. Managing that software is a basic skill all Linux users should have. Doing it via a graphical user interface is usually pretty self-explanatory, as you just have to follow the instructions on the screen. Performing the same task by using a command line interface on the other hand is a whole different matter and could scare some Linux newcomers. To help crumble that fear a bit, here are some of the things to keep in mind about software management on a Linux-based operating system.

Software Mgmt On Linux-based OS

Most of today’s distributions of Linux-based operating systems install software in pre-compiled packages, which are archives that contain binaries of software, configuration files, and information about dependencies. Software on a Linux system often relies on other programs and libraries to work. These other programs are called dependencies. In addition, package management tools keep track of updates and upgrades so that the user doesn’t have to sniff out information about bug and security fixes. 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 tools APT (Advanced Packaging Tool) and Aptitude; RPM (Red Hat Package Manager) with its tools YUM (Yellowdog Updater, Modified) and DNF (Dandified Yum) are used by RedHat; Packman Package Manager is used by Arch Linux and so forth. Each packet manager and tool has its own pros and cons. Software can also be installed using newers solutions like AppImageFlatpack and Snap. For the sake of this blog let’s focus on dpkg and it’s front-end tools.

Before You Install…

Before installing anything onto the operating system there is an important question you should ask yourself – from where do the required packages come from? When Linux installs new programs, it installs them from a repository (also known as a repo). APT has a nifty little file named /etc/apt/sources.list which contains a list of the repositories it uses. It’s quite clever. If it notices that a program depends on another program, it will install both at the same time so you don’t have to worry about it. It is possible to add new repositories into that file or remove the ones that are already there to accommodate your specific needs. Another way to add/remove repositories into/from the list is to use the add-apt-repository command. This creates a new source file with the repository info into the /etc/apt/sources.list.d/ directory. It is also possible to manually add source files into that directory.

An additional thing to do before installing anything with APT would be to update it’s cache with apt-get update. This will update the package database, which means it will update the list of available packages and their versions, but it does not install or upgrade any packages.

Example of /etc/apt/sources.list

deb xenial main universe
deb xenial-updates main restricted universe multiverse
deb 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 xenial main universe' 
Removes a repository from the file
  #add-apt-repository -r 'deb xenial main universe'
Updates the APT cache
  #apt-get update

There is one more important matter to attend to before going to the next subject. 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 secure apt. Each time you add another apt repository to /etc/apt/sources.list, you’ll also have to give apt its key if you want apt to trust it. Once you have obtained the key, you can validate it by checking the key’s fingerprint and then signing this public key with your private key. You can then add the key to apt’s keyring with apt-key add <key>.

Installing, Upgrading, Removing And Cleaning Up

On to installation. If you want to install something you just need to use the command apt-get install <name-of-package>. This will search the database to see if the package you need is present in the repositories listed in /etc/apt/sources.list. If it is there, then it will ask for a confirmation and then install the package on a positive reply. In case a package needs to be reinstalled the --reinstall parameter can be used with the command. If you want to know which versions of which packages will be installed you can use 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 you cannot use the APT tool and have to use dpkg -i <path-to-package> instead.

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: 
 #dpkg -i /home/student/Downloads/htop_2.0.2-1_amd64.deb

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

Examples of upgrading packages

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

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 is good to regularly clean up the filesystem from not needed 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 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 without it growing out of control. apt-get autoremove is used to remove packages that were automatically installed to satisfy dependencies for some package and are no more 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. For example this can come up when a program depends on another program, but Linux can’t find it in the repositories. In these cases a good tool is apt-get checkwhich checks the system to see if there are any 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. With the manualsetting the package is marked as having been manually installed: you explicitly installed it, so the system will not remove it unless you ask for it to 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 you manually installed. APT marks these dependencies as automatically installed. These packages are marked for auto-removal if they are no longer needed, for instance with apt-get autoremove. The hold setting is used when the package needs to be held back. It will not be possible 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. Command line interfaces aren’t scary if you get to know them a little.