The “apt” Command in Linux [13+ Practical Examples]

The apt command in Linux stands for “Advanced Package Tool”. It is a command-line interface for managing the packages on a user’s system. You can perform several actions including installation, update, and removal of packages with this command. In this article, you will find the most common uses of the apt command in Linux with practical examples.

Table of Contents Expand

A. Description

The apt command in Linux provides an interactive interface for the user. You must use the sudo command along with apt to get root permissions while managing packages on your machine. These actions are achieved by combining apt with the corresponding commands.

B. Syntax

The syntax for the apt command is quite easy to follow. At first, you will need to type the apt command. Then write the command for your desired action and then the name of the package/s.

apt command pkg…
Note: In the above syntax pkg followed by 3 dots represents that multiple packages can be utilized simultaneously.

C. Option

Many useful options are available for the apt command. I have listed some of the most used ones here. However, you can look at the man page to learn more.

man apt

Useful Options

  • -d/–download-only: Downloads a package but does not install.
  • –only-upgrade: Upgrades a specific package.
  • –installed: Returns only installed packages.
  • –upgradeable: Returns only upgradeable packages.
  • -y: Answers “yes” to prompt without interruption.
Note: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

D. Commands

The Linux distribution provides a list of commands for the package management system. These commands are applied along with the apt command in Linux. Below are the most frequently used package managing commands. You can find more details on the man page of this command.

man apt

Useful Commands

  • autoremove: Removes packages currently not needed.
  • edit-sources: Allows editing of sources.list files.
  • full-upgrade: Upgrades the system as a whole.
  • install: Installs packages.
  • list: Displays a list of packages.
  • purge: Removes leftover configuration files.
  • search: Searches for mentioned package.
  • show: Displays information about the mentioned package.
  • remove: Removes packaged data.
  • reinstall: Reinstalls packages.
  • update: Downloads package information from all configured sources.
  • upgrade: Installs available upgrades of all packages currently installed.
Note: These commands in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “apt” Command in Linux

The apt command in Linux provides an end-user interface for the package management system. You can handle packages from the command line using this tool. Here are some practical examples of managing packages using the apt command in Linux.

Example 1: Installing New Packages Using the “apt” Command in Linux

Using the apt command in Linux you can install new packages in your system. In this example, I will show you the installation of the ncal package using the apt command. You can follow the steps below to do the same.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt install ncal

➌ Enter your Password.

❹ Now, hit ENTER.

Output >

In the given image, you can see that I have installed the ncal package on my machine.Installing packages using apt command in linux.


Similar Readings


Example 2: Reinstalling Packages Using the “apt” Command in Linux

Using the apt command in Linux you can reinstall existing packages in your system. In this example, I will show you the process of reinstalling the ncal package using the apt command.

Steps to Follow >

➊ At first go to the Ubuntu Terminal.

➋ Write the following command in the command prompt:

sudo apt reinstall ncal

➌ Enter your Password.

❹ Now, press ENTER.

Output >

In the image below, you can see that I have reinstalled the ncal package on my machine.Reinstalling sudo apt reinstall ncal

Example 3: Removing Packages Using the “apt” Command in Linux

You can remove an existing package from your system using the apt command in Linux with the command remove. In this example, I will illustrate removing the ncal package from my machine.

Steps to Follow >

➊ At first, launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt remove ncal

➌ Enter your Password.

❹ Hit ENTER.

❺ Press Y.

Output >

In the following image, you can see that I have removed the ncal package from my system.Removing sudo apt reinstall ncal


Similar Readings


Example 4: Removing Configuration Files Using the “apt” Command in Linux

Using the remove command with apt usually leaves the small configuration files on your system. You can get rid of these unwanted configuration files using the apt with the purge command. You will be able to remove all the leftover configuration files at once as well as files remaining from a specific package. To learn about the two different applications of the command follow the cases below.

Case A: Removing All The Leftover Configuration Files Using the “apt” Command in Linux

To remove all the unwanted configuration files follow the steps below.

Steps to Follow >

➊ At first go to the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt purge

➌ Enter your Password.

❹ Now, hit ENTER.

Output >

In this image, you can see that the purge command searched for all the unwanted files on my machine. Since no file has left, it counted 0 files for removal.Removing all the leftover configuration files using apt command in linux.

Case B: Removing Leftover Configuration Files of a Certain Package Using the “apt” Command in Linux

To remove the configuration file of a certain package try the given steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt purge ncal

➌ Type your Password.

❹ Hit ENTER.

Output >

In this image, you can see that I am removing the ncal package using the purge command. It removes the package along with all the configuration files.Removing specific package configuration file using apt command in linux.

Example 5: Downloading Package Information Using the “apt” Command in Linux

You can download available package information from the command line using the apt with the command update. In this example, I will download available package information on my machine. To do so you can follow the steps below.

Steps to Follow >

➊ At first launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt update

➌ Enter your Password.

❹ Hit ENTER.

Output >

In the given image, you can see that I have downloaded all the available package information on my terminal.Downloading package information using apt command in linux.


Similar Readings


Example 6: Upgrading Packages Using the “apt” Command in Linux

Using the apt command in Linux you can upgrade installed packages in your system. To do so you will need to combine the command upgrade. You can upgrade either all the available packages or a specific package. By following the examples below, you will be able to achieve any of the desired actions.

