Managing Installed Software
local graph
Chapter 7 — Managing Installed Software
Section titled “Chapter 7 — Managing Installed Software”As you work in various Linux or Unix environments, you’ll need to add or remove software. This is usually done via package managers, although occasionally you’ll have to use other methods in a pinch.
You’re likely familiar with tools that manage libraries in your programming environment – npm, gem, pip, go get, maven, gradle, and so on. These package managers all operate on the same principles as the ones found in Linux and Unix.
Software package managers abstract away the many configuration and binary files that make up a piece of software and let you work with a single, neat “package” instead. This should feel familiar if you’re coming from Windows (.exe or .msi installers) or macOS (.dmg installers).
Additionally, most Linux package managers add a layer of security to the process, by:
- Using secure transport (TLS) for your download.
- Using cryptographic signing on the packages themselves, to prove that the authors are at least who they say they are (whether you implicitly trust them or not).
Various Linux distributions also pioneered the idea of searchable package repositories that help users find software to download, which inspired the Apple and Microsoft “App Stores” that exist today.
In this chapter, we’ll cover the following:
- What package managers are
- The most common package managers you’ll see
- The most important package management operations and the individual commands you’ll need to execute them, “translated” across package managers – this is 90% of what you’ll need to know in practice
- A popular procedure for downloading and executing custom install scripts
- A quick practical introduction to locally building and installing software yourself
If you’re looking for instructions on installing software in Docker, please check out Chapter 15, Containerizing Applications with Docker instead.
First things first: as a developer, you’ll reach for package management commands when you want to do a few common things:
- Install new software packages, for example, dependencies that your application expects to be available in its execution environment.
- Check for installed packages (for example, “Is the nginx web server already installed on this system?”).
- Update the currently installed set of packages, to make sure you’ve got the latest versions of everything. This is common for addressing vulnerabilities that are discovered, or ensuring you’ve got all the latest features of a piece of software.
- Remove a package – that is, uninstall it from the system.
Let’s jump into how to achieve these practical goals and see the practical commands that you’ll be using.
Working with software packages
Section titled “Working with software packages”Before we dive into practical commands, you should know that the exact commands you’ll use will differ based on the flavor of Unix (or distribution of Linux) that you’re using. Different Linux distributions use different package managers, and although their syntaxes diverge just enough to be annoying, they all work in an almost identical fashion. Common package managers include:
homebrew(macOS)apt(reliably found on Ubuntu and Debian-based systems, even minimal ones that don’t have aptitude installed)pacman(Arch)apk(Alpine)
In the rest of the practical sections that follow, we’ll introduce the high-level goal we want to achieve (such as “install a specific package”), and then show you the exact commands that accomplish that task, using the popular package managers we just named.
Update your local cache of repository state
Section titled “Update your local cache of repository state”Before installing or deleting packages, you’ll want to make sure that your local package cache (your system’s record of the packages available on the internet) is up to date.
For example, if you’re trying to install the nginx web server, but the last time someone updated the local package cache was a month ago, you might unintentionally install an outdated version from last month, instead of the new release from this week.
To update the cache, find your package manager in the list below and run the corresponding command:
| Package manager | Commands |
|---|---|
homebrew | brew update |
apt | apt update |
pacman | pacman -Sy |
apk | apk update |
Your local cache of available packages will be updated, and then you can move on to the exciting work of finding and installing a package.
Search for a package
Section titled “Search for a package”Not all packages have the same name as the software they contain. Firefox may be available in the firefox package, but you might be disappointed if you try to install ag (package name: silversearcher-ag). Search for a description of the package you’re considering installing with the following package manager commands:
| Package manager | Commands |
|---|---|
homebrew | brew search $PACKAGENAME |
apt | apt-cache search $PACKAGENAME |
pacman | pacman -Ss $PACKAGENAME |
apk | apk search $PACKAGENAME |
This is a good way to confirm you’re getting what you expect but it can also be used to spread out your search and look for general software related to your problem. For example, on Ubuntu, I can search for grep-like tools with apt-cache search grep. Any package containing grep in the name or description will be shown.
Install a package
Section titled “Install a package”Finally, the main event! Now that our repository cache is up to date and we know exactly which package we want to install, let’s run an install command:
| Package manager | Commands |
|---|---|
homebrew | brew install $PACKAGENAME |
apt | apt install $PACKAGENAME |
pacman | pacman -Sy $PACKAGENAME |
apk | apk add $PACKAGENAME |
If your package manager prompts you for confirmation, respond to the prompt and your package will be installed (along with any other packages it depends on).
Because some of these commands prompt for confirmation before installing a package, these can block a script. When you’re automating tasks with scripts, and you need to install a package, make sure to read the appropriate package manager man page for how to install packages in a noninteractive way. This is often done via an environment variable or an extra argument to the command.
Upgrade all packages that have available updates
Section titled “Upgrade all packages that have available updates”On a long-running system, you’ll want to occasionally upgrade the installed packages to their latest versions. This fixes known vulnerabilities, gets you the newest features, and keeps different systems from having their state drift apart just because they were provisioned a few months before or after another system:
| Package manager | Commands |
|---|---|
homebrew | brew upgrade |
apt | apt dist-upgrade |
pacman | pacman -Syu |
apk | apk upgrade |
These commands will also prompt for confirmation, so if you’re using them in scripts, the same advice about adding an option to make them noninteractive applies. For example, apt –y dist-upgrade will not wait for manual confirmation, and will just perform the upgrade.
Remove a package (and any dependencies, provided other packages don’t need them)
Section titled “Remove a package (and any dependencies, provided other packages don’t need them)”Sometimes you want to uninstall a package: maybe you were just trying it out, your application requirements have changed, or it’s known to be vulnerable without a fix in sight. All package managers have a command for removing an installed package:
| Package manager | Commands |
|---|---|
homebrew | brew remove $PACKAGENAME |
apt | apt remove $PACKAGENAME |
pacman | pacman -Rs $PACKAGENAME |
apk | apk del $PACKAGENAME |
One thing you may need to do before or after removing a package is verify that a package is even installed. Let’s see how to do that now.
Query installed packages
Section titled “Query installed packages”If you need to list all packages that are currently installed on a system, you can do that with a single command:
| Package manager | Commands |
|---|---|
homebrew | brew list |
apt | dpkg –l |
pacman | pacman -Qi |
apk | apk info |
Because this list is often hundreds or thousands of packages long, it’s a bit unwieldy. Narrow down the output by piping it into a search command like grep, to find just what you’re looking for:
dpkg -l | grep silversearcherii silversearcher-ag 2.2.0+git20200805-1 arm64 very fast grep-like program, alternative to ack-grepIf you’re confused by how we’re using that pipe to feed the output of dpkg into the grep command, see Chapter 1, How the Command Line Works for the basics of chaining together commands with the pipe character (|). We’ll also take a much deeper dive into this mechanic in the upcoming Chapter 11, Pipes and Redirection.
Now that we’ve covered the basic package management commands that you’ll be using 90% of the time, it’s time to demonstrate a few patterns you’ll use when there is no pre-built package available for the software you want to install.
Caution required – curl | bash
Section titled “Caution required – curl | bash”Sometimes you won’t find a pre-built package for the software you need. And that’s okay! Many online sources – even trustworthy and popular ones like homebrew on macOS – recommend a command-line install process that looks like this:
curl $SOMEURL | bashThis uses the curl command to download content from the web, and then uses that content as the input (|, the pipe character, which we covered in Chapter 1, How the Command Line Works) for running Bash. When you do this, you’re essentially running a script on the web instead of as a local file. This can be an extremely convenient way of installing software, but please make absolutely sure that it’s coming from a trustworthy source.
We recommend always at least looking at the script source, which you can see in the browser by visiting the script URL for this command (represented in the example below as $SOMEURL), or by splitting the single curl $URL | bash command into multiple commands, so that you can:
- Download the script.
- Read through it in a local text editor to verify that it’s not doing anything malicious, and optionally edit the script to suit your requirements.
- Run the script, now that you’ve verified it does only what you want it to do.
To split something like this curl $URL | bash pattern into multiple commands, you’d take the following steps:
# Download the installer and name the resulting file installer.shcurl $SOMEURL -o installer.sh# Read and optionally modify the script using a text editor like vimvim installer.sh# Make the script executable and run itchmod +x installer.sh./installer.shBy splitting this into multiple steps instead of just downloading an untrusted script and running it immediately, we give ourselves time to review the code we’re about to execute and verify that it’s safe. The end result is the same (the install script runs), but this approach gives us much more control and requires less blind trust.
There’s another way to get software installed on a system, even if there’s no pre-written install script available.
Compiling third-party software from source
Section titled “Compiling third-party software from source”This is the most manual, and most time-honored, way to install software on a system – manual compilation and installation! It doesn’t have the many advantages of a package manager, like speed, repeatability, ease of managing the installed software, and cryptographic validation of the software binary you’re installing.
But in a pinch, it’s still the most reliable way to get something installed, with no real outside dependencies except for the basic software tools (compiler, linker, and make script) that you’re already familiar with as a developer.
You’ll find yourself manually compiling and installing software when:
- There is no pre-packaged version of the software in the package manager you have available. For example, If you’re using a minimal container distribution (such as, Alpine), you might not find what you need in the package manager. In that case, you can compile your own binary from source, and get it into your container image that way.
- You need to add your own (or other custom) software to a Docker container.
- You need the absolute latest, bleeding-edge version of a piece of software, for which a packaged version doesn’t yet exist. This can be the case for slower-moving projects that don’t always have new packages available, or for situations where a hotfix for a critical vulnerability needs to be rolled out immediately, before the hotfix has made its way through the packaging process.
This process involves a few steps, with slight variations depending on the software.
Usually, this involves:
curlorwgetto download the compressed software archive.tar zxf downloadnameorunzip downloadnameto unarchive and decompress the source code directory you just downloaded.- Changing directory into the source directory you downloaded and reading any included
READMEfiles. This is where you’ll be informed of the exact process you need to follow to build the software, as well as any deviations from the norms we’re describing here. - Running
./configure, followed bymake, followed bysudo make installto build and install the binary.
As with other ways of installing software, either manually or through a package manager, keep in mind that configure and make execute arbitrary code by design. This means that running make install as root will result in all of this arbitrary code running as root. This should worry you. Make sure to verify that the software’s source code is trustworthy and that you are downloading it from a trustworthy source.
Example: compiling and installing htop
Section titled “Example: compiling and installing htop”To run through a real example of this, we’re going to download, compile, and install htop, which is a small, extremely useful system monitoring tool (similar, but superior, to the built-in top command). For the record, this is easily available via almost every Linux distribution’s package manager, but we’re going to pretend that it’s a hard-to-find, custom program that is not widely distributed through package managers.
The system we’re doing this on is an Ubuntu 22.04 Linux server, so if you want to follow along without having to do any troubleshooting of your own, use that.
First, we check for the latest release from the official GitHub repository here: https://github.com/htop-dev/htop/releases – at the time of this writing, that’s version 3.2.2.
Now you’ll want to make a directory for this build, just to keep things neat – I recommend something in the /tmp directory, which holds temporary files and has its contents deleted every time the system starts up:
mkdir /tmp/htopbuild && cd /tmp/htopbuildThat way, we can delete everything once the build is complete, to prevent cluttering our system with junk files from old builds. Now we’re ready to get started.
Install prerequisites
Section titled “Install prerequisites”First, we need to install the basic C development toolchain (compiler, linker, make, and other tooling – all the stuff you need to compile C code on Linux). On Ubuntu, this is accomplished by installing a metapackage – a package that is a kind of alias for multiple other packages – called build-essential:
sudo apt install build-essentialWe’ll also install a few other tools that we’ll use: wget to download files from the web and the ncurses dev library, which htop uses for a responsive command-line interface:
sudo apt install wget libncurses-devDownload, verify, and unarchive the source code
Section titled “Download, verify, and unarchive the source code”First, we’re going to download the source code and verify its cryptographic signature to make sure it’s a genuine release signed with the developer’s key:
wget https://github.com/htop-dev/htop/releases/download/3.2.2/htop-3.2.2.tar.xzThat gets us the compressed, archived source code directory, which we’ll be compiling into a binary.
Let’s now ensure that we have a developer-sanctioned release by checking the signature, which is just a sha256 hash of the source code.
Download the file containing the expected hash for this release and print it to the terminal:
wget https://github.com/htop-dev/htop/releases/download/3.2.2/htop-3.2.2.tar.xz.sha256cat htop-3.2.2.tar.xz.sha256Provided you’re working with the same version as we are in this example, you’ll see this hash:
bac9e9ab7198256b8802d2e3b327a54804dc2a19b77a5f103645b11c12473dc8 htop-3.2.2.tar.xzNow hash the source code we downloaded to verify that the hashes match, using the sha256sum tool:
sha256sum htop-3.2.2.tar.xzbac9e9ab7198256b8802d2e3b327a54804dc2a19b77a5f103645b11c12473dc8 htop-3.2.2.tar.xzWonderful! We now know that the software we have is the same as the official release that we meant to download. Let’s unarchive the source code directory and step inside it:
tar xf htop-3.2.2.tar.xzcd htop-3.2.2If you’re interested, now is the time to read the Readme file (general information about the program) and the INSTALL file (instructions for how to build and install the program).
Now we’re ready to start configuring and compiling this piece of software!
Configure and compile htop
Section titled “Configure and compile htop”Inside the source code directory, it’s time to run the ./configure script. This script ensures that we’ve got any dependencies required for compilation (shared libraries, tooling, and so on) installed, and configures things for the compilation we’re about to do:
./configureThis will produce output while the script runs, checking various dependencies and ensuring that your environment looks like it has everything needed for compilation.
If this script produces errors, read them carefully: usually, it will clearly tell you what’s wrong – a missing library or a problematic operating system setting. After fixing any problems it alerts you about, re-run it. When it has run successfully to completion, you’re ready to compile the htop binary:
makeThis will again produce reams of output while the compile script runs. If you’re totally new to makefiles, they are an extremely useful automation tool used widely by developers. Here’s an excellent tutorial: https://makefiletutorial.com/
Once the compilation is complete, we can install the htop binary we just created (it’ll be in the main source directory, named htop). Usually, there’s an automated way to do this:
sudo make installsudo is required because you’re moving the compiled binary into a protected (root-owned) location. After that, you can verify that htop is installed and working by typing:
htopYou should see a beautiful terminal-based GUI (thanks to the ncurses library) showing you your system’s current CPU load, memory utilization, and process list.
For programs that don’t ship with a full-featured install command, you can rely on the fact that there’s no magic in Linux and simply install the binary by moving it into the /user/local/bin/ directory, where locally compiled binaries belong:
mv htop /usr/local/bin/Having seen how straightforward this process can be, you now have all the knowledge you need to go forth and compile!
Conclusion
Section titled “Conclusion”In this chapter, you learned the basics of managing the software that’s installed in your Linux environment. First, we looked at how to accomplish this the easy way: software management via the package managers you’re most likely to encounter. Although this first approach should cover 90% of your needs, you then learned about the procedures you’ll need to apply to the last 10% of situations – careful vetting, followed by using custom install scripts or manual compilation and installation.
Hopefully you followed along with the practical compilation example and tried out the htop system monitor. Thankfully, htop is available via package managers everywhere – it’s a really useful tool that a lot of system administrators find invaluable on long-running production systems.
You should now be comfortable with the high-level concepts and practical commands you’ll need to effectively use many Unix and Linux systems, in both development and production environments.