The apt Install Command in Linux: 17 Examples Inside

Try this guide with our instant dedicated server for as low as 40 Euros

apt install command

Over the years, the Advanced Package Tool (apt) has become the standard package manager in many Debian-based Linux distributions, such as Ubuntu and Linux Mint. Users consider it an efficient and user-friendly means of handling packages. 

You can use it in the terminal and experience really fast performance in installing (apt install), updating (apt update), and removing packages (apt remove). As a result, users can maintain their system’s software lineup up to date. Thanks to its support for a vast repository of packages, you can install (or remove) everything from essential system utilities to programming languages to third-party applications.

Its robust dependency management automatically resolves and installs the required libraries and packages, eliminating manual tracking and installation of software dependencies. apt commands allow users to keep their systems up-to-date with the latest security patches and features.

In this tutorial, we will discuss the apt install and other commands. We will start with an overview of the apt utility and then go into the details of using it in several real world scenarios. 

Table Of Contents

  1. What is apt?
  2. Common apt Commands
  3. 17 Examples of Using apt for Package Management
    1. Prerequisites
    2. Example #1: Update System Package List
    3. Example #2: Upgrade Installed Packages
    4. Example #3: Upgrade a Specific Package
    5. Example #4: Update and Upgrade in a Single Step
    6. Example #5: Perform a Full System Upgrade
    7. Example #6: Install New Packages with apt install
    8. Example #7: Download Packages Without Installation
    9. Example #8: Removing Packages with apt remove
    10. Example #9: Remove Configuration and Residuals
    11. Example #10: Clear Unused Dependencies
    12. Example #11: Simultaneously Install and Remove Packages
    13. Example #12: List Packages with apt list
    14. Example #13: List Only Installed or Upgradeable Packages
    15. Example #14: Finding Package Dependencies
    16. Example #15: Search for Packages
    17. Example #16: Limit the Search to Package Names Only
    18. Example #17: View Detailed Package Information
  4. Conclusion
  5. FAQs

What is apt?

The apt install command in Linux represents an enhanced interface to the Debian package management system. While it was introduced in 2014, it gained popularity around 2016. 

Many users consider it a replacement for an earlier apt-get utility. Developers imagined apt as a more user-friendly package management utility for Debian distributions. The current version of the utility offers functionalities such as searching, installing, removing, and querying package information. It also provides direct access to the capabilities of the libapt-pkg library.

The apt suite also has apt-cache, a tool for querying package information from your system’s package database. You can use it to discover packages by name or description and examine package dependencies.

We recommend installing the apt-doc package to get detailed help from documentation at /usr/share/doc/apt-doc/guide.html/index.html. 

Important: Note that you need administrator or sudo privileges to run apt commands.

Common apt Commands

Use Case apt Command
Update the package list apt update
Upgrade the installed packages apt upgrade
Upgrade the system with dependency changes apt full-upgrade
Install a specific package apt install {{{package}}}
Eliminate a package and its configuration files apt purge {{{package}}}
Identify upgradeable packages apt list –upgradable
Remove a specific package apt remove {{{package}}}
Remove unused packages apt autoremove
Search for packages apt search {{{string}}}
View package versions available apt list -a {{{package}}}
Show detailed package information apt show {{{package}}}
Display extensive package information versions of packages, including dependencies apt show -a {{{package}}}
Search for packages by keyword apt search word
List a package’s dependencies: apt depends {{{package}}}
Show available package version dependencies apt-cache showpkg {{{package}}}

17 Examples of Using apt for Package Management

Using the apt install command in Debian and its derivatives provides a streamlined approach to managing .deb packages. The utility automates the process of managing packages, efficiently handling the installation and removal of software along with their dependencies.

Prerequisites

Before trying out the following examples, we recommend you have the following:

  • A system running a Debian-based distribution, such as Ubuntu (we have Ubuntu 20.04 on our test system).
  • A user account with administrator or sudo privileges.

Example #1: Update System Package List

The apt update synchronizes your local package index with the online repositories, refreshing the metadata information about available package versions. This step is crucial before any upgrades or new installations to ensure you access the latest software versions. 

The syntax of the command is as follows:

# apt update

apt update

reading package list

Example #2: Upgrade Installed Packages

Use apt upgrade to update all your installed packages to the latest versions available in the repositories. When used without specifying a package name, the following command upgrades all the packages on the system:

# apt upgrade

Example #3: Upgrade a Specific Package

Alternatively, you can update just a single package by including the name in the following command syntax:

# apt upgrade [package-name]

Example #4: Update and Upgrade in a Single Step

You can combine the update and upgrade steps in the following command. Note that the -y flag ensures that the command bypasses the confirmation step and directly moves to the execution phase:

# apt update && sudo apt upgrade -y

Example #5: Perform a Full System Upgrade

The apt tool can execute a more comprehensive package upgrade, potentially removing packages to resolve complex dependency changes. This is a useful command to work with packages usually at the end of a distribution’s life cycle.

The command syntax is as follows:

# apt full-upgrade

Example #6: Install New Packages with apt install

