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

    • Using MAXQDA for Qualitative Data Analysis on Linux
      by Kasimba



      by George Whittaker


      Introduction

      Qualitative data analysis (QDA) is a cornerstone of research across various fields, from social sciences to marketing. It involves uncovering patterns, themes, and meanings within non-numerical data such as interviews, focus groups, and textual narratives. In this era of digital tools, MAXQDA stands out as a premier software solution for QDA, empowering researchers to organize...
      11-21-2024, 11:31 PM
    • HAProxy on Ubuntu: Load Balancing and Failover for Resilient Infrastructure
      by Kasimba



      by german.suarez


      Introduction

      In today’s fast-paced digital landscape, ensuring the availability and performance of applications is paramount. Modern infrastructures require robust solutions to distribute traffic efficiently and maintain service availability even in the face of server failures. Enter HAProxy, the de facto standard for high-performance load balancing and failover.


      This article...
      11-21-2024, 03:00 PM
    • Providing a license for package sources
      by Kasimba
      Arch Linux hasn't had a license for any package sources (such as PKGBUILD files) in the past, which is potentially problematic. Providing a license will preempt that uncertainty.

      In RFC 40 we agreed to change all package sources to be licensed under the very liberal 0BSD license. This change will not limit what you can do with package sources. Check out the RFC for more on the rationale and prior discussion.

      Before we make this change, we will provide contributors with...
      11-19-2024, 09:21 AM
    • Linux Binary Analysis for Reverse Engineering and Vulnerability Discovery
      by Kasimba



      by George Whittaker


      Introduction

      In the world of cybersecurity and software development, binary analysis holds a unique place. It is the art of examining compiled programs to understand their functionality, identify vulnerabilities, or debug issues—without access to the original source code. For Linux, which dominates servers, embedded systems, and even personal computing, the skill of binary analysis is...
      11-18-2024, 07:10 PM
    • Ubuntu vs Debian: Linux Distributions Compared Deep Dive
      by Kasimba
      Debian and Ubuntu are two popular Linux distributions. In this deep dive we will guide you on the key differences between them from perspective of both corporate enterprise and personal productivity or pleasure usage. After reading this blog post you should be in a better position to decide to select Ubuntu or Debian.
      Stewardship, Licensing, Community and Cost

      Where as Debian is 100% fully committed to free software as defined by the Debian Free Software Guidelines, Ubuntu is created...
      11-17-2024, 08:30 PM
    • Debian Backup and Recovery Solutions: Safeguard Your Data with Confidence
      by Kasimba



      by George Whittaker


      Introduction

      In the digital age, data loss is a critical concern, and effective backup and recovery systems are vital for any Debian system administrator or user. Debian, known for its stability and suitability in enterprise, server, and personal computing environments, offers a multitude of tools for creating robust backup and recovery solutions. This guide will explore these solutions,...
      11-13-2024, 05:30 PM
    Working...
    X