FUNDAMENTALS A Complete Guide for Beginners
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.
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…
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.
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.
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.
Similar Readings
- The “apt-get” Command in Linux [10 Practical Examples]
- The “firewall-cmd” Command in Linux [7 Practical Examples]
- The “finger” Command in Linux [6 Practical Examples]
- The “env ” Command in Linux [9 Practical Examples]
- The “enable” Command in Linux [6 Practical Examples]
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.
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.
Similar Readings
- The “time” Command in Linux [4 Practical Examples]
- The “tty” Command in Linux [4 Practical Examples]
- The “uptime” Command in Linux [5 Practical Examples]
- The “vmstat” Command in Linux [6 Practical Examples]
- The “uname” Command in Linux [11 Practical Examples]
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.
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.
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.
Similar Readings
- The “free” Command in Linux [8 Practical Examples]
- The “getent” Command in Linux [11 Practical Examples]
- The “groupadd” Command in Linux [7 Practical Examples]
- The “addgroup” Command in Linux [7 Practical Examples]
- The “groups” Command in Linux [6 Practical Examples]
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.
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.
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.
Similar Readings
- The “groupmod” Command in Linux [5+ Practical Examples]
- The “id” Command in Linux [7+ Practical Examples]
- The “service” Command in Linux [6 Practical Examples]
- The “sestatus” Command in Linux [4 Practical Examples]
- The “shutdown” Command in Linux [7 Practical Examples]
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.
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.
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.
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.
Similar Readings
- The “dmesg” Command in Linux [7+ Practical Examples]
- The “install” Command in Linux [6+ Practical Examples]
- The “reboot” Command in Linux [3 Practical Examples]
- The “sync” Command in Linux [8 Practical Examples]
- The “chage” Command in Linux [7 Practical Examples]
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.
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.
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.
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.
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