
A Simple Guide to Backing Up and Restoring Your Ubuntu Packages
Imagine this scenario: you’ve spent months, or even years, perfecting your Ubuntu setup. You have all the right applications, development tools, and libraries installed. Now, you need to set up a new machine, reinstall your operating system, or recover from a system failure. The thought of manually finding and reinstalling every single package is daunting.
Fortunately, there’s an elegant and powerful way to save a complete list of your installed packages and use it to restore your exact software environment on a fresh Ubuntu installation. This process not only saves you an immense amount of time but also ensures consistency across your machines.
Step 1: Create a Backup of Your Installed Packages
The first step is to generate a list of all currently installed packages on your source system. This is easily accomplished with a single command in your terminal.
Open a terminal window and run the following command:
dpkg --get-selections > packages.list
Let’s break down what this does:
dpkg --get-selections: This is the core command that queries the Debian Package Manager (dpkg) for a list of all packages and their current state (e.g., install, hold, deinstall).>: This is a standard output redirector. It takes the output from the command on the left and writes it into the file on the right.packages.list: This is the plain text file where your package list will be saved. You can name it anything you like.
After running this command, you will have a file named packages.list in your current directory. It is crucial to save this file to a safe, external location, such as a USB drive, cloud storage (like Dropbox or Google Drive), or a version control repository like GitHub. This file is the key to restoring your system.
Step 2: Restore Your Packages on a New Ubuntu System
Once you have a fresh Ubuntu installation ready, you can use your packages.list file to automatically install all your previous applications.
Before you begin, make sure you have your packages.list file accessible on the new machine.
Add Necessary Repositories (If Applicable)
If you used any custom PPAs (Personal Package Archives) or third-party repositories for certain software, you must add them to your new system before proceeding. The package list only contains package names; it doesn’t store the repository information. If you skip this step,aptwon’t be able to find any packages that aren’t in the official Ubuntu repositories.Update Your Package List
First, ensure your new system’s package manager is up to date with the latest information from its configured repositories.sudo apt-get updateSet the Package Selections
Next, you need to load your saved list into the new system’s package manager. This tellsdpkgwhich packages should be installed.sudo dpkg --set-selections < packages.listThe
<operator does the opposite of>—it feeds the contents of thepackages.listfile as input to thedpkg --set-selectionscommand.Install the Packages
At this point, you’ve only told the system what you want installed; nothing has actually been downloaded or installed yet. The final step is to execute the installation process.sudo apt-get dselect-upgradeThis command instructs
apt-getto look at the list of selections made bydpkgand install anything marked for installation while intelligently handling dependencies. The process may take some time depending on the number of packages and your internet speed.
An Alternative Method: Backing Up Manually Installed Packages
Sometimes, you may not want to back up every single package, including all the automatic dependencies. Instead, you might only want a list of the packages you explicitly installed yourself. This creates a cleaner, more minimal backup.
To generate a list of only manually installed packages, use this command:
apt-mark showmanual > manual-packages.list
To restore from this list, the process is slightly different and arguably simpler. On your new system, run:
sudo apt-get install $(cat manual-packages.list)
This command reads the contents of manual-packages.list and feeds them directly to apt-get install, which will then install those packages and pull in their required dependencies automatically.
Final Thoughts
Mastering this backup and restore process is a vital skill for any serious Ubuntu user or system administrator. It transforms the tedious task of setting up a new machine into a simple, automated procedure. By taking a few seconds to save your package list, you can ensure a fast, accurate, and stress-free migration or recovery, allowing you to get back to work with your familiar software environment in no time.
Source: https://www.tecmint.com/backup-restore-ubuntu-packages-dpkg/


