Categories
Development Linux Ubuntu

Snapshot and rollback of apt-installed packages

Is there a way to take a snapshot of the packages installed on a Debian/Ubuntu system, and to roll back to that snapshot later?

My use case is the following: I’d like to write a script that will build a program with large set of build dependencies. I’d like to apt install those dependencies at the start of the script. Some of those dependencies may already be installed, in which case some of the installations will be-no ops. After building the program, I’d like to get the system back in the state it was in before the build, i.e. I’d like to uninstall the things my script installed, but otherwise leave things untouched.

In other words, I want to roll apt back to a snapshot!

I’m imagining something like:

magic-command-that-writes-apt-snapshot my-packages.txt
apt install gcc texlive fortran75 cobol60 qbasic fftw 
./configure && && make && make install
magic-command-that-rollsback-to-apt-snapshot my-packages.txt

Maybe this is possible by parsing /var/log/apt/history.log but that seems very fragile.

Some specifics that might make things simpler or more complicated:

  • I’d like a general solution that is robust against the initial state of the machine, but I can guarantee my script will contain exactly one apt install command (not several, not dist-upgrade, not build-deps).
  • In this case, I’m not worried about packages that the apt command upgrades. That’s a benign side-effect for my purposes. The rollback should not uninstall them, but it’s fine if it doesn’t downgrade them.
  • The script needs to remain non-interactive (it happens to run in a docker build).
  • I want to avoid “collateral damage”. In particular, I can’t assume packageA should be uninstalled simply because because packageB was installed by the script, packageB depends on packageA, and packageA has no dependents other than packageB. Maybe packageA was already installed! (This requirement is the reason this question is not a dupe of questions like this, if I understand correctly.)

Leave a Reply

Your email address will not be published. Required fields are marked *