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

    -h, --help

    Display an overview of functionalities, and related help.
    1
    apt -h
    or
    1
    apt --help
    Output:
    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.

    -v, --version

    Display the current version number.
    1
    apt -v
    or
    1
    apt --version
    Output:
    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.
    1
    sudo apt update

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.

2. Upgrading Installed Packages

To upgrade all the installed packages on your system to their latest versions, use:

1
sudo apt upgrade

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.

3. Full Upgrade

Sometimes, you need to perform a full upgrade, which handles changing dependencies with new versions of packages.

1
sudo apt full-upgrade

Explanation: This command performs the same task as 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:

1
sudo apt install <package-name>

Example:

1
sudo apt install vim

Explanation: This command installs the 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:

1
sudo apt remove <package-name>

Example:

1
sudo apt remove vim

Explanation: This command removes the 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:

1
sudo apt purge <package-name>

Example:

1
sudo apt purge vim

Explanation: This command not only removes the package but also deletes its configuration files.

7. Searching for Packages

To search for a package, use:

1
sudo apt search <search-term>

Example:

1
sudo apt search vim

Explanation: This command searches for packages related to the term 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:

1
sudo apt show <package-name>

Example:

1
sudo apt show vim

Explanation: This command provides detailed information about the vim package, including its version, dependencies, and description.

9. Cleaning Up

To remove unused packages and clear up disk space, use:

1
sudo apt autoremove

Explanation: This command removes packages that were installed automatically to satisfy dependencies for other packages and are no longer needed.

10. Cleaning Up the Package Cache

To clear the local repository of retrieved package files, use:

1
sudo apt clean

Explanation: This command deletes all cached package files from the /var/cache/apt/archives directory.

11. Listing packages that can be updated:

To list packages that can be updated, use:

1
sudo apt list --upgradeable

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.
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 the redis package:
If you can’t remember the full package name, you can type the beginning of the name and press the Tab key to list matching packages. For example, if you type red and press Tab, it will show related packages.

1
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 redshift

To install redis without upgrading it if it already exists, use the --no-upgrade option:

1
sudo apt install redis --no-upgrade

Example:

1
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.

To upgrade redis without installing it if it doesn’t already exist, use the --only-upgrade option:

1
sudo apt upgrade redis --only-upgrade

To specify a particular version of a package, use the following syntax:

1
sudo apt install <package_name>=<version_number>

Replace package_name with the name of the package and version_number with the desired version.
To remove a package, use the remove command:

1
sudo apt remove redis

To find packages related to 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