Debian APT Package Download Cache Configuration
Debian's Advanced Package Tool (APT) stores downloaded
.deb packages in a local cache before installing them. By
modifying or creating configuration snippets in
/etc/apt/apt.conf.d/, administrators can control whether
packages are retained after installation, adjust the cache directory
location, or enforce size and age limits on stored archives.
The
Primary Retention Setting:
APT::Keep-Downloaded-Packages
The primary directive controlling whether downloaded
.deb files remain on the system after installation is
APT::Keep-Downloaded-Packages.
To retain packages in the cache:
APT::Keep-Downloaded-Packages "true";To automatically delete packages after installation:
APT::Keep-Downloaded-Packages "false";
In modern Debian releases, the command-line utility apt
defaults to deleting packages (false), whereas
apt-get historically defaults to keeping them
(true). To enforce a uniform policy across all package
management tools, create a dedicated file such as
/etc/apt/apt.conf.d/01cache containing your preferred
directive.
Changing the
Download Directory: Dir::Cache::Archives
By default, APT stores packages in
/var/cache/apt/archives/ and incomplete downloads in
/var/cache/apt/archives/partial/. You can redirect this
storage path to an alternative filesystem or mount point using the
following configuration:
Dir::Cache::Archives "/path/to/custom/cache/";
Ensure the destination directory exists and has appropriate read and
write permissions for the _apt and root
users.
Controlling Automatic Cache Cleanup
For systems using background maintenance scripts (such as
apt-daily.service or unattended upgrades), APT supports
parameters that limit cache expansion based on time and disk
consumption:
APT::Archives::MaxAge: Sets the maximum number of days a package file can stay in the cache. Files older than this value are removed during automated cleanup runs.APT::Archives::MaxAge "30";APT::Archives::MinAge: Prevents the deletion of packages that were downloaded more recently than the specified number of days, regardless of cache size limits.APT::Archives::MinAge "2";APT::Archives::MaxSize: Sets a target maximum size (in megabytes) for the cache. If the directory exceeds this limit, older packages are purged until the size falls back beneath it.APT::Archives::MaxSize "500";
Completely Disabling Disk Caching
In containerized environments (like Docker) or environments with extreme storage constraints, you can prevent APT from using disk storage for archives entirely by directing the archive storage to an empty string:
Dir::Cache::Archives "";
APT::Keep-Downloaded-Packages "false";
This prevents APT from committing .deb files to
permanent storage, saving disk space at the expense of needing to
redownload files if a package reinstallation is required.