Replicate Installed Package Selections From One Ubuntu System To Another

Published:
Last modified:

Overview

This guide shows how to restore all packages used in an Ubuntu instance into another one, to have the same programs available.

Having a backup of the software used is a good practice to easily replicate your environments accross multiple machines.

Here I will show you a method using apt-mark.

apt-mark can be used as a unified front-end to set various settings for a package, such as marking a package as being automatically/manually installed or changing dpkg selections such as hold, install, deinstall and purge which are respected e.g. by apt-get dselect-upgrade or aptitude.

There are two kinds of packages that are of interest for this purpose:

  • automatically installed packages: packages will be removed when no more manually installed packages depend on these packages
  • manually installed packages: prevents packages from being automatically removed if no other packages depend on them.

In other terms: manual packages are those installed by us, auto packages are those which came already installed or are dependencies of other packages.

1. List packages using apt-mark

apt-mark handles various settings for packages. We can separate our backup files in two files:

  1. One will hold the packages installed automatically
  2. Other file will hold the packages we installed manually
apt-mark showauto
showauto is used to print a list of automatically installed packages with each package on a new line. All automatically installed packages will be listed if no package is given. If packages are given only those which are automatically installed will be shown.
apt-mark showmanual
showmanual can be used in the same way as showauto except that it will print a list of manually installed packages instead.

So we create these two files:


$ apt-mark showauto > pkgs_auto.lst
$ apt-mark showmanual > pkgs_manual.lst

2. Restoring

Then at the target machine, with the same or a newer Ubuntu version, we copy the above file lists and restore the packages to add them to the that instance:


$ sudo apt install $(cat pkgs_auto.lst)
$ sudo apt install $(cat pkgs_manual.lst)

For example for the the manually added packages in the new environment:

sudo apt install $(cat pkgs_manual.lst)
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package xpdf is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

Package musescore is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

Package libfluidsynth1 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Unable to locate package google-chrome-stable
E: Package 'libfluidsynth1' has no installation candidate
E: Unable to locate package linux-headers-4.20.0-042000
E: Couldn't find any package by glob 'linux-headers-4.20.0-042000'
E: Unable to locate package linux-headers-4.20.0-042000-generic
E: Couldn't find any package by glob 'linux-headers-4.20.0-042000-generic'
E: Unable to locate package linux-image-unsigned-4.20.0-042000-generic
E: Couldn't find any package by glob 'linux-image-unsigned-4.20.0-042000-generic'
E: Unable to locate package linux-modules-4.20.0-042000-generic
E: Couldn't find any package by glob 'linux-modules-4.20.0-042000-generic'
E: Package 'musescore' has no installation candidate
E: Unable to locate package python-gtk2
E: Package 'xpdf' has no installation candidate

As you can see, some packages may not be possible to install in the new environment due to multiple possible reasons (outdated apt sources, or obsolete packages) so they won’t be available at first, you will have to filter the list by hand until everything goes smooth.

Conclusions

I find particularly useful to have a list of the manual packages at hand, so I can install them in any other computer without missing any functionality int that other environment.

Changelog

  • 2021-03-03:
    • working solution, tested with Ubuntu 20.04
    • fixed the way to install the packages in the new system
    • removed the alternative dpkg method

References

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


How to get a list of all installed packages on an Ubuntu server and install them in another machine.

Clutter-free software concepts.
Translations English Español

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·