How to use the APT package manager (Ubuntu / Debian)

The Advanced Package Tool (APT) is a powerful command-line package manager used in Debian-based Linux distributions like Ubuntu.

It simplifies the process of installing, removing, updating, and managing software on your system. APT works by retrieving information from software repositories, which are online servers containing collections of packages.

Let’s learn how to use the APT Package Manager.

Introduction to APT package manager

APT simplifies software management by automatically handling package dependencies.

When you install a package, APT ensures that all required libraries and components are also installed.

Similarly, when you remove a package, it takes care of any unused dependencies.

How APT Package Manager Works

APT maintains a local database of available packages. This database is updated periodically by retrieving information from the repositories.

When you issue a command, the APT package manager consults this database to determine the actions needed to fulfill your request.

Common APT Commands

  • Updating the Package List (apt update)
    This refreshes your local package database with the latest information from the repositories. It’s a good practice to run this command before installing or upgrading any packages.

Bash
sudo apt update

  • Upgrading Packages (apt upgrade)
    This command upgrades all installed packages to their latest versions available in the repositories. It is important to keep your system up-to-date to benefit from security patches and bug fixes.sh

Bash
sudo apt upgrade 

  • Installing a Package (apt install)
    This command installs a specific package and its dependencies. Replace package_name with the name of the package you want to install.

Bash
sudo apt install package_name

  • Removing a Package (apt remove)
    This command removes a specific package but keeps its configuration files. If you want to remove the configuration files as well, use apt purge instead.

Bash
sudo apt remove package_name

  • Searching for a Package (apt search)
    This command searches for packages matching a given keyword or pattern. It is useful when you don’t know the exact name of the package you are looking for.

Bash
apt search keyword

  • Listing Installed Packages (apt list)
    This command lists all installed packages on your system. You can use options to filter the output, such as --installed to show only installed packages or --upgradable to see packages with available updates.
Bash
apt list --installed

Additional APT Tips and Tricks

  • Auto-remove Unneeded Packages (apt autoremove):
    This command removes packages that were installed as dependencies of other packages and are no longer needed.

Bash
sudo apt autoremove

This is a handy way to save space. Here is an example of my output:

Bash
sudo apt autoremove
[sudo] password for rbailey: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED
  libc-ares2 libjs-highlight.js libnode72 libwpe-1.0-1 libwpebackend-fdo-1.0-1 pigz python3-cliapp python3-markdown python3-pygments python3-ttystatus runc
0 to upgrade, 0 to newly install, 11 to remove and 6 not to upgrade.
After this operation, 66.8 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 280575 files and directories currently installed.)
Removing libnode72:amd64 (12.22.9~dfsg-1ubuntu3.5) ...
Removing libc-ares2:amd64 (1.18.1-1ubuntu0.22.04.3) ...
Removing libjs-highlight.js (9.18.5+dfsg1-1) ...
Removing libwpebackend-fdo-1.0-1:amd64 (1.14.2-0ubuntu0.22.04.1) ...
Removing libwpe-1.0-1:amd64 (1.14.0-0ubuntu0.22.04.1) ...
Removing pigz (2.6-1) ...
Removing python3-cliapp (1.20180812.1-4) ...
Removing python3-markdown (3.3.6-1) ...
Removing python3-pygments (2.11.2+dfsg-2) ...
Removing python3-ttystatus (0.38-4) ...
Removing runc (1.1.7-0ubuntu1~22.04.2) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.8) ...

  • Fix Broken Dependencies (apt install -f):
    If you encounter dependency issues, this command attempts to resolve them by installing or removing packages as needed.

Bash
sudo apt install -f 

Here is an example output

Bash
sudo apt install -f snap
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed
  snap
0 to upgrade, 1 to newly install, 0 to remove and 6 not to upgrade.
Need to get 377 kB of archives.
After this operation, 2,756 kB of additional disk space will be used.
Get:1 http://gb.archive.ubuntu.com/ubuntu jammy/universe amd64 snap amd64 2013-11-29-11 [377 kB]
Fetched 377 kB in 0s (1,973 kB/s)
Selecting previously unselected package snap.
(Reading database ... 279996 files and directories currently installed.)
Preparing to unpack .../snap_2013-11-29-11_amd64.deb ...
Unpacking snap (2013-11-29-11) ...
Setting up snap (2013-11-29-11) ...
Processing triggers for man-db (2.10.2-1) ...

  • Hold a Package (apt-mark hold package_name):
    This prevents a package from being automatically upgraded.

  • Unhold a Package (apt-mark unhold package_name):
    This allows a package to be upgraded again.

Important Considerations

  • Always use sudo: Most APT commands require administrative privileges. Use sudo to run them as the root user.
  • Keep Backups: Before performing major upgrades or changes, back up your important data to avoid potential data loss.
  • Read Documentation: The APT documentation provides detailed information on all available commands and options.
Elsewhere On TurboGeek:  Vi, Nano, and Vim: The Battle of the Linux Text Editors

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate ยป