How to Use APT Package Manager

August 22, 2024

Introduction

The APT package manager is a powerful tool for managing software on Debian-based Linux distributions. It simplifies installing, updating, and removing packages by automating the retrieval and configuration of software from trusted repositories.

This tutorial explains what the APT package manager is and how to use it.

How to use APT package manager

Prerequisites

  • A Linux system.
  • Access to the terminal.
  • An account with sudo or root privileges.

What Is APT Package Manager?

Advanced Package Tool (APT) is a package manager used in Debian-based Linux distributions, such as Ubuntu. It simplifies installing, updating, and removing software by handling dependencies and retrieving packages from trusted repositories. APT provides command-line tools, like apt-get, apt, and apt-cache, to manage software packages efficiently.

apt-get is an older command that offers an extensive range of options, primarily used in scripts and for more advanced package management tasks.

apt is a newer, user-friendly command that combines functionalities from both apt-get and apt-cache. It provides a streamlined interface for common tasks like installing, updating, and searching for packages. The difference between the two is that apt handles routine operations while apt-get offers more granular control for advanced actions.

The apt-cache command focuses on querying the APT package database. It allows users to search for packages, view package details, and check dependencies.

Note: Learn about the differences between apt and apt-get.

Why Use APT Package Manager?

APT simplifies software management by automating installation, updates, and removal tasks. It automatically handles dependencies and ensures that all required libraries and packages are installed.

Other reasons to use APT are:

  • Security. APT retrieves packages from verified repositories, ensuring the software is secure and stable. It performs integrity checks on packages to reduce the risk of installing corrupted or compromised software.
  • Versatility. APT includes command-line tools tailored to specific tasks. This versatility allows users to perform routine management, automate tasks through scripting, or query detailed package information, which is useful to both new users and advanced administrators.
  • Consistency. APT ensures the software installation and updates are consistent across systems, reducing the likelihood of version conflicts or dependency issues. This consistency makes maintaining and managing multiple systems easier, ensuring they all have the same software versions and configurations.
  • Community support and documentation. APT is widely used in the Linux community, meaning many resources, forums, and documentation are available. This strong community support makes finding help, troubleshooting issues, and learning more about using APT easier.

How to Use APT Package Manager?

Understanding how to use APT effectively helps streamline tasks like installing, updating, and removing packages while ensuring system stability and security. The following text guides you through the essential commands for utilizing this package manager.

Searching for Packages

Searching for packages with the APT package manager lets you quickly locate software available in your system's repositories. With the apt search command, you can find packages by name, description, or other attributes.

The syntax is:

apt search [Search_Term]

For example, search for a package related to nginx using the APT package manager:

apt search nginx
apt search nginx terminal output

This command returns a list of available packages that match the term nginx, along with descriptions of each package.

Listing Packages

Listing packages with the APT package manager allows you to view installed, available, or upgradable software on your system. This helps you keep track of your system's software and manage updates effectively.

To see which packages are currently installed, run:

apt list --installed
terminal output for apt list --installed

List the packages available in the repositories with this command:

apt list
apt list terminal output

To see packages with newer versions available for update, run:

apt list --upgradable
apt list --upgradable terminal output

Viewing Package Information

Viewing package information with the APT package manager helps you gather detailed insight about a specific software package, such as the package's description, version, dependencies, repository source, and maintainer. The following command displays the package information:

apt show [Package_Name]

For example, view detailed information about the curl package using the APT package manager with the following command:

apt show curl
apt show curl terminal output

Installing Packages

The APT package manager allows you to download and install software from your system's repositories. It automatically handles dependencies, ensuring all required libraries and packages are installed along with the software. Use the following command:

sudo apt install [Package_Name]

Installing packages requires administrative privileges. Therefore, using sudo is necessary.

For example, install Vim with:

sudo apt install vim
sudo apt install vim terminal output

Updating Package Information

Updating package information with the APT package manager allows you to get the latest software versions and security updates. To update your package information, use the following command:

sudo apt update
sudo apt update terminal output

This command fetches the latest package lists from the repositories configured on your system, allowing you to see which updates are available. It does not upgrade any packages.

Upgrading Packages

Upgrading packages with the APT package manager allows you to update installed software to the latest available versions from your system's repositories. To upgrade all the installed packages, use the following command:

sudo apt upgrade
sudo apt upgrade terminal output

This command installs the latest versions of all packages with available updates without removing any existing packages.

Managing Automating Updates

Managing automated updates with the APT package manager helps ensure your system stays up-to-date with the latest software and security patches without manual intervention. Automate updates by configuring the unattended-upgrades package, which automatically installs updates for your system.

To accomplish that, take the following steps:

1. Install unattended-upgrades if it's not already on your system. Run the following:

sudo apt install unattended-upgrades
sudo apt install unattended-upgrades terminal output

2. Reconfigure the unattended-upgrades package to enable automatic updates:

sudo dpkg-reconfigure --priority=low unattended-upgrades
Configuring unattended-upgrades

