in

20 apt Commands to Know as a Sysadmin for Managing Debian/Ubuntu Packages

APT or Advanced Package Tool is the command line utility for package management in Debian/Ubuntu-based distributions. It allows you to install, modify or remove different packages without building them from source code. apt can perform many more actions related to package management beyond what tools like apt-get can do.

In this comprehensive guide, we‘ll cover 20 useful apt commands with examples to help you effectively manage packages on your Debian/Ubuntu servers.

1. Update Package Database

apt needs a repository of packages with a database of all available packages that can be installed on a Debian/Ubuntu system. This repository database should be updated before installing packages to ensure apt has the latest details like version, architecture, and location.

Updating the repository database is easy:

$ sudo apt update

For example:

$ sudo apt update
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]  
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Reading package lists... Done

This fetches the latest package metadata from all configured repositories.

2. Upgrade Installed Packages

To upgrade installed packages to the latest available versions, use:

$ sudo apt upgrade

This will never remove existing packages. If an upgrade requires removing an installed package, that package will be kept at the existing version.

For example:

$ sudo apt upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
  gzip libffi6 libssl1.1 openssl 
4 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,536 kB of archives.
After this operation, 2,048 B of additional disk space will be used.
Do you want to continue? [Y/n] 

This will upgrade the specified packages to their latest versions.

3. Full-Upgrade Packages

The full-upgrade command differs from upgrade in that it will remove installed packages if needed to upgrade the system as a whole:

$ sudo apt full-upgrade

Be careful using this, as it may remove packages you want to keep. Stick to upgrade unless you specifically need full-upgrade.

4. Install a Single Package

To install a new package:

$ sudo apt install [package]

For example, to install nmap:

$ sudo apt install nmap
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed
  nmap
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,054 kB of archives.
After this operation, 3,179 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 nmap amd64 7.80+dfsg1-1ubuntu2 [1,054 kB]
Fetched 1,054 kB in 0s (1,875 kB/s)
Selecting previously unselected package nmap.
(Reading database ... 148671 files and directories currently installed.)
Preparing to unpack .../nmap_7.80+dfsg1-1ubuntu2_amd64.deb ...
Unpacking nmap (7.80+dfsg1-1ubuntu2) ...
Setting up nmap (7.80+dfsg1-1ubuntu2) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for mime-support (3.64ubuntu1) ...

This installs the nmap package.

5. Install Multiple Packages

To install multiple packages in one command:

$ sudo apt install [package1] [package2] [package3]

For example:

$ sudo apt install nmap tcpdump wireshark
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libc-ares2 liblua5.3-0 libpcap0.8 libsmi2ldbl libwiretap8
  libwsutil8 python3-crypto python3-cryptography python3-cryptography-vectors
  python3-impacket python3-pysqlite2 tshark wireshark-common wireshark-qt
Suggested packages:
  python-cryptography-doc python-impacket-doc
The following NEW packages will be installed:
  libc-ares2 liblua5.3-0 libpcap0.8 libsmi2ldbl libwiretap8 libwsutil8 nmap
  python3-crypto python3-cryptography python3-cryptography-vectors python3-impacket
  python3-pysqlite2 tshark tcpdump wireshark wireshark-common wireshark-qt
0 upgraded, 18 newly installed, 0 to remove and 0 not upgraded.
Need to get 15.4 MB of archives.
After this operation, 59.6 MB of additional disk space will be used.
Do you want to continue? [Y/n]

This will install nmap, tcpdump and wireshark plus any required dependencies.

6. Install a Specific Version

To install a specific version of a package (if available in the repos):

$ sudo apt install [package]=[version]

For example, to install version 1.10.0 of nmap explicitly:

$ sudo apt install nmap=1.10.0
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed
  nmap
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,626 kB of archives. 
After this operation, 4,992 kB of additional disk space will be used.
Get:1 http://old-releases.ubuntu.com/ubuntu focal/main amd64 nmap amd64 1.10.0-1 [1,626 kB]
Fetched 1,626 kB in 0s (4,438 kB/s)       
Selecting previously unselected package nmap.
(Reading database ... 148805 files and directories currently installed.)
Preparing to unpack .../nmap_1.10.0-1_amd64.deb ...
Unpacking nmap (1.10.0-1) ...
Setting up nmap (1.10.0-1) ...
Processing triggers for man-db (2.9.1-1) ...

This explicitly installs version 1.10.0 instead of the latest 7.80.

7. Remove a Package

To remove an installed package:

$ sudo apt remove [package]

This will remove the package binaries but leave configuration files intact.

For example:

$ sudo apt remove nmap
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED
  nmap
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 3,179 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 148815 files and directories currently installed.)
Removing nmap (1.10.0-1) ...
Processing triggers for man-db (2.9.1-1) ...

This removes the nmap package (but not its config files).

8. Purge a Package

To fully remove a package including its configuration files:

$ sudo apt purge [package]

For example:

$ sudo apt purge nmap
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED
  nmap*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 3,179 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 148771 files and directories currently installed.)
Removing nmap (1.10.0-1) ...
Processing triggers for man-db (2.9.1-1) ...

This completely removes nmap and its configuration files.

9. Search for Packages

To search for packages by keyword:

$ apt search [query]

For example:

