Debian

23 Fundamental apt-get and apt-cache Commands You Need to Manage Your System

The “apt-get” and “apt-cache” commands are very frequently used commands in the Linux environment. The former command mainly allows you to install, update, and remove packages, whereas the latter lets you search for packages and their related information. Both of these commands have their specific use cases, however, most of the users are not aware of their exact capabilities except for their basic usage. Therefore, today we have decided to share with you the twenty-three different examples where you can use “apt-get” and “apt-cache” commands to manage your system.

Fundamental apt-get Command Examples

The seventeen fundamental “apt-get” commands are described below.

Example 1: Update your Linux System

This is the most common usage of the “apt-get” command within the Linux environment since you need to update your system before installing any new application. At times, even before troubleshooting your system for any errors, you are required to update it so that any broken links or dependencies can be fixed beforehand.

To update your Linux system with the “apt-get” command, you need to run it in the following manner:

sudo apt-get update

Example 2: Upgrade your Linux System

After updating your system, you are presented with a message about the number of all those packages that can be upgraded, i.e., packages whose upgraded versions are available. Therefore, essentially the next step after updating your Linux system is to upgrade it, and this can be done by running the “apt-get” command in the following manner:

sudo apt-get upgrade

As compared to the “apt-get update” command, the “apt-get upgrade” command takes a longer time to execute as it has to install the upgraded versions of all those packages whose upgrades are available.

Example 3: Install a New Package on your Linux System

If you are a frequent computer user, then you have to install new packages on your system on a daily basis. You can also do this via the Linux GUI, however, if you prefer to use the CLI, then that is also possible.

You can conveniently install a new package on your Linux system by running the “apt-get” command in the following manner:

sudo apt-get install PackageName

Here, you have to replace the “PackageName” with the actual name of the package that you wish to install. We wanted to install the VLC media player for a demonstration, which is why we have replaced the “PackageName” with “vlc”.

Example 4: Delete a Package from your Linux System

When you have a large bulk of packages installed on your Linux system, then you would have seen the message of “storage space running out” very frequently. In this situation, the only solution you can come up with is to remove all the unused or irrelevant packages from your system.

This can easily be done by running the “apt-get” command in the following manner:

sudo apt-get remove PackageName

Here, you have to replace the “PackageName” with the actual name of the package that you wish to remove. We wanted to remove the VLC media player for a demonstration, which is why we have replaced the “PackageName” with “vlc”.

Example 5: Delete a Package along with its Configuration Files from your Linux System

The simple “apt-get remove” command merely removes the specified package from your Linux system. However, the configuration files of that package still reside on your system. If you also want to get rid of those configuration files along with the desired package, then you can run the “apt-get” command in the following manner:

sudo apt-get purge PackageName

Here, you have to replace the “PackageName” with the actual name of the package that you wish to remove, along with its configuration files. We wanted to remove the VLC media player for demonstration, which is why we have replaced the “PackageName” with “vlc”.

Example 6: Delete all the Dependencies of a Deleted Package from your Linux System

At times, a single package on Linux requires multiple other packages to function properly. These packages are known as the dependencies of the said package, and they are installed along with that package. However, when you remove a package, either with the “apt-get remove” command or with the “apt-get purge” command, these dependencies are not removed automatically. Therefore, to remove these unused dependencies for freeing up your system’s space, you can run the “apt-get” command in the following manner:

sudo apt-get autoremove

This command will explore your whole Linux system in search of all those packages and dependencies that are no longer needed and will remove them altogether.

Example 7: Smartly Upgrade your Linux System

The simple “apt-get upgrade” command upgrades all the available packages on your Linux system without even checking if upgrading a particular package is required or not. If you want to perform the up-gradation process smartly, then you can run the “apt-get” command in the following manner:

sudo apt-get dist-upgrade

Another feature of this command is that it not only upgrades the packages smartly but also removes those packages and dependencies that are no longer needed.

Example 8: Perform a Linux System Clean up

If you want to clean up all the temporary files from your Linux system, then you can use the “apt-get” command in the following manner:

sudo apt-get clean

This command will remove all the temporary files from your Linux system, hence freeing up your disk space.

Example 9: Find out the Changes in a Package by Checking the Log

Whenever you install an application or a package on your Linux system and start using it, you make certain changes to it. If you want to take a look at all those changes before you upgrade any desired package, then you can run the “apt-get” command in the following manner:

sudo apt-get changelog PackageName

Here, you have to replace the “PackageName” with the actual name of the package whose changelog you want to view before upgrading it. We wanted to view the changelog of the VLC media player for demonstration, which is why we have replaced the “PackageName” with “vlc”.

Example 10: Clean all your Linux System’s Local Repository Packages

At times, some .deb files keep residing in your Linux system’s local repository even if they are no longer installed. That is why it is always recommended to get rid of these files as soon as possible to reclaim your disk space.

To clean all your Linux system’s local repository packages, you can run the “apt-get” command in the following manner:

sudo apt-get autoclean

Example 11: Download a Package on your Linux System

Sometimes, you only wish to download a package on your Linux system without installing it. This may be required when you need to install this package at a later time or if you want to keep a downloaded backup copy of a particular package. Therefore, to download a package on your Linux system without installing it, you can run the “apt-get” command in the following manner:

sudo apt-get download PackageName

Here, you have to replace the “PackageName” with the actual name of the package that you want to download without installing it on your Linux system. We wanted to download the VLC media player for demonstration, which is why we have replaced the “PackageName” with “vlc”.

Example 12: Look for Broken Packages or Dependencies on your Linux System

At times, some installed dependencies or packages on your Linux system might get corrupted. We refer to such packages or dependencies as “broken”. Therefore, if you wish to find out all the broken packages or dependencies on your Linux system, then you can run the “apt-get” command in the following manner:

sudo apt-get check

Example 13: Fix the Broken Packages or Dependencies on your Linux System

Once you have found out all the broken packages or dependencies on your Linux system, the next step is to fix them all, so you can easily proceed with further installations.

You can fix all your broken packages or dependencies on your Linux system by running the “apt-get” command in the following manner:

sudo apt-get –f install

Example 14: Access the Manual Pages of the “apt-get” Command

If you ever wish to know more details about the “apt-get” command, then you can access its man pages in the following manner:

man apt-get

Example 15: Access the Help Pages of the “apt-get” Command

If you want to know the syntax and options that are available for the “apt-get” command, then you can access its help pages for this purpose.

To access the help pages of the “apt-get” command, you can run this command in the following manner:

apt-get --help

Example 16: Reinstall a Package on your Linux System

Sometimes, an installed package on your Linux system might not be performing correctly. Such issues can arise for several reasons, out of which the most important reason is its improper installation. In such a situation, you might think of reinstalling an already installed package.

This can be done by running the “apt-get” command in the following manner:

sudo apt-get install --reinstall PackageName

Here, you have to replace the “PackageName” with the actual name of the package that you want to reinstall on your Linux system. We wanted to reinstall the VLC media player for demonstration, which is why we have replaced the “PackageName” with “vlc”.

Example 17: Download the Source Code of a Package on your Linux System

If you just wish to download the source code of a package without installing it on your Linux system, then you can run the “apt-get” command in the following manner:

sudo apt-get --download-only source PackageName

Here, you have to replace the “PackageName” with the actual name of the package whose source code you want to download without installing it on your Linux system. We wanted to download the source code of the VLC media player for a demonstration, which is why we have replaced the “PackageName” with “vlc”.

Fundamental apt-cache Command Examples

The six fundamental “apt-get” commands are described below.

Example 18: List all the Available Packages on your Linux System

Generally, when you start using a Linux based system, initially, you are clueless about which packages you essentially need to install. At times, you are not even aware of the exact package names. Therefore, it is always considered very helpful if you can somehow list down all the installable packages.

To list all the available packages on your Linux system, you can use the “apt-cache” command in the following manner:

sudo apt-cache pkgnames

This command will list down the names of all the packages that are available for your Linux system.

Example 19: List the Detailed Information about a Package on your Linux System

At times, you might want to know all the details regarding a particular package for the sake of knowing its compatibility and other stuff like that.

To list the detailed information about a package on your Linux system, you can run the “apt-cache” command in the following manner:

sudo apt-cache showpkg PackageName

Here, you have to replace the “PackageName” with the actual name of the package whose detailed information you want to list down on your Linux system. We wanted to list down the detailed information about the VLC media player for demonstration, which is why we have replaced the “PackageName” with “vlc”.

Example 20: List all the Basic Information about a Package on your Linux System

The above-mentioned command list down all the possible details about any particular package. However, if you only wish to list down the basic information about a package, then you can run the “apt-cache” command in the following manner:

sudo apt-cache show PackageName

Here, you have to replace the “PackageName” with the actual name of the package whose basic information you want to list down on your Linux system. We wanted to list down the basic information about the VLC media player for a demonstration, which is why we have replaced the “PackageName” with “vlc”.

Example 21: Search for some Specific Packages via Keyword Search on your Linux System

Sometimes, you do not remember the exact name of a package, but you still wish to search for it. All you remember is a specific keyword that is a part of the name of your package. In this scenario, you can still perform a keyword search to look for all those packages, which contain the specified keyword.

To do this, you can run the “apt-cache” command in the following manner:

sudo apt-cache search “Keyword”

Here, you have to replace the “Keyword” with the actual keyword contained in the name of the package that you want to look for on your Linux system. We wanted to search for all those packages that contain the “server” keyword in their names for demonstration, which is why we have replaced the “Keyword” with the “server”.

Example 22: Search for a Specific Package by Mentioning its Name on your Linux System

If you remember the exact name of a package, then you can simply look for it on your Linux system by mentioning its exact name.

You can search for a specific package on your Linux system by running the “apt-cache” command in the following manner:

sudo apt-cache search PackageName

Here, you have to replace the “PackageName” with the actual name of the package that you want to look for on your Linux system. We wanted to search for the VLC media player for a demonstration, which is why we have replaced the “PackageName” with “vlc”.

Example 23: List Cache Statistics on your Linux System

If you wish to know the overall cache statistics of your Linux system, i.e., the total number of packages, types of packages, etc. then you can run the “apt-cache” command in the following manner:

sudo apt-cache stats

Conclusion

In this article, we provided you with a very detailed tutorial on the usage of the “apt-get” and “apt-cache” commands. Most of the users are familiar with the “apt-get” command, but they do not have any clue about the usage of the “apt-cache” command. After going through this tutorial, they will be in a very good position to use the “apt-cache” command as smoothly as they would have used any other basic Linux command. Moreover, the usage of the “apt-get” command is also explained in-depth in this article, as seventeen out of our twenty-three examples were dedicated to this purpose. That is why, after going through this article, anyone can master the usage of the “apt-get” and “apt-cache” commands.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.