The APT package manager is the powerhouse behind software management in Ubuntu and Debian-based Linux distributions. After years of refinement, APT makes installing, removing, and updating packages a breeze.

In this comprehensive guide, I‘ll demonstrate the ins and outs of harnessing APT to effortlessly manage software on your Ubuntu 20.04 LTS system. Whether you‘re a new or experienced Linux user, you‘ll learn techniques to become an APT master.

A High-Level Overview of APT

APT is an acronym for Advanced Package Tool. As the name suggests, it is an advanced package management system comprised of multiple commands and utilities to handle software packages on Debian-based systems.

Some key advantages of APT include:

  • Dependency resolution – APT automatically handles dependencies, meaning if you try to install Package A and it requires Package B and C to run, APT will install all 3 packages automatically.

  • Secure repositories – Packages are downloaded from trusted and secure software repositories verified by your distro. This avoids malware and compromised software.

  • Version control – APT integrates seamlessly with Ubuntu and Debian‘s software repositories containing multiple versions of packages. You can install specific versions or upgrade to newer ones easily.

  • Optimization – APT downloads packages efficiently by analyzing what packages are required and retrieving all dependencies in bulk rather than individually.

Now that you understand why APT is so integral to Ubuntu, let‘s explore using it hands-on.

Managing Software Repositories

APT relies on dpkg for handling .deb packages, but also integrates with Ubuntu and Debian‘s online software repositories storing these .deb archives. This enables you to download and install thousands of open source packages from your terminal with APT.

These repositories are defined in the /etc/apt/sources.list file and .list files under /etc/apt/sources.list.d/:

$ cat /etc/apt/sources.list

deb http://us.archive.ubuntu.com/ubuntu/ focal main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ focal main restricted

deb http://us.archive.ubuntu.com/ubuntu/ focal-updates main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ focal-updates main restricted

The deb lines specify repositories with downloadable Debian binary packages, while deb-src lines specify repositories with source code that must be compiled first.

Let‘s break down the format:

deb [options] uri release component1 component2 ...
  • deb – Defines a source containing Debian binary packages
  • [options] – Optional compiler flags like CPU architecture
  • uri – Location of the repository (URL of Ubuntu archive)
  • release – Codename of the Ubuntu release (focal for 20.04)
  • component – Sections of the archive (main, restricted, universe)

You can view all currently enabled repositories:

$ sudo egrep -h -v ‘(^#)|(^$)‘ /etc/apt/sources.list $(ls /etc/apt/sources.list.d/*.list 2>/dev/null)

deb http://us.archive.ubuntu.com/ubuntu/ focal main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ focal main restricted
deb http://us.archive.ubuntu.com/ubuntu/ focal-updates main restricted 
deb-src http://us.archive.ubuntu.com/ubuntu/ focal-updates main restricted
deb http://us.archive.ubuntu.com/ubuntu/ focal universe  
deb http://us.archive.ubuntu.com/ubuntu/ focal multiverse
deb http://us.archive.ubuntu.com/ubuntu/ focal-updates universe
deb http://us.archive.ubuntu.com/ubuntu/ focal-updates multiverse

Let‘s go over the main repositories:

  • Main – Officially supported open source Ubuntu packages
  • Restricted – Proprietary drivers and closed-source packages
  • Universe – Community maintained open source packages
  • Multiverse – Software with copyright or legal restrictions

And some key operations for managing repositories:

Listing Enabled Repositories

$ sudo egrep -h -v ‘(^#)|(^$)‘ /etc/apt/sources.list $(ls /etc/apt/sources.list.d/*.list 2>/dev/null)

Enabling a Repository

$ sudo apt-add-repository universe

Disabling a Repository

$ sudo apt-add-repository --remove universe 

Adding a PPA Repository

$ sudo apt-add-repository ppa:libreoffice/ppa

Removing a PPA Repository

$ sudo apt-add-repository --remove ppa:libreoffice/ppa  

With key repositories configured, you‘re ready to start using APT!

Searching for Packages

The first step in installing new software is finding available packages. You can search Ubuntu repositories using the apt search command:

$ apt search "text editor"

Sorting... Done
Full Text Search... Done
atom/focal 1.47.0 amd64
  Atom is a hackable text editor built on Electron.

atom-beta/focal 1.50.0-beta0 amd64
  Atom is a hackable text editor built on Electron.

gedit/focal 3.36.1-1ubuntu1 amd64
  official text editor of the GNOME desktop environment

gedit-plugins/focal 3.36.1-1ubuntu1 amd64
  official plug-ins for gedit

The search queries package names, descriptions, and other metadata. Useful operators include:

  • "^foo" – Match names starting with foo
  • "bar$" – Match names ending with bar
  • "foo|bar" – Match foo OR bar

You can further inspect a package using apt show:

$ apt show gedit

Package: gedit
Version: 3.36.1-1ubuntu1  
...
Description-en: official text editor of the GNOME desktop environment
 Gedit is the official text editor of the GNOME desktop environment.
 It has been designed to be simple to use, yet powerful. It offers a
 decent range of features without cluttering the user interface.
 .
 Gedit features include:
 - Automatically syntax highlights over 50 programming
   and markup languages
 - Edit multiple files in one window (tabs)
 - ...

These basics enable you to search Ubuntu‘s vast repository and understand package information before installing anything.

Handling Packages with APT

Now for the real power moves – installing, removing, upgrading, and pinning packages.

Installing Packages

Let‘s try installing the Nginx web server:

$ sudo apt install nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
  libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip
  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter
  libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common nginx-core
Suggested packages:
  libgd-tools fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
  libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip
  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter
  libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx
  nginx-common nginx-core
0 upgraded, 18 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,554 kB of archives.
After this operation, 9,032 kB of additional disk space will be used.
Do you want to continue? [Y/n] 

APT handles installing any required dependencies automatically.

You can also stack multiple package names to batch install:

$ sudo apt install package1 package2 package3

And don‘t forget there‘s thousands more available by searching Ubuntu repositories with apt search!

Removing Packages

No longer need a package? Removing one is simple:

$ sudo apt remove nginx

To additionally purge configuration files:

$ sudo apt purge nginx

You can batch remove multiple packages the same as installing.

Upgrading Packages

Installing updates is a critical task to obtain important security patches and new features.

See packages with newer versions available:

$ apt list --upgradable

Listing... Done
libmysqlclient21/focal-updates,now 8.0.23-0ubuntu0.20.04.1 amd64 [upgradable from: 8.0.22-0ubuntu0.20.04.2]
mysql-common/focal-updates 5.8+1.0.5ubuntu2 all
nginx/focal-updates 1.18.0-0ubuntu1 amd64
nginx-core/focal-updates,now 1.18.0-0ubuntu1 amd64 [upgradable from: 1.18.0-0ubuntu1] 

Then upgrade everything:

$ sudo apt upgrade

Or individual packages:

$ sudo apt install package1=version package2=version

On modern Ubuntu versions, major kernel and system packages will trigger snapshots being created automatically via Snappy before upgrading. This enables rolling back in case of issues.

Pinning Packages

When working with multiple software versions, you may need to "pin" a package to prevent accidental upgrades from replacing a required older or newer release.

1. Identify Package Version

$ apt policy postgresql

postgresql:
  Installed: (none)
  Candidate: 12+214ubuntu0.1
  Version table:
     12+214ubuntu0.1 500
        500 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages

This shows me postgresql 12 is the current candidate for installation.

2. Create Preferences File

Add a file like /etc/apt/preferences.d/postgresql:

Package: postgresql
Pin: version 12+214ubuntu0.1
Pin-Priority: 1001

This pins postgresql at version 12+214ubuntu0.1

3. Install Pinned Version

Now attempt to install postgresql:

$ sudo apt install postgresql

APT will install version 12 instead of trying to upgrade to a newer release. Adjust pin priority from the default of 1000 to force or loosen version pinning strength.

When ready to upgrade, remove the pin file and install normally.

Instant Local Package Searches with APT-Cache

The apt-cache command offers powerful local package query functionality. It enables instant searching without touching remote repositories when you may lack an internet connection or wish to avoid network latency.

Common examples include:

# Search installed packages
$ apt-cache search postgres

# Show postgresql package details
$ apt-cache show postgresql

# List installed version of postgresql 
$ apt-cache policy postgresql

# Search package contents like bin files or libraries
$ apt-cache pkgnames postgresql  

Learn even more at man apt-cache!

Expert APT Tips and Tricks

Let‘s move onto some pro tips for managing software with APT!

Install 3rd Party Debs

APT isn‘t limited to official Debian/Ubuntu repositories. You can install any .deb file downloaded from external sources:

$ sudo apt install /path/to/package.deb

For example, Google provides a .deb for installing Chrome. APT will fetch any dependencies from standard repositories.

Fix Broken Packages

Occasionally system updates or modifications can create "broken" packages with unsatisfied dependencies and errors. Resolve issues by running:

$ sudo apt --fix-broken install

APT will attempt to fix broken installs and restore sanity.

Downgrade Packages

If upgrading a package causes problems, instantly downgrade:

$ sudo apt install package=version

Using the same install command but explicitly targeting an older release will trigger a downgrade.

Install Package Candidates

Instead of issuing full upgrades, preview what packages have newer versions available:

$ sudo apt list --upgradable

Then target them individually:

$ sudo apt install package1/focal-updates package2/focal-updates

This only upgrades specific packages, great for testing changes.

Source Repositories

APT supports utilizing deb-src source repositories. For example:

$ apt update
$ apt source linux
$ apt build-dep linux  

This downloads Linux kernel source then installs required dependencies to build it. Great for test compiling without requiring traditional development tools.

Of course this still requires essentials like gcc, make and so on to actually compile code.

Clean the Cache

APT maintains a local package cache in /var/cache/apt. This minimizes downloads for new installs. Clean it with:

$ sudo apt clean

And delete cached .deb archives to free disk space with apt autoclean.

Final Thoughts

Mastering APT is an integral process for operating Ubuntu and Debian-based systems. I hope these tips give you new insight into harnessing its power for seamless software management.

The tool offers unparalled repository integration, streamlined dependency resolution, robust version management and security hardening integrating with your Linux distro. Combined with the comprehensive Ubuntu and Debian software archives, you wield the power to install over 50,000 open source packages with a few keystrokes!

Of course no single guide can demonstrate everything APT has to offer. Be sure to check the man pages and explore functionality like aptitude, utilizing .dpkg selections, and alternative commands like apt-get.

Thanks for reading and happy packaging managing your Ubuntu box!

Similar Posts