A Deep Dive into the apt Command: Linux Package Management Simplified
The apt
command is a powerful tool used for managing packages on Debian-based Linux distributions such as Ubuntu. It simplifies the process of installing, updating, and removing software packages. Whether you’re a beginner or an experienced user, mastering the apt
command can greatly enhance your Linux experience. In this guide, we’ll explore some of the most commonly used apt
commands with practical examples.
Syntax
1 | apt [options] [command] [package ...] |
- Options: Optional settings, such as -h (help), -y (automatically answer “yes” to prompts), -q (quiet mode, which suppresses the installation output), and others.
- Command: The operation to perform.
- Package: The name(s) of the package(s) to install.
Options
Display an overview of functionalities, and related help.-h
,--help
or1
apt -h
Output:1
apt --help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30$ apt -h
apt 2.0.9 (amd64)
Usage: apt [options] command
apt is a commandline package manager and provides commands for
searching and managing as well as querying information about packages.
It provides the same functionality as the specialized APT tools,
like apt-get and apt-cache, but enables options more suitable for
interactive use by default.
Most used commands:
list - list packages based on package names
search - search in package descriptions
show - show package details
install - install packages
reinstall - reinstall packages
remove - remove packages
autoremove - Remove automatically all unused packages
update - update list of available packages
upgrade - upgrade the system by installing/upgrading packages
full-upgrade - upgrade the system by removing/installing/upgrading packages
edit-sources - edit the source information file
satisfy - satisfy dependency strings
See apt(8) for more information about the available commands.
Configuration options and syntax is detailed in apt.conf(5).
Information about how to configure sources can be found in sources.list(5).
Package and version choices can be expressed via apt_preferences(5).
Security details are available in apt-secure(8).
This APT has Super Cow Powers.
Display the current version number.-v
,--version
or1
apt -v
Output:1
apt --version
1
2$ apt --version
apt 2.0.9 (amd64)Most Used Commands
1. Updating Package Lists
Before installing or upgrading any packages, it’s a good practice to update your package lists. This ensures you get the latest versions and security patches.Explanation: This command downloads the package lists from the repositories configured on your system. It does not upgrade any packages but prepares your system for installation or upgrade tasks.1
sudo apt update
2. Upgrading Installed Packages
To upgrade all the installed packages on your system to their latest versions, use:Explanation: This command will upgrade all the packages on your system that have updates available. It will prompt you to confirm the upgrade if necessary.1
sudo apt upgrade
3. Full Upgrade
Sometimes, you need to perform a full upgrade, which handles changing dependencies with new versions of packages.Explanation: This command performs the same task as1
sudo apt full-upgrade
apt upgrade
, but it also handles situations where new dependencies need to be installed or old dependencies need to be removed.4. Installing a New Package
To install a new software package, use the following command:Example:1
sudo apt install <package-name>
Explanation: This command installs the1
sudo apt install vim
vim
text editor. Replace<package-name>
with the name of the package you want to install.5. Removing a Package
If you want to remove a package but keep its configuration files, use:Example:1
sudo apt remove <package-name>
Explanation: This command removes the1
sudo apt remove vim
vim
package but leaves configuration files behind in case you decide to reinstall it later.6. Completely Removing a Package
To remove a package along with its configuration files, use:Example:1
sudo apt purge <package-name>
Explanation: This command not only removes the package but also deletes its configuration files.1
sudo apt purge vim
7. Searching for Packages
To search for a package, use:Example:1
sudo apt search <search-term>
Explanation: This command searches for packages related to the term1
sudo apt search vim
vim
. It helps you find packages that might be relevant to your needs.8. Viewing Package Information
To view detailed information about a specific package, use:Example:1
sudo apt show <package-name>
Explanation: This command provides detailed information about the1
sudo apt show vim
vim
package, including its version, dependencies, and description.9. Cleaning Up
To remove unused packages and clear up disk space, use:Explanation: This command removes packages that were installed automatically to satisfy dependencies for other packages and are no longer needed.1
sudo apt autoremove
10. Cleaning Up the Package Cache
To clear the local repository of retrieved package files, use:Explanation: This command deletes all cached package files from the1
sudo apt clean
/var/cache/apt/archives
directory.11. Listing packages that can be updated:
To list packages that can be updated, use:Explanation: This command displays a list of packages on your system that have updates available. It shows which packages can be upgraded, including their current and new versions, helping you decide which updates to apply.1
sudo apt list --upgradeable
Example:1
2
3
4
5
6
7$ sudo apt list --upgradeable
Listing... Done
apache2-utils/focal-updates,focal-security 2.4.41-4ubuntu3.17 amd64 [upgradable from: 2.4.41-4ubuntu3.15]
apparmor/focal-updates 2.13.3-7ubuntu5.3 amd64 [upgradable from: 2.13.3-7ubuntu5.2]
apt-transport-https/focal-updates 2.0.10 all [upgradable from: 2.0.9]
apt-utils/focal-updates 2.0.10 amd64 [upgradable from: 2.0.9]
apt/focal-updates 2.0.10 amd64 [upgradable from: 2.0.9]Examples
To install theredis
package:
If you can’t remember the full package name, you can type the beginning of the name and press theTab
key to list matching packages. For example, if you typered
and pressTab
, it will show related packages.To install1
2
3
4
5
6$ sudo apt install red redboot-tools redet-doc redis-sentinel redmine-pgsql redshift-gtk
redeclipse redfishtool redis-server redmine-plugin-custom-css redsocks
redeclipse-common redir redis-tools redmine-plugin-local-avatars
redeclipse-data redis redland-utils redmine-plugin-pretend
redeclipse-server redis-redisearch redmine redmine-sqlite
redet redis-redisearch-doc redmine-mysql redshiftredis
without upgrading it if it already exists, use the--no-upgrade
option:Example:1
sudo apt install redis --no-upgrade
To upgrade1
2
3
4
5
6$ sudo apt install redis --no-upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Skipping redis, it is already installed and upgrade is not set.
0 upgraded, 0 newly installed, 0 to remove and 239 not upgraded.redis
without installing it if it doesn’t already exist, use the--only-upgrade
option:To specify a particular version of a package, use the following syntax:1
sudo apt upgrade redis --only-upgrade
Replace1
sudo apt install <package_name>=<version_number>
package_name
with the name of the package andversion_number
with the desired version.
To remove a package, use theremove
command:To find packages related to1
sudo apt remove redis
redis
, use the search command:1
sudo apt search redis
Conclusion
The apt
command is a versatile tool that simplifies package management on Debian-based Linux distributions. By mastering these commands, you can effectively manage your system’s software and keep it up-to-date. For more detailed information, you can always refer to the apt
manual pages using:1
man apt
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.