APT (Advanced Package Tool) is the package management system used by Debian, Ubuntu, and their derivatives. It simplifies the process of installing, updating, and removing software by handling dependencies and providing user-friendly commands.
APT is a front-end for the dpkg
package management system, which handles .deb
packages. APT automates the retrieval, configuration, and installation of software packages, making it easier for users to manage their systems.
- apt-get: A lower-level tool for managing packages.
- apt: A higher-level tool that combines functionalities of
apt-get
andapt-cache
for a more streamlined experience. - dpkg: The backend tool for handling
.deb
packages directly.
To install a package, use the following command:
sudo apt install package-name
Example:
sudo apt install git
Before installing or upgrading packages, update the package lists from the repositories:
sudo apt update
This command fetches the latest information about available packages, ensuring that you install the most recent versions.
To upgrade all installed packages to their latest versions:
sudo apt upgrade
If you want to perform a full system upgrade, which might include removing obsolete packages or installing new dependencies, use:
sudo apt full-upgrade
To remove a package while keeping its configuration files:
sudo apt remove package-name
Example:
sudo apt remove vlc
To remove a package along with its configuration files:
sudo apt purge package-name
To search for a package by name or description:
apt search package-name
Example:
apt search apache
This command will display a list of packages matching the search term.
To view detailed information about a package, including its description, dependencies, and installation size:
apt show package-name
Example:
apt show nginx
After installing or upgrading packages, you might want to clean up the local package cache to free up disk space:
sudo apt clean
To remove packages that were automatically installed to satisfy dependencies but are no longer needed:
sudo apt autoremove
Sometimes, package installations or removals can leave your system in an inconsistent state. To fix broken dependencies:
sudo apt --fix-broken install
This command will attempt to correct any dependency issues.
Pinning allows you to prioritize or prevent certain packages from being upgraded. This is useful if you want to stick with a specific version of a package.
To pin a package, you need to create or edit the /etc/apt/preferences
file:
Package: package-name
Pin: version version-number
Pin-Priority: priority
For example, to pin the nginx
package to version 1.18.0
:
Package: nginx
Pin: version 1.18.0
Pin-Priority: 1001
If you want to prevent a package from being upgraded, you can place a hold on it:
sudo apt-mark hold package-name
To remove the hold and allow upgrades again:
sudo apt-mark unhold package-name
In some cases, you might need to add additional repositories to install software that is not available in the default repositories. To do this, first, add the repository:
sudo add-apt-repository ppa:repository-name
Then, update the package lists:
sudo apt update
Finally, you can install the package from the newly added repository.
PPAs are third-party repositories for software packages. They are commonly used to get newer versions of software that are not yet available in the official repositories.
To add a PPA:
sudo add-apt-repository ppa:repository-name
To remove a PPA:
sudo add-apt-repository --remove ppa:repository-name
The list of repositories that APT uses is stored in the /etc/apt/sources.list
file and the /etc/apt/sources.list.d/
directory. You can manually edit these files to add or remove repositories.
Example entry in sources.list
:
deb http://archive.ubuntu.com/ubuntu/ focal main restricted
If APT encounters conflicts between packages during installation or upgrade, it will usually provide options to resolve them. You can force the installation with:
sudo apt install -f
This command forces the installation of packages with unresolved dependencies.
If a package is misbehaving or requires configuration, you can reconfigure it:
sudo dpkg-reconfigure package-name
APT logs its activities in the /var/log/apt/
directory. You can check these logs if you encounter issues during package management.
To view the history of APT transactions:
less /var/log/apt/history.log
APT is a powerful and versatile package management tool that simplifies software management on Debian-based systems. By mastering the commands and techniques outlined in this guide, you can effectively manage your system's software, ensuring that it remains up-to-date, secure, and efficient.
Next: YUM/DNF (RHEL/CentOS)
Previous: Using Package Managers