Re-installing packages on new Debian Linux systems is a real pain. It takes time and is tedious to get done. As a result, many users have resorted to writing complex shell scripts that automatically install apps on new systems. If you’re new to Debian, or just tired of having to list out the programs you’d like to re-install manually, there is a better way to create an app install script: using the Debian program installer to generate a script automatically.
Note: though this tutorial covers Debian, it can also work on Debian derivatives. Feel free to follow along with the information outlined below.
Exporting installed packages from Apt
There are several ways to generate a list of packages, and over the years, many different methods have been employed. However, all of these old methods involve piping multiple commands together to strip the description of each app, to make it so that just the package names appear in the list. Those methods are hardly practical so we won’t cover any of them in this tutorial. Instead, we’ll take advantage of the new Apt package manager included on Debian 9 and 10, as it comes with the apt-mark feature
The apt-mark feature is a great aspect of the Apt package manager. With it, users can use it to show a list of automatically installed apps (pre-setup by the system during installation,) as well as a list of packages that the user installed manually after the fact.
Please note that if you plan to use this feature on Debian, you need to be using a version of the operating system that has support for Apt. Apt-get will not work! So, if you’re running Debian 8, you must go through the upgrade process before beginning this guide!
The apt-mark application doesn’t just generate a list of installed packages of software installed directly from the Debian software repositories. It will also show packages installed via a downloadable DEB, or stuff from third-party software repositories. Keep that in mind, when using this tool.
Generate a list of manually installed packages
If you’re just looking to create a list of manually installed apps from various repositories, rather than generating a gigantic list of every single application that Debian Linux includes by default, the “showmanual” feature in apt-mark is for you.
To generate the list, open up a terminal by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Then, enter the command-line operation below to generate a list in the terminal prompt.
Look through the terminal prompt, and you’ll see a list of every package you’ve ever installed on Debian Linux manually. If the list is too long to read at a glance, add the “more” option with the command below.
apt-mark showmanual | more
To save this list for later, pipe it through to a text file. For example, to save all manually installed packages to the “Documents” folder, do:
apt-mark showmanual > ~/Documents/debian-packages-manually-installed.txt
Generate a list of automatically installed packages
Need to generate a list of all of the packages that were automatically installed to your Debian system during the installation? If so, you’ll need to make use of the “showauto” feature in apt-mark.
Open up a terminal window using Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Then, enter the command-line operation below to get a list of automatically installed packages on Debian.
Take a look at the generated list in the terminal window. Or, if the list of generated packages that apt-mark prints out are too much to read, consider adding the “more” command-line argument to make sorting through them easier.
apt-mark showauto | more
To save automatically installed Debian packages for later, pipe it through to a text file with the command below.
apt-mark showauto > ~/Documents/debian-packages-auto-installed.txt
To re-install the packages on the list saved from a text file, open up a terminal window. Then, follow the instructions below.
Please note that any packages that can not be re-downloaded (AKA were installed via a downloadable DEB) will not install with this method, and the Apt package manager will show an error for ones it is unable to download.
Re-install manually installed packages
To re-install all of the Debian apps on the list of manually installed packages, enter the following command into a terminal window.
sudo apt install $(cat ~/Documents/debian-packages-manually-installed.txt | xargs)
If the packages are already on the system, but you’d like to re-install them anyway, add the “–reinstall” command-line switch.
sudo apt install $(cat ~/Documents/debian-packages-manually-installed.txt | xargs) --reinstall
Re-install automatically installed packages
To re-install all of the Debian apps on the list of automatically installed packages, enter the command below into a terminal window.
sudo apt install $(cat ~/Documents/debian-packages-auto-installed.txt | xargs)
Alternatively, if you’d like to re-install stuff that is already present on the system, add the “–reinstall” command-line switch.
sudo apt install $(cat ~/Documents/debian-packages-auto-installed.txt | xargs) --reinstall