How to Migrate Installed Debian Packages to Another System: A Complete Guide for Developers


6 views

The most efficient way to transfer your Debian package configuration is by generating a list of installed packages. Here's the standard command:

dpkg --get-selections > package_list.txt

This creates a file containing all explicitly installed packages, including their selection state (install/hold/purge). For a more developer-focused approach that excludes dependencies, use:

apt-mark showmanual > manual_packages.txt

Copy the generated file to your target system using your preferred method:

scp package_list.txt user@new-system:/tmp/

Or for containerized environments:

docker cp package_list.txt container_name:/tmp/

First update your package database:

sudo apt update

Then install using the transferred list:

sudo xargs -a package_list.txt apt install

For more control over the installation process (recommended for production systems):

sudo dpkg --set-selections < package_list.txt
sudo apt-get dselect-upgrade

For development environments where package versions matter:

apt list --installed > versioned_packages.txt

Then create an installation script:

#!/bin/bash
while read -r line; do
    pkg=$(echo $line | cut -d'/' -f1)
    version=$(echo $line | awk '{print $2}')
    sudo apt-get install "${pkg}=${version}"
done < versioned_packages.txt

If you encounter unavailable packages:

sudo apt --fix-broken install

For architecture-specific problems:

dpkg --print-architecture
dpkg --print-foreign-architectures

The first step is to generate a list of currently installed packages. We'll use dpkg, Debian's package management tool:

# Get all explicitly installed packages (excluding dependencies)
dpkg --get-selections | grep -v deinstall > package_list.txt

# Alternative: Get ALL installed packages (including dependencies)
dpkg -l | grep ^ii | awk '{print $2}' > full_package_list.txt

On your new Debian system, first update the package database:

sudo apt update
sudo apt upgrade -y

There are several approaches to install packages from your list:

# Method 1: Using xargs with apt
xargs -a package_list.txt sudo apt install -y

# Method 2: Using a while loop
while read pkg; do
  sudo apt install -y "$pkg"
done < package_list.txt

You might encounter some common problems:

# Skip unavailable packages
while read pkg; do
  if apt-cache show "$pkg" >/dev/null 2>&1; then
    sudo apt install -y "$pkg"
  else
    echo "Package $pkg not available - skipping"
  fi
done < package_list.txt

For more complex scenarios, consider these approaches:

# Create a meta-package with equivs
sudo apt install equivs
equivs-control mypackages.control
# Edit the control file to include your packages
equivs-build mypackages.control
sudo dpkg -i mypackages.deb

# Alternative: Use aptitude for better dependency resolution
sudo apt install aptitude
aptitude install $(cat package_list.txt)

After installation, verify the results:

# Compare installed packages between systems
comm -3 <(dpkg --get-selections | cut -f1 | sort) \
         <(ssh source-system "dpkg --get-selections | cut -f1" | sort)