When using the Ubuntu system, we often need to install software and update the system. But did you know that during software installation, the system temporarily stores downloaded installation packages in a place called the “cache”? Over time, these cache files may occupy a significant amount of disk space and even affect the system’s running speed. Today, we’ll discuss how to use the apt clean command to clear these caches and keep the system running smoothly.
一、What is APT Cache?¶
First, let’s briefly understand the role of “cache”. Ubuntu uses the apt (Advanced Package Tool) utility to manage software package installation, updates, and removals. When you use apt install to install software, the system downloads the corresponding .deb installation package from the software source (e.g., Ubuntu’s official repositories) and temporarily stores it in a local cache directory. This allows the system to directly read from the local cache for subsequent installations of the same software, avoiding repeated downloads and improving installation speed.
Cache files are stored by default in the /var/cache/apt/archives/ directory. This directory contains various software installation packages (.deb files) and related metadata.
二、Why Clean APT Cache?¶
While the cache speeds up installations, failing to clean it regularly leads to two main issues:
1. Disk Space Occupation: If you’ve installed many software packages or system updates, the cache can accumulate several gigabytes or more of space.
2. System Clutter: The cache may contain outdated installation packages of uninstalled software, occupying space unnecessarily.
At this point, cleaning the cache becomes a necessary system maintenance task.
三、Clearing Cache with apt clean¶
apt clean is a command in Ubuntu specifically designed to completely clear the APT cache. It deletes all downloaded .deb installation packages and related files in the /var/cache/apt/archives/ directory, including outdated and unnecessary packages.
How to Use apt clean?¶
- Open the Terminal: Use the shortcut
Ctrl+Alt+Tto open the terminal directly, or search for “Terminal” in the application menu. - Execute the Clean Command: In the terminal, enter
sudo apt clean.
-sudo: Indicates that administrator privileges are required (since clearing the cache requires modifying system files).
-apt clean: The command itself, used to clear the cache. - Enter Your Password: After executing the command, the system will prompt you to enter your password (note: no characters will be displayed while typing the password; simply enter the correct password and press Enter).
- Wait for Completion: The command may display some progress prompts during execution, but there will be no obvious feedback upon completion (unless there’s an error).
四、Other Cache-Cleaning Commands (Brief Comparison)¶
In addition to apt clean, there are several commonly used cache-related commands. It’s important to understand their differences:
-
apt autoclean
- More “gentle” thanapt clean, it only clears outdated installation packages in the cache (e.g., when a newer version is available in the software source, the old version cache is removed). It does not delete all cached files.
- Usage:sudo apt autoclean(similar toclean). -
apt autoremove
- Note: This does not clear the cache. Instead, it removes packages that are no longer dependencies of other software (e.g., temporary dependencies installed when a software was first installed; these dependencies become useless after the main software is uninstalled).
- Usage:sudo apt autoremove(only handles dependencies, not cache files). -
apt cleanvsapt autoclean
-apt clean: Completely clears the entire cache directory (excluding temporary files in/var/cache/apt/archives/partial/).
-apt autoclean: Only clears outdated installation packages, keeping the most recent versions (suitable for scenarios where you want to clean old packages without deleting all cached files).
五、Precautions¶
- Clearing Cache Does Not Affect Installed Software:
apt cleanonly deletes downloaded installation packages and does not remove the installed software itself. Thus, you don’t need to worry about software malfunctions. - Regular Cleaning is Healthier: It is recommended to run
apt cleanonce a month (or whenever you receive a disk space warning) after prolonged system use. - Manually Check Cache Size: If you want to know the cache size, run
du -sh /var/cache/apt/archives/to check the occupied space before deciding to clean.
六、Summary¶
apt clean is an efficient tool for clearing APT caches in Ubuntu. With a simple command, it can free up significant disk space and keep the system running smoothly. Remember that administrative privileges (sudo) are required to execute the command, and the system remains unaffected by cleaned cache files. For more granular cache management, combine autoclean or autoremove as needed. Regular cache maintenance ensures a faster and more stable system!