APT also know as the “Advanced Package Tool” is a package management tool for Debian-based distributions including, Ubuntu, Debian and Linux Mint. It is used for installing, removing, updating and managing packages on Debian-based operating systems. The APT command is introduced in a newer version of Ubuntu 18.04/20.04, Debian 10 and Linux Mint 20. It replaces the older utility called apt-get. However, apt-get utility is also compatible with a newer version of Ubuntu and Debian.
Note: If you are using the rpm-based Linux distributions including, RHEL, CentOS and Fedora. You can read my guide on “YUM Command in Linux – A Definitive Guide”
You will learn the following:
- Update and Upgrade Packages with APT
- Full-upgrade vs Dist-upgrade
- Install a Package with APT
- Remove a Package with APT
- Remove Unused Packages with APT
- Download a Package and Source with APT
- List All Packages with APT
In this post, we will show you how to use the apt command to manage packages in Linux with various examples.
Prerequisites
- A server running Ubuntu/Debian/Mint.
- A root password is configured on your server.
1 – Update and Upgrade a Package with APT
Do you know? There are big differences between update and upgrade. When you run the “apt update” command it will only update the repository so that newer versions of packages available. It does not update the packages that are already installed. While the “apt upgrade” command will upgrade all installed packages in your system to the latest version.
To update the package repository or cache, run the following command:
apt update
To upgrade all installed packages to the latest version, run the following command:
apt upgrade
If you want to upgrade just a single package, run the following command:
apt upgrade package-name
2 – Full-upgrade and Dist-upgrade
The full-upgrade command will upgrade all installed packages and also remove some of them if that is needed to upgrade the whole system. You can run the following command to perform a full upgrade:
apt full-upgrade
The dist-upgrade command will intelligently handle the installation and removal of software packages. It will search for dependencies with newer versions, installs new packages, and removes old ones on its own. You can run the following command to perform a dist upgrade:
apt dist-upgrade
3 – Install a Package with APT
You can use the apt command to install one or more packages by specifying a package name. To install a single package named apache2, run the following command:
apt install apache2
To reinstall any package, run the following command:
apt install apache2 --reinstall
To install multiple packages named apache2 and mariadb-server, run the following command:
apt install apache2 mariadb-server
4 – Remove a Package with APT
You can use the apt command to remove one or more packages by specifying a package name. For example, remove a single package named apache2, run the following command:
apt remove apache2
To remove multiple packages, run the following command:
apt remove apache2 mariadb-server
The above command will only remove the specified packages, but it will leave some configuration file. If you want to remove a package with all configuration files, you can use the purge command with apt:
apt purge apache2
5 – Remove Unused Packages with APT
When you install any package it will also install all required dependencies for that package to your system. When the package is removed, the dependencies will still stay on the system. So it is a good idea to remove those unwanted packages from your system.
To remove the unwanted dependencies, run the following command:
apt autoremove
It is also recommended to remove all downloaded .deb file from /var/cache/apt/archives to free up the disk space. You can clean the apt cache with the following command:
apt autoclean
6 – Download a Package and Source with APT
If you want to download only the source code of any package, run the following command:
apt --download-only source apache2
To download and extract the source code of any package, run the following command;
apt source apache2
To download a .deb package without installing it to your system, run the following command:
apt download apache2
7 – List All Packages with APT
You can use the options list to list all available, installed and, upgradeable packages. For example, to list all available packages, run the following command:
apt list
You should get a list of all available packages in the following output:
0ad-data-common/focal,focal 0.0.23.1-1 all 0ad-data/focal,focal 0.0.23.1-1 all 0ad/focal 0.0.23.1-4ubuntu3 amd64 0install-core/focal 2.15.1-1 amd64 0install/focal 2.15.1-1 amd64 0xffff/focal 0.8-1 amd64 2048-qt/focal 0.1.6-2build1 amd64 2ping/focal,focal 4.3-1 all 2to3/focal,focal 3.8.2-0ubuntu2 all 2vcard/focal,focal 0.6-2 all 3270-common/focal 3.6ga4-3build1 amd64 389-ds-base-dev/focal 1.4.3.6-2 amd64 389-ds-base-libs/focal 1.4.3.6-2 amd64 389-ds-base/focal 1.4.3.6-2 amd64
To list only installed packages in your system, run the following command:
apt list --installed
Output:
acpid/focal,now 1:2.0.32-1ubuntu1 amd64 [installed] adduser/focal,focal,now 3.118ubuntu2 all [installed] alsa-topology-conf/focal,focal,now 1.2.2-1 all [installed,automatic] alsa-ucm-conf/focal,focal,now 1.2.2-1 all [installed,upgradable to: 1.2.2-1ubuntu0.5] apache2-bin/focal-updates,focal-security,now 2.4.41-4ubuntu3.1 amd64 [installed,automatic] apache2-data/focal-updates,focal-updates,focal-security,focal-security,now 2.4.41-4ubuntu3.1 all [installed,automatic] apache2-utils/focal-updates,focal-security,now 2.4.41-4ubuntu3.1 amd64 [installed,automatic]
To get a list of all upgradable packages, run the following command:
apt list --upgradeable
To find whether a specific package is installed or not, run the following command:
apt list --installed | grep apache2
Output:
apache2-bin/focal-updates,focal-security,now 2.4.41-4ubuntu3.1 amd64 [installed,automatic] apache2-data/focal-updates,focal-updates,focal-security,focal-security,now 2.4.41-4ubuntu3.1 all [installed,automatic] apache2-utils/focal-updates,focal-security,now 2.4.41-4ubuntu3.1 amd64 [installed,automatic] apache2/focal-updates,focal-security,now 2.4.41-4ubuntu3.1 amd64 [installed] libapache2-mod-php7.4/focal-updates,focal-security,now 7.4.3-4ubuntu2.4 amd64 [installed,automatic]
To get information about any package including, package dependencies, installation size and the package source, you can use the options show with the apt command.
For example, get detailed information of package named apache2, run the following command:
apt show apache2
Output:
Package: apache2 Version: 2.4.41-4ubuntu3.1 Priority: optional Section: web Origin: Ubuntu Maintainer: Ubuntu Developers <[email protected]> Original-Maintainer: Debian Apache Maintainers <[email protected]> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 541 kB Provides: httpd, httpd-cgi Pre-Depends: dpkg (>= 1.17.14) Depends: apache2-bin (= 2.4.41-4ubuntu3.1), apache2-data (= 2.4.41-4ubuntu3.1), apache2-utils (= 2.4.41-4ubuntu3.1), lsb-base, mime-support, perl:any, procps Recommends: ssl-cert