$ apt search "network scanner"
Sorting... Done
Full Text Search... Done
nmap/focal 7.80+dfsg1-1ubuntu2 amd64
  powerful CLI utility for network discovery and security auditing
dnstracer/focal 1.9.1-3 amd64
  Trace a chain of DNS servers to the source
angular-route/focal,now 1.7.9-1 all [installed]
  Routing and deeplinking services for AngularJS apps
hping3/focal 3.0.0-1 amd64
  Active Network Smashing Tool
macchanger/focal 1.7.0-7 amd64
  utility for manipulating the MAC address of network interfaces
masscan/focal 1.0.5+git20190825.7ad69dd-1 amd64
  TCP port scanner, spews SYN packets asynchronously
ndiff/focal 1.0.0+git20180627.60bd3dd-1 amd64
  Tool to scan filesystem differences like diff/cmp
sipgrep/focal 0.9.1-1 amd64
  Console tool for filtering SIP calls messages
wireshark-cli/focal 3.2.6+g32dac6a-2 amd64
  network traffic analyzer - console application

This searches package names and descriptions for "network scanner", listing any matches.

10. View Package Details

To view information about a package:

$ apt show [package]

For example:

$ apt show nmap
Package: nmap
Version: 7.80+dfsg1-1ubuntu2
Priority: optional
Section: net
Maintainer: Ubuntu Developers <[email protected]>
Installed-Size: 3179 kB
Depends: libc6 (>= 2.15), libdnet
Suggests: ndiff
Conflicts: nmap-common (<< 3.00)
Replaces: nmap-common (<< 3.00)
Homepage: https://nmap.org
Download-Size: 1054 kB
APT-Sources: http://archive.ubuntu.com/ubuntu focal/main amd64 Packages
Description: powerful CLI utility for network discovery and security auditing
 Nmap ("Network Mapper") is an open source tool for network exploration and
 security auditing. It was designed to rapidly scan large networks, although
 it works fine against single hosts. Nmap uses raw IP packets in novel ways

This prints detailed information about the nmap package.

11. List Installed Packages

To list all packages installed on the current system:

$ apt list --installed

For example:

$ apt list --installed
Listing... Done
acl/focal,now 2.2.53-6 all [installed]
acpid/focal,now 1:2.0.32-1ubuntu1 amd64 [installed]
adduser/focal,now 3.118ubuntu2 all [installed,automatic]
apt/focal,now 2.0.2ubuntu0.1 amd64 [installed,automatic] 
apt-utils/focal,now 2.0.2ubuntu0.1 amd64 [installed,automatic]
base-files/focal,now 11ubuntu5.2 amd64 [installed]
base-passwd/focal,now 3.5.47 amd64 [installed]
bash/focal,now 5.0-6ubuntu1.1 amd64 [installed]
bsdutils/focal,now 1:2.34-0.1ubuntu9.1 amd64 [installed]

This lists all installed packages in a concise format.

12. List Available Packages

To list all packages available for installation from configured repositories:

$ apt list --all-versions

This will print all available package names along with version and architecture information. It‘s a very long list so piping to grep or similar is recommended.

13. List Upgradable Packages

To view available updates for installed packages:

$ apt list --upgradable

For example:

$ apt list --upgradable
Listing... Done
libffi6/focal-updates,now 3.3-4ubuntu0.1 amd64 [upgradable from: 3.3-4]
libssl1.1/focal-updates,now 1.1.1f-1ubuntu2.7 amd64 [upgradable from: 1.1.1f-1ubuntu2.6]
openssl/focal-updates,now 1.1.1f-1ubuntu2.7 amd64 [upgradable from: 1.1.1f-1ubuntu2.6]

This lists any installed packages that have updates available.

14. Autoremove Unused Packages

Over time, you can accumulate outdated dependencies that are no longer needed:

$ sudo apt autoremove

This will remove any packages that were automatically installed as dependencies but are now no longer needed.

15. Check Package Dependencies

To view dependencies of a package:

$ apt depends [package]

For example:

$ apt depends nmap
nmap
  Depends: libc6
  Depends: libdnet
  Recommends: ndiff

This prints the packages that nmap depends on to function properly.

16. Reinstall a Package

To reinstall a package without removing it first:

$ sudo apt reinstall [package]

This will download the latest version and reinstall the package while retaining the existing config.

17. Download a Package

To download a package file without installing it:

$ apt download [package]

For example:

$ apt download nmap
nmap_7.80+dfsg1-2_amd64.deb

This will download the .deb file for the package to the current directory.

18. Check Package Changelog

To view the changelog of a package from the repo:

$ apt changelog [package]  

This will display the changelog showing a history of changes in each version of that package.

19. Edit apt Sources

To edit the source list files used by apt:

$ sudo apt edit-sources

This will open your sources.list files in an editor (defaults to $EDITOR) to allow updating. Use with caution.

20. Get Help for apt

To view usage information and available options:

$ apt help

And for help with a specific command:

$ apt help [command]

This will print detailed help for the given apt command.

Summary

The apt command provides powerful package management capabilities for Debian/Ubuntu systems. The examples covered here should give you a good idea of how to effectively use apt to search, install, upgrade and configure packages from the command line.

Let me know in the comments if you have any other favorite apt usage tips!

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.