You can install a new package in Debian-based distributions with the following command:

# apt install [package-name]

apt install mlocate

Remember to update and upgrade your system before installing new software to ensure compatibility and access to the latest versions.

Example #7 Download Packages Without Installation

apt allows for downloading .deb files without immediate installation. This is useful for manual inspection and offline use. The command is as follows:

# apt download [package-name]

apt download ftp

Example #8: Removing Packages with apt remove

You can easily uninstall a package from the system with the following command:

# apt remove [package-name]

apt remove ftp

The command will ask for your confirmation before executing the removal.

Example #9: Remove Configuration and Residuals

While apt remove deletes the package, it may not remove residual configuration files. These files can cause hard-to-trace errors that can disrupt system operations. We recommend the apt purge to remove both the package and its configuration files:

# apt purge [package-name]

apt purge ftp

Example #10: Clear Unused Dependencies

When you remove a package, the process often ignores the packages that were installed as dependencies. Overtime, this clutters the system with unused packages that take up space and do not contribute to its performance. 

We recommend clearing up these dependencies with the following command:

# apt autoremove

Example #11: Simultaneously Install and Remove Packages

apt supports adding and removing packages in one command using + or suffixes. The command syntax on this scenario is as follows:

# apt install [package-name]+ remove [another-package-name]-

Example #12: List Packages with apt list

You can easily list all available, installed, or upgradeable packages on the system with the following command. Note that we have piped the output into more to make it more manageable.

# apt list | more

apt list

Example #13: List Only Installed or Upgradeable Packages

You can narrow down the list of packages to only the installed and upgradable with the following commands:

# apt list --installed

# apt list --upgradable

apt list --installed

Example #14: Finding Package Dependencies

You can find a package dependencies with the following command: 

# apt depends [package-name]

apt depends ftp

Example #15: Search for Packages

You can easily find packages by keyword in names and descriptions. For this, run the following command: 

# apt search [search-term]

Example #16: Limit the Search to Package Names Only

The previous search command can take time because it tries to match the search term with the package name and description. However, you can save time by searching only the packages’ names with the following command:

# apt search --names-only [search-term]

Example #17: View Detailed Package Information

For in-depth details about a package, such as dependencies and installation size from package management tools on the command-line tool:

# apt show [package-name]

apt show ftp

Also Read: lsof Command in Linux with Examples

Also Read: A Guide on How to Install and Use Linux Screen Command in 2024

Conclusion

This comprehensive guide on the apt command simplifies the complexity of package management, package managers, package lists & software packages in Debian-based Linux distributions. 

Whether updating, upgrading, installing, or removing packages, apt streamlines these processes, ensuring users have a reliable and efficient means to manage their system’s software. 

At RedSwitches, we take pleasure in assisting our valued customers with their unique server needs. Our range of customizable bare metal servers is designed to provide you with reliable server infrastructure for your projects. We understand the importance of competitive pricing, and that’s why we offer dedicated server options at affordable rates. When you choose us, you can expect a seamless experience as we prioritize swift delivery, typically fulfilling orders approved on the same day. Whether you require a dedicated server, a traffic-friendly 10Gbps dedicated server, or a high-performance bare metal server, we are here to be your trusted hosting partner.

FAQs

Q. What is the apt command in Linux?

The apt command, short for Advanced Package Tool, is a powerful package management utility used in Debian-based Linux distributions. It simplifies the process of managing software by automating the installation, upgrade, and removal of packages and their dependencies.

Q. How do I update the package list using the apt command?

You can use the apt update command to update the list of available packages and their versions. This command refreshes your local package index with the latest information from the repositories.

Q. What is the difference between apt update and apt upgrade?

apt update updates the package index, ensuring you have the latest information on available packages and versions. apt upgrade, on the other hand, actually upgrades the installed packages to the latest versions based on the updated index.

Q. How can I install a new package using the apt command?

To install a new package, use apt install package_name, replacing “package_name” with the actual name of the package you wish to install. This command automatically installs the package and its dependencies.

Q. Can I remove a package using the apt command? How?

Yes, to remove a package, use the command apt remove package_name. This removes the package but may leave behind configuration files. To remove the package and its configuration files, use apt purge package_name.

Q. What does apt full-upgrade do?

The apt full-upgrade command performs an upgrade that may remove installed packages if necessary to upgrade the system as a whole. This is more comprehensive than an apt upgrade, which avoids removing packages.

Q. How do I search for packages using the apt command?

To search for packages, use apt search keyword, replacing “keyword” with a term related to the package you are looking for. This command searches the package descriptions and names for your keyword.

Q. Is it possible to download a package without installing it using the apt command?

Yes, you can download a package without installing it by using apt download package_name. This command downloads the .deb file to the current directory.

Q. How can I list all installed packages using the apt command?

To list all installed packages, use apt list –installed. This command displays a list of all packages installed on your system.

Q. What should I do if I want to clean up unused packages and dependencies?

To remove unused packages and their dependencies, use apt autoremove. This command cleans up packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed.

Try this guide with our instant dedicated server for as low as 40 Euros