Announcement

Collapse
No announcement yet.

15 APT Command Examples on Linux

Collapse
X
Collapse
  •  

  • 15 APT Command Examples on Linux



    In this guide, we will demonstrate a number of ways of using the APT command on Linux.

    Prerequisites

    To perform the steps demonstrated in this guide, you will need the following components:
    • A properly configured Linux distro that uses APT as the package manager, for example, Debian, Ubuntu, Linux Mint, Devuan, etc.
    • Basic understanding of the CLI and package management.

    The APT Command

    Any Linux distro comprises a number of packages. To manage these numerous packages in an efficient manner, almost all distros use one or more package managers.

    APT is one such package manager. It’s a CLI tool that can install, uninstall, and manage DEB packages on distros like Debian, Ubuntu, and Debian/Ubuntu-based ones.

    If an APT command is to make system-level changes, it must run with root privilege (with the help of the sudo command).

    Using the APT Command

    Example 1: Updating the List of Available Packages

    Before APT can work with packages, it needs a working database of all the available packages.

    To generate the most up-to-date database, run the following command:

    sudo apt update




    Here, APT will fetch the latest package database from the package repo(s). If any package update is available, APT will also print a notification.

    Example 2: List Available Package Upgrades

    If APT finds package upgrades, the following command will list all of them:

    apt list --upgradable




    Example 3: Upgrading Packages

    If one or more package updates were found, then you can upgrade all of them at once using the following command:

    sudo apt upgrade




    Alternatively, the following command will upgrade the whole system by removing, installing, and upgrading packages as needed:

    sudo apt full-upgrade




    Example 4: Upgrading Specific Packages

    If you don’t want to install all the package upgrades but specific ones, use the following command structure:

    sudo apt --only-upgrade install [package_name]




    Example 5: Downgrading Packages

    Sometimes, a package upgrade may break things. In such a situation, you may want to downgrade the problematic package(s) to an earlier version.

    To downgrade a package, run the following command:

    apt install [package_name]=[older_package_version]




    Example 6: Searching for a Package

    To check if a package is available from the package repo, use the following command:

    apt search [package_name]




    Example 7: Installing a Package

    If a package exists on the package repo(s) specified in the sources.list, then you can directly install it using the following command:

    sudo apt install [package_name]




    Example 8: Installing a Specific Package Version

    The procedure is the same as example #5. If you want to install a specific version of a package, specify it in the following manner:

    sudo apt install [package_name]=[package_version]




    If the package version is not specified, then APT will automatically install the latest package.

    Example 9: Listing Available Package Versions

    The default package repo(s), in most cases, will host multiple versions of a package. The following command will reveal all the available package versions:

    apt-cache policy [package_name]




    Example 10: Holding a Package

    Whenever running the

    apt upgrade


    command, it will check for upgrades for all the installed packages. In certain situations, however, you may want to skip upgrading certain packages for various reasons (stability, compatibility, etc.).

    In such a situation, you can mark the target package(s) as hold. Basically, whenever performing automatic package upgrades, APT will skip these packages.

    To mark a package as hold, run the following command:

    sudo apt-mark hold [package_name]




    To get a list of all the hold packages, run the following command:

    apt-mark showhold




    To remove the hold mark from a package, use the following command:

    sudo apt-mark unhold [package_name]




    Example 11: Installing a DEB Package

    Debian and Debian-based systems use DEB as the software packaging. All the packages from the package repo(s) also come as DEB files.

    To install a DEB package, use the following APT command:

    sudo apt install [path_to_deb]




    APT should take care of all the necessary dependencies as well.

    Example 12: Uninstalling a Package

    To uninstall a package, use the following command:

    sudo apt remove [package_name]




    Generally, APT won’t remove the package dependencies. To remove them afterward, run the following command:

    sudo apt autoremove




    We can also instruct APT to perform both of these actions in a single command:

    sudo apt autoremove --purge [package_name]




    Note that purging a package will also remove all the package-related configuration files, so exercise caution.

    Example 13: Listing Installed Packages

    APT keeps track of all the packages installed from the package repo(s) and DEB packages.

    The following command will list all the installed packages that APT is keeping track of:

    apt list --installed




    We can filter this output using grep to check if a package with a particular name/pattern is installed:

    apt list --installed | grep [pattern]




    Learn more about grep.

    Example 14: Package Details

    Before installing a package from the repo, we can check detailed info about it:

    apt show [package_name]




    Example 15: Downloading a Package from Repo

    To download a package from the package repo(s) without installing it, use the following command:

    apt download [package_name]






    It will download the package as a DEB file in the current directory. You can later install it using APT following the steps demonstrated in example #11.

    Bonus: Editing sources.list

    The sources.list file contains the URL for all the APT repos. We can open it using APT for editing:

    sudo apt edit-sources




    Alternatively, we can manually edit it using any text editor:

    sudo vim /etc/apt/sources.list




    In the case of Ubuntu, to auto select the nearest mirror, update the repo URLs with the following one:

    mirror://mirrors.ubuntu.com/mirrors.txt




    After updating sources.list, you have to update the APT cache:

    sudo apt update




    Bonus: APT Documentation

    The following command will print a quick help page:

    apt --help




    To learn more about all the available options with in-depth explanations, check out the man page:

    man apt




    Final Thoughts

    In this guide, we demonstrated numerous ways of using the APT command. We learned about installing, uninstalling, upgrading, downgrading, and downloading packages on Debian and Debian-based systems.

    While APT handles DEB packages, there are other Linux packaging formats, for example, flatpak, snap, etc. These packages are designed to be practically universal Linux packages that can be installed on any Linux system.

    Happy computing!







    More...
      Posting comments is disabled.

    Categories

    Collapse

    Article Tags

    Collapse

    There are no tags yet.

    Latest Articles

    Collapse

    • How to Change Your Prompt in Bash Shell in Ubuntu
      by Kasimba
      I don’t like my prompt, i want to change it. it has my username and host, but the formatting is not what i want. This blog will get you started quickly on doing exactly that.
      This is my current prompt below:



      To change the prompt you will update .bashrc and set the PS1 environment variable to a new value.

      Here is a cheatsheet of the prompt options:

      You can use these placeholders for customization:

      \u – Username

      ...
      01-06-2025, 01:15 AM
    • How to Install ZSH shell on Rocky Linux
      by Kasimba
      In this post I will show you how to install the ZSH shell on Rocky Linux. ZSH is an alternate shell that some people prefer instead of BASH shell. Some people say ZSH has better auto-completion, theme support, and plugin system. If you want to give ZSH a try its quite easy to install and give it a try. This post is focused on the Rocky Linux user and how to install ZSH and get started with its usage.
      Before installing anything new, it’s good practice to update your system packages:...
      12-25-2024, 02:01 AM
    • 5 Compelling Reasons to Choose Linux Over Windows
      by Kasimba



      by George Whittaker


      Introduction

      In the world of operating systems, Windows has long held the lion’s share of the market. Its user-friendly interface and wide compatibility have made it the default choice for many. However, in recent years, Linux has steadily gained traction, challenging the status quo with its unique offerings. What was once considered the domain of tech enthusiasts and developers is now...
      12-21-2024, 06:52 AM
    • NGINX vs Apache; Web Server Comparison
      by Kasimba
      Overview of NGINX and Apache

      NGINX and Apache are leading web server solutions utilized for hosting websites and web applications. Apache, developed by the Apache Software Foundation, offers robust configuration options and extensibility. NGINX, created by Igor Sysoev, is known for its efficiency in handling numerous concurrent connections with low resource utilization. Both servers function not only as HTTP servers but also as reverse proxies, load balancers, and more.

      What is

      ...
      12-21-2024, 03:54 AM
    • Monthly News – November 2024
      by Kasimba
      Hi everyone, I hope you are enjoying the BETA so far! This release introduces new features, tools, and artwork, so we anticipate a good number of bug reports. Every single fix helps us refine and improve the final release. Your feedback during the BETA phase is extremely important to us. Linux Mint 22.1 is our […]

      More...
      12-16-2024, 11:50 AM
    • Mastering OpenSSH for Remote Access on Debian Like a Pro
      by Kasimba



      by George Whittaker


      Introduction

      Remote access is a cornerstone of modern IT infrastructure, enabling administrators and users to manage systems, applications, and data from virtually anywhere. However, with great power comes great responsibility—ensuring that remote access remains secure is paramount. This is where OpenSSH steps in, providing robust, encrypted communication for secure remote management....
      12-13-2024, 10:31 PM
    Working...
    X