Case A: Upgrading All the Existing Packages Using the “apt” Command in Linux

To upgrade every package on your system follow the given steps.

Steps to Follow >

➊ At first go to the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt upgrade

➌ Enter your Password.

❹ Now, hit ENTER.

Output >

In this image, you can see that I have upgraded all the available packages on my machine.upgrading all packages using apt command in linux.

Case B: Upgrading a Specific Package Using the “apt” Command in Linux

In this example, I will show you upgrading only the ncal package on my machine. You can do the same by following the steps below.

Steps to Follow >

➊ At first go to the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt install ncal --only-upgrade

➌ Enter your Password.

❹ Now, press ENTER.

Output >

In the given image, you can see that I have only upgraded the ncal package on my machine.Upgrading specific package using apt command in linux.

Note: The update command applied with apt will not update the packages on your system. Rather it will only download the available package information. On the contrary, the upgrade command can be used for updating packages.

Example 7: Upgrading Full System Using the “apt” Command in Linux

Using the apt command in Linux you can upgrade the system as a whole. The full-upgrade command may remove installed packages and install updated packages. In this example, I will upgrade my full system. You can follow the steps below to do the same.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt full-upgrade

➌ Enter your Password.

❹ Press ENTER.

Output >

In the following image, you can see that I have upgraded the system using the apt command.Full system upgrade using apt command in linux.


Similar Readings


Example 8: Listing Available Packages Using the “apt” Command in Linux

You can display a list of packages on your Terminal using the apt command in Linux. For this, you will need to use the command list as well. You can display all the available packages, installed packages, and upgradeable packages separately. Follow the examples below to learn about the separate applications of these commands.

Case A: Listing All the Available Packages Using the “apt” Command in Linux

To list all the available packages on the machine follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt list

➌ Enter your Password.

❹ Now, hit ENTER.

Output >

In the image below, you can see that I have listed all the available packages on my machine.Listing all packages using apt command in linux.

Case B: Listing Installed Packages Using the “apt” Command in Linux

To display a list of installed packages on your system follow the given steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt list --installed

➌ Enter your Password.

❹ Now, hit ENTER.

Output >

In this image, you can see that I have displayed the installed packages on my machine.Listing all installed using apt command in linux.

Case C: Listing Upgradeable Packages Using the “apt” Command in Linux

To list the upgradeable packages on your system follow the given process.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt list --upgradeable

➌ Enter your Password.

❹ Now, hit ENTER.

Output >

In this, you can see that I have listed all the upgradeable packages on my machine.Listing all upgradeable packages using apt command in linux.

Example 9: Searching for a Package Using the “apt” Command in Linux

You can search for a specific package from the command line using the apt with the command search. In this example, I will search for the ncal package on my system. To do so you can follow the steps below.

Steps to Follow >

➊ At first launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt search ncal

➌ Enter your Password.

❹ Hit ENTER.

Output >

In this image, you can see that I have searched for the ncal package. The command returned every package that has the string “ncal” in its name along with the ncal package.

Searching for a package Full-system upgrade using apt command in linux.


Similar Readings


Example 10: Displaying Package Information Using the “apt” Command in Linux

You can display information about a package from the command line using the apt with the command show. In this example, I will show information about the ncal package. You can follow the steps below to do the same.

Steps to Follow >

➊ Open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt show ncal

➌ Give your Password.

❹ Hit ENTER.

Output >

In the given image, you can see that I have shown the ncal package information on my Terminal.Display package information using apt command in linux.

Example 11: Downloading a Package Using the “apt” Command in Linux

You will be able to download a package using the apt command in Linux. The download command used for this task only downloads the package on your machine. In this example, I will illustrate downloading the ncal package. You can follow the given steps to do the same.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt install --download-only ncal

➌ Type your Password.

❹ Press ENTER key.

Output >

In the image below, you can see that I have downloaded the ncal package from my Terminal.Download the 'ncal' package using apt.

Example 12: Automatically Removing Installed Packages  That are No Longer Needed

Using the apt command in Linux you can remove packages that are no longer needed. These packages are generally installed to satisfy dependency with other packages. In this example, I will remove unnecessary packages using the apt command. You can follow the steps below to do the same.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command line:

sudo apt autoremove

➌ Enter your Password.

❹ Now, hit ENTER.

Output >

In the given image, you can see that I have removed the unnecessary packages from my machine.Automatically remove unwanted packages using apt command in linux.

Example 13: Editing the “Source.list” File Using the “apt” Command in Linux

The apt command in Linux allows you to edit the “source.list” file. To do so you will need to run the command edit-source. In this example, I will open my “source.list” file on a text file editor. You can achieve the same result by following the process below.

Steps to Follow >

➊ At first launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt edit-sources

➌ Enter your Password.

❹ Hit ENTER.

Output >

In the image below, you can see that I have opened the “source.list” file on the nano editor. The command allows editing of this file.Command for editing source.list file .Editing source.list file using apt command in linux.

Conclusion

In this article, I have presented the most frequent applications of the apt command in Linux. You will find more commands and options associated with the apt on its man page. However, these practical examples of the apt command should aid your engagement with Linux and make you an expert in the command line.


Similar Readings

Rate this post
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment