Debian

Why you need apt-get clean options?

All system administrators and most of the regular Ubuntu users prefer to use apt-get for installing and upgrading packages but do not pay attention towards cleaning their system afterward. This article will explain why you need to regularly clean up your system and how to do it via apt-get clean command.

Note that, we have run the commands and procedure mentioned in this article on a Ubuntu 18.04 LTS system.

Why we need apt-get clean?

In Ubuntu and Debian operating systems, while using apt-get for installation or up-gradation of packages, the installer packages are downloaded from the repositories to local storage in your system and installed from there. The downloaded packages are in .deb format and they are saved in var/cache/apt/archives/ directory. The system keeps these installer packages in local storage even after you uninstall them. Generally, when we install a package using apt-get, the following happens:

  • It checks if the required package needs a dependency package; what are those packages and whether they are installed or not.
  • Download the package and its dependencies.
  • Install the package.

All the packages and their dependencies are kept in the local storage at /var/cache/apt/archives directory. Also, if you upgrade the package, its new version is similarly downloaded and kept on the local storage. Generally, once you installed or upgraded the packages, you do not need the .deb installer files anymore. It’s a good idea to remove these files as they are taking your system space.

To check the size of .deb files occupying the space, you can execute the following command in your Terminal:

$ du -sh /var/cache/apt/archives

Using apt-get clean

The apt-get clean command helps to clean out the cache once you have installed the packages using apt-get install command in your system. It removes the files that are no longer required but are still residing on your system and keeping the system space.

The apt-get command removes the retrieved .deb installer files and clears out the /var/cache/apt/archives leaving only the files in the lock and the partial directory.

If you view the /var/cache/apt/archives directory using the ls command as follow, you will see number of files with .deb extension.

$ sudo ls /var/cache/apt/archives

Now run the clean operation as follows:

$ sudo apt-get clean

At this moment, the cache will be cleared and you will see only the lock file and partial directory.

Note that, the apt-get clean does not uninstall the files from the system; it only removes the files from the cache.

Apt-get autoclean

Similar to apt-get clean, another command apt-get autoclean also cleans the local repository of retrieved packages but only for the packages that you have uninstalled or those with no newer versions available. It performs the cleanout operation for such packages that are still on the system and no longer needed.

If you run the apt-get autoclean, you will see the .deb files that are removed.

Similar to apt-get clean, apt-get autoclean does not uninstall the package form the system,

Difference between apt-get clean and apt-get autoremove

Some users often get confused between the apt-get clean and apt-get autoremove when performing the cleanup operation in their system. They two commands are not same and have absolutely different functions. Apt-get clean or apt-get autoclean removes the retrieved packages from the local cache only while the apt-get autoremove removes the unneeded packages that were once installed as a dependency. So when you uninstall a package, theses dependencies are of no use. Therefore, it is recommended to run the autoremove command to remove all those dependencies too.

Cleaning up the system should be part of your system’s regular maintenance in order to avoid unnecessary clutter taking up your disk space. Among many other cleanup tools, apt-get clean is one of the useful tools that can help you to remove cache files that exist in your system and taking disk space.

About the author

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.