20 Essential apt Commands for Ubuntu/Debian Sysadmins

By John Smith

As a Linux sysadmin, knowing how to effectively manage packages on Ubuntu and Debian systems is an indispensable skill. The apt command line tool offers immense power and flexibility when working with these .deb-based distributions.

Mastering apt allows you to seamlessly install, remove, upgrade, search, and manage packages on servers and workstations. While GUI tools exist, apt commands give you more control and insight into what’s happening under the hood.

In this comprehensive guide, we’ll explore 20 of the most useful apt subcommands and best practices for sysadmins.

1. Update Package Database

Before installing or removing any packages, you should update apt’s package database to ensure it has metadata on the latest versions:

sudo apt update

This fetches package info from all configured software repositories without making changes to the system itself.

Running updates regularly is crucial – new patches, features, and security fixes are constantly being released. Out of date package metadata will prevent you from accessing these updates.

2. Upgrade Installed Packages

Once your system is aware of the newest package versions, upgrade will fetch and install improved versions of whatever you have installed:

sudo apt upgrade

This intelligently upgrades software without ever removing anything. If a new package conflicts with something already on your system, it will simply ignore it rather than uninstalling the existing program.

3. Full System Upgrade

For more aggressive upgrades that incorporate new dependencies and potential removals, use full-upgrade:

sudo apt full-upgrade

This starts fresh by reexamining all packages and their requirements, uninstalling and installing as needed to get you to the latest everything.

Because full upgrades can remove software, only use them after understanding what might get cleaned up. Monitor the interactive output during upgrades to oversee changes.

4. Install New Packages

Installing a .deb package along with any required dependencies is straightforward:

sudo apt install nginx

You can pass multiple new package names to a single install command to queue up multiple items.

5. Remove Unneeded Packages

Removing packages no longer needed is a great way to simplify systems and reduce potential vulnerabilities:

sudo apt remove nginx 

In general you‘ll want to use remove instead of purge – this gets rid of the package while preserving configuration data in case you install again later.

6. Erase Configuration Files

When you want to completely cleanse a package from a system, use purge:

sudo apt purge nginx

This erases everything related to the specified package, including config data that remove would leave behind. Be very careful using purge!

7. Search Available Packages

If you know the name of a package you want to install, you can skip searching. But apt makes it very easy to find software in repositories:

apt search php

This searches package names and descriptions. Change search to ~description if you only want matches in package summaries.

8 View Package Details

When you want deeper information on an available or installed package, use show:

apt show nginx 

The output displays version numbers, dependencies, package size, maintainer info, and the repository source.

9. List Installed Packages

You can output a full listing of what’s currently installed on the system to the terminal:

apt list --installed

Customize the format even further with formatting options explained in man apt.

10. Identify Upgradeable Packages

It’s easy to check which installed packages have upgrades available from configured repositories:

apt list --upgradeable 

Compare this list after an apt update to see newly available upgrades.

11. Cleanup Unused Dependencies

Over time, accumulating dependencies for no-longer-installed software can bloat disk usage. Identify and remove these extras:

sudo apt autoremove

Autoremove scours your system looking for unnecessary packages based on current installations. It’s safe to run regularly.

12. Check Package Dependencies

To inspect dependencies of a package prior to installing it, use depends:

apt depends apache2

This can reveal extra required software you might need to prepare for. It’s also useful for identifying why a package was installed originally.

13. Reinstall Damaged Packages

If a package seems corrupted or improperly configured, you can reinstall it without removing it first:

sudo apt reinstall nodejs

This fetches pristine replacement package files and re-sets everything up properly again.

14. Simulate Installs and Upgrades

When you want to preview how an install or upgrade will modify your system, include -s to simulate the changes:

sudo apt install -s nodejs

For upgrades:

sudo apt full-upgrade -s

Review the verbose output before running without -s to apply the changes for real. Simulations are an excellent way to safely test complex operations.

15. Download Package Files Without Installing

Maybe you want to fetch a .deb without actually installing it yet. Just use download instead of install:

sudo apt download nginx

The package file will be saved in your current working directory. You can transfer it to another machine without apt and install locally with dpkg.

16. Fix Broken Package Dependencies

If you encounter dependency errors during installations, use the fix-broken parameter:

sudo apt install --fix-broken

This will hunt down and attempt to resolve any missing packages or conflicts standing in your way.

17. Edit apt Configuration Sources

To modify apt repositories and settings defined in /etc/apt/sources.list configuration data, use:

sudo apt edit-sources

This will open your defined package sources in an editor. Modify carefully and save upon exit.

18. Pin Package Versions

When you want to lock in specific package version rather than always getting the latest, manipulate apt preferences.

First, install the right version you want with =:

sudo apt install nginx=1.18.0

Next, pin that version by creating a preferences .pref file:

echo "Package: nginx
Pin: version 1.18.0" | sudo tee /etc/apt/preferences.d/nginx

Now apt will not upgrade Nginx further even if newer releases become available. Adjust as needed!

19. Enable Recommended Packages

When installing software that suggests additional “recommended” packages you may want to enable, do:

sudo apt install --install-recommends nginx

This pulls down not only the required Nginx files but also any supplementary packages recommended for full functionality per distro guidelines.

20. Read the Manual

Don’t forget, built-in documentation is available directly from apt:

apt help
man apt  

This shows usage tips, available commands, formatting options, and more details on everything covered here. The man pages are quite helpful once you get comfortable with the basics and want to dive deeper!

apt offers immense control over packages when administering Ubuntu, Debian, Linux Mint, Pop!_OS, and other apt-based distributions. Mastering key commands like installing, removing, updating, upgrading, searching, and cleaning up unneeded packages will make your sysadmin life much smoother.

The power is right at your fingertips – wield it wisely and enable your infrastructure to stay lean, secure, and up-to-date!