Brief: This article guide walks us through the use of the Linux apt command with practical examples for effective package management in a Debian-based system.
Before Ubuntu 16.04, Debian-based Linux distributions like Ubuntu and Linux Mint made use of the ‘apt-get‘ command for package management.
The ‘apt‘ command came into the picture because ‘apt-get‘ was perceived as way too low level. The differences between these two commands are well explained in the following article:
What is APT?
Advanced Package Tool, more frequently called APT, is a collection of command-line programs used to install, remove, query, update and manage software packages on Debian and Debian-based distributions such as Ubuntu and Linux Mint through the use of official system repositories or third-party repositories that hold collections of software packages.
How to Use APT Command in Debian, Ubuntu, and Linux Mint
The usage of ‘apt‘ commands in Debian-based Linux distributions can be broken down into the following subsections.
apt update – Update Software Packages
apt update command is used to update the local repository cache with software package metadata (information about the latest software versions available) and displays the list of all packages available for upgradation.
$ sudo apt update
Three lines are likely to show up during this command execution:
- Hit – Implies no package version changes.
- Ign – package version info is ignored due to file retrieval error or version mismatch.
- Get – Implies availability of new package version and downloads the package version information which makes it possible to, later on, install the package.
apt upgrade – Upgrade Installed Software Packages
apt upgrade command is used to upgrade all available software packages presently installed on the system to the latest versions. Also, note during the upgrade process the newer packages will be installed if needed to satisfy dependencies, but existing packages will never be removed.
$ sudo apt upgrade
apt update and apt upgrade – What is the Difference?
apt update command only retrieves the latest version info on the installed package and the
apt upgrade command uses this version info to download and install data needed to upgrade the already existing package.
It, therefore, makes sense to jointly run the two commands as:
$ sudo apt update && sudo apt upgrade -y
apt full-upgrade – Upgrade Packages with Dependencies
apt full-upgrade command is used to upgrade all installed packages and get rid of redundant or no-longer-required packages. The full-upgrade is usually performed at the distribution release’s life cycle end.
$ sudo apt full-upgrade
apt install – Install New Packages
apt install command is used to install new software packages from the repository.
$ sudo apt install tree
If you are unsure of the package name, the
[Tab] key to auto-complete the package name after keying in the first few letters. You will be provided with package name suggestions that exist.
apt install – Install Multiple Packages
apt install command is also used to install multiple packages at the same time as shown.
$ sudo apt install tree treil
apt install – Install Package Without Upgrading
apt install command with
--no-upgrade option avoids the installed package from being upgraded.
$ sudo apt install tree --no-upgrade
apt install – Upgrade Installed Package
apt install command with
--only-upgrade option only upgrades the packages that are already installed, and does not install new packages.
$ sudo apt install tree --only-upgrade
apt install – Install Specific Package Versions
The Debian-based repository always avails the latest package version for installation. However, to install a specific version for project compatibility or something else, implement:
$ sudo apt install tree=2.0.2-1
apt remove – Remove Installed Package
apt remove command removes an already installed package from the system without removing any configuration files created by the package.
$ sudo apt remove tree
Note that after running the ‘apt remove‘ command, it’s only the package binaries that are removed. Re-installing this package reuses the already existing configuration files. The ‘apt purge‘ command removes both the package binaries and its configuration files.
apt purge – Remove Package with Configuration Files
apt purge command completely removes a package together with its configuration files.
$ sudo apt purge tree
apt search – Search New Packages
It is always a good idea to search for a package before installing it. The
apt search command retrieves all packages with the specified search term.
$ sudo apt search tree
apt show – Show Package Information
To view package content like description, sources, download size, installation size, and dependencies, implement:
$ apt show tree
apt list – List Installed and Upgradable Packages
apt list commands display information about all available, installed, and upgradeable packages.
$ sudo apt list --upgradable [List Upgradable Packages] $ sudo apt list --installed [List Installed Packages] $ sudo apt list --all-versions [List Package Versions]
apt autoremove – Remove Packages No Longer Required
Sometimes the Debian-based system needs some clean-up routine to free up space from packages that are not being used by the system. Unlike apt-get which uses clean and autoclean commands, apt uses the autoremove command to achieve this objective.
$ sudo apt autoremove
apt autoremove command gets rid of libraries and packages automatically installed during the installation of a targeted package but the system finds these libraries and packages somewhat useless.
apt edit-sources – Edit Package Sources List File
apt edit-sources command opens the /etc/apt/sources.list file contains info links on the packages’ location prior to their installation, update, and removal. Do not add, remove, or modify the content of this file unless you 100% understand what you are doing or have Linux system administration experience.
$ sudo apt edit-sources
Choose a preferred editor to open the file:
Changes on these /etc/apt/sources.list files are implemented during the next system update.
This guide on using ‘apt‘ commands with examples should guide you toward the mastery of the Linux command-line environment on a Debian-based Linux distribution.
Did we miss any important apt command examples? or if you have any extra tips, please share them in the comments below.