During the configuration process, you'll be prompted to choose whether or not to enable automatic updates. Select Yes to enable them.

3. Acces /etc/apt/apt.conf.d/50unattended-upgrades with a text editor. This tutorial uses Nano:

sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

4. Customize which updates are automatically installed by editing the configuration file located at /etc/apt/apt.conf.d/50unattended-upgrades. For example, to ensure all updates are automatically applied, edit or add the following section

Unattended-Upgrade::Origins-Pattern {
"o=Ubuntu,a=jammy-security";
"o=Ubuntu,a=jammy-updates";
"o=Ubuntu,a=jammy-proposed";
"o=Ubuntu,a=jammy-backports";
};
Customizing /etc/apt/apt.conf.d/50unattended-upgrades in nano

The section consists of:

  • "o=Ubuntu". Specifies the origin as Ubuntu.
  • "a=jammy-security". Applies security updates for Ubuntu 22.04 LTS ("Jammy Jellyfish").
  • "a=jammy-updates". Applies general updates.
  • "a=jammy-proposed". Applies updates from the proposed repository.
  • "a=jammy-backports". Applies updates from the backports repository, offering newer versions of software that aren't included in the regular repositories.

5. Save and exit the file.

Uninstalling Packages

Uninstalling packages with the APT package manager lets you remove software you no longer need from your system.

To uninstall a package while leaving its configuration files intact, run:

sudo apt remove [Package_Name]

For instance, remove Vim with:

sudo apt remove vim
sudo apt remove vim terminal output

To remove the package along with its configuration files, use:

sudo <span style="background-color: initial; font-family: inherit; font-size: inherit; color: initial;">apt purge [Package_Name]</span>

Remove Vim and its configuration files with:

sudo apt purge vim
sudo apt purge vim terminal output

For a more thorough cleanup of unused dependencies, use:

sudo apt autoremove

The command detects packages automatically installed to cover other packages' dependencies but are no longer required because those packages have been removed.

Adding Repositories

Adding repositories with the APT package manager allows you to access additional software packages not included in your system's default repositories. Follow these steps to add a repository:

1. Use the add-apt-repository command followed by the repository's Personal Package Archive (PPA) or other repository details. The syntax for adding a repository using PPA is:

sudo add-apt-repository ppa:[Repository_Name]/ppa

The syntax for adding a repository using repository details is:

sudo add-apt-repository [Repository_Details]

For example, add a PPA for a software package with:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo add-apt-repository ppa:deadsnakes/ppa terminal output

This command adds the PPA for additional Python versions.

2. Update the package list:

sudo apt update
terminal output for sudo apt update

3. Install packages from the added repository:

sudo apt install python3.9
sudo apt install python3.9 terminal output

Removing Repositories

Removing repositories with the APT package manager involves deleting repository entries that are no longer needed. Here's how to do it:

1. Remove a PPA with:

sudo add-apt-repository --remove ppa:repository_name/ppa

For example, if you had added the ppa:deadsnakes/ppa and now want to remove it, run:

sudo add-apt-repository --remove ppa:deadsnakes/ppa
sudo add-apt-repository --remove ppa:deadsnakes/ppa terminal output

2. Update package list:

sudo apt update
sudo apt update command terminal output

APT Package Manager Best Practices

Effective management of your system with APT involves following best practices to ensure stability, security, and optimal performance.

The following text presents the APT package manager best practices:

  • Regularly update package lists. Run sudo apt update frequently to ensure you have the latest information on available packages and updates.
  • Upgrade the system regularly. Use sudo apt upgrade to keep your system and installed packages up-to-date.
  • Clean up unused packages. Periodically run sudo apt autoremove to remove unnecessary packages and free up disk space.
  • Verify package sources. Ensure you only add trusted repositories and PPAs to avoid security risks and stability issues.
  • Check package information. Use apt show [Package_Name] to review package details before installing or upgrading.
  • Backup important data. Before performing major upgrades or changes, back up important data to prevent data loss.
  • Avoid manual dependency management. Let APT handle dependencies automatically to prevent conflicts and ensure proper installations.

Conclusion

This tutorial explained what the APT package manager is and how it works. It also elaborated on reasons to use the APT package manager and its best practices.

Next, learn how to fix broken packages in Ubuntu if you encounter any issues.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
YUM vs. APT: What's the Difference?
November 24, 2022

APT and YUM are two popular package managers. This article shows their similarities and differences, and presents examples of basic package management operations...
Read more
Snap vs APT: What's the Difference?
January 21, 2021

The software in Linux is traditionally organized in repositories that contain applications and all the dependencies necessary to run them. However, there are also some...
Read more
How to Use apt-get reinstall on Debian and Ubuntu
October 22, 2020

When packages are accidentally removed or modified on Debian or Ubuntu, reinstalling them usually resolves the problem. The --reinstall flag is an apt-get command...
Read more
How to List Installed Packages on Ubuntu
March 9, 2022

Having a precise list of installed packages helps system admins maintain, replicate, and reinstall systems. In this tutorial, learn how to list all installed packages...
Read more