Is there any way do apt-get clean after apt-get install automatically, like in other distros?
- 1,897
- 4
- 29
- 43
- 594
- 2
- 10
- 26
-
Thanks to @Braiam I found a bug in Ubuntu and I'll report it. In Debian Braiam's solution work fine. – Tiago Carrondo Dec 12 '13 at 13:23
-
related: [How do I free up disk space?](http://askubuntu.com/q/5980) – rubo77 Sep 12 '14 at 01:23
-
1@TiagoCarrondo Can you share the bug link please? – Ken Sharp Jan 01 '15 at 18:23
-
In Xenial all my machines clean themselves. I don't know where this is configured but it works. – Ken Sharp Jun 22 '18 at 08:30
5 Answers
You just need to add it at the apt configurations files. Just run:
sudo sh -c "echo 'DSELECT::Clean "always";' >> /etc/apt/apt.conf.d/99AutomaticClean"
This will trigger automatic clean each time you do upgrade.
Lets explain this entry, from the man page:
CleanCache Clean mode; this value may be one of always, prompt, auto, pre-auto and never. always and prompt will remove all packages from the cache after upgrading, prompt (the default) does so conditionally. auto removes only those packages which are no longer downloadable (replaced with a new version for instance). pre-auto performs this action before downloading new packages.
More info:
https://groups.google.com/d/msg/linux.debian.user/aK2jvfL_tuw/rUd6i6bd4YQJ
- 66,947
- 30
- 177
- 264
-
I guess it should need a semicolon at the end `;`! and therefore it should be `sudo sh -c "echo 'APT::Get::Clean=always;' >> /etc/apt/apt.conf.d/99AutomaticClean"` – Indian Dec 12 '13 at 12:34
-
@Braiam I've tried it and it doesn't work. I even tried APT::Get::Clean "always" like in other files but no luck. any clue? – Tiago Carrondo Dec 12 '13 at 13:01
-
-
@TiagoCarrondo how about now? [This usenet message](https://groups.google.com/d/msg/linux.debian.user/aK2jvfL_tuw/rUd6i6bd4YQJ) says that is the winner. – Braiam Dec 12 '13 at 13:11
-
I've tried that already after your first post. Going to change file location... wait – Tiago Carrondo Dec 12 '13 at 13:15
-
let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/11928/discussion-between-tiago-carrondo-and-braiam) – Tiago Carrondo Dec 12 '13 at 13:18
-
-
The most direct way (that I know of) to achieve the desired effect here is with the "keep-downloaded-packages" option:
- Create a new apt config file, e.g.
/etc/apt/apt.conf.d/99custom-conf
- Add the following to the file.
# Remove downloaded .deb files after installation by apt-get
APT::Keep-Downloaded-Packages "false";
Starting the file name with '99' means this config file is read last (and will overwrite any other configuration). Then by setting the 'keep-downloaded-packages' option BOOL value to false, apt-get will automatically delete a packages .deb file from the /var/cache/apt/archives/ directory, immediately after installation (just like 'apt clean').
Note that apt itself uses a separate binary directive for the same option, and per this apt NEWS entry, since Nov 2016 (so at least Debian 9 (Stretch)) this has been set to "false" by default. If (for whatever reason) this is not the case for your system, you can append the below to your config file.
# Remove downloaded .deb files after installation by apt
Binary::apt::APT::keep-downloaded-packages "false";
NOTE: I cannot speak to other desktop environments, but KDE Plasma's Discover software center is not affected by either of these options, and so any package installations or upgrades conducted through Discover will keep the cached .deb files, and you will need to run apt clean to remove these.
For a convenient index of all apt configuration directives/options, open "/usr/share/doc/apt/examples/configure-index.gz OR JUST configure-index (in later versions)".
- 121
- 1
- 1
- 5
Write a shell script!
sudo nano /usr/bin/aptinstaller
Inside this file type:
#!/bin/bash
sudo apt-get install $1
sudo apt-get -y autoclean
sudo apt-get -y autoremove
Save and exit from nano and type:
sudo chmod +x /usr/bin/aptinstaller
Now everytime you would type
sudo aptinstaller <package-name>
It would install and then clean.
- 1,560
- 5
- 22
- 39
My workaround, though not a real solution, is to set the archives directory to /tmp. It won't be cleaned automatically after an install but it will be cleaned on reboot.
I created /etc/apt/apt.conf.d/99clean and added:
Dir::Cache::archives /tmp;
Alternatively you could mount /var/cache/apt/archives using tmpfs, though that will obviously use up RAM and I wouldn't recommend that option.
In /etc/fstab, for example:
tmpfs /var/cache/apt/archives tmpfs size=128m,mode=755 0 0
- 955
- 8
- 30
A hack appears to be adding the following to e.g. /etc/apt/apt.conf.d/clean (via):
DPkg::Post-Invoke {"/bin/rm -f /var/cache/apt/archives/*.deb || true";};
This will clear the cache automatically after dpkg has been invoked by apt.
This does not clean the cache after apt-get update though, the hook APT::Update::Post-Invoke might be used for this.
The best method still appears to be calling apt-get clean / aptitude clean manually.