Debian

How to upgrade Debian 11 (bullseye) to Debian 12 (bookworm) safely

This tutorial explains the procedure for upgrading Debian 11 (bullseye) to Debian 12 (bookworm) in a few simple steps.

Before you start with the upgrade, backup all critical data. That’s especially the config data in the /etc directory and the user data in /home. When you run a server, then you should include the databases (MySQL, PostgreSQL, etc), web, and email data in the backup as well. The backup should be stored on an external hard disk, on an external server e.g., by FTP or on a cloud drive. Here are a few examples of how to create backups with tar.

Backup the configuration and data

Configuration files in the /etc directory.

tar -pczf etc.tar.gz /etc

Backup of the /var/www website directory.

tar -pczf www.tar.gz /var/www

Backup of the /home directories.

tar -pczf home.tar.gz /home


Backup of the /usr/local/directadmin directory.

tar -pczf direct_admin.tar.gz /usr/local/directadmin

Backup MySQL databases.

mysqldump --defaults-file=/etc/mysql/debian.cnf -cCeQ --hex-blob --quote-names --routines --events --triggers --all-databases -r all_databases.sql

Then move the tar.gz and .sql files for safe storage on a backup drive.

Check the apt sources.list file
We will install all available bullseye updates first before we upgrade to bookworm. On some systems, the package source is defined as “stable” in the sources.list file instead of “bullseye” or “bookworm”. To avoid an accidental early upgrade to bookworm, please check the sources.list now and ensure that it contains “bullseye” and not “stable” as the source:

Use a command line editor like nano or vi to open /etc/apt/sources.list. The lines should be similar to the ones below:

nano /etc/apt/sources.list

# deb http://asi-fs-d.contabo.net/debian bullseye main

deb http://asi-fs-d.contabo.net/debian bullseye main non-free contrib
deb-src http://asi-fs-d.contabo.net/debian bullseye main non-free contrib

deb http://security.debian.org/debian-security bullseye-security main contrib non-free
deb-src http://security.debian.org/debian-security bullseye-security main contrib non-free

# bullseye-updates, to get updates before a point release is made;
# see https://www.debian.org/doc/manuals/debian-reference/ch02.en.html#_updates_and_backports
deb http://asi-fs-d.contabo.net/debian bullseye-updates main contrib non-free
deb-src http://asi-fs-d.contabo.net/debian bullseye-updates main contrib non-free
## These are official Contabo mirrors providing faster updates for your server ##
## (the files from the official servers are synced to it) ##

Update the packages for Debian 11
The following commands will install all pending bullseye updates. This ensures that your system is in good shape for the update to bookworm.

apt-get update

Ready for the first upgrade:

apt-get upgrade

Follow this with:

apt-get dist-upgrade

Check the package state to ensure that no packages are on hold or in a half-installed state
This test is important, we will check the package state to ensure that no packages are on hold or in a half-installed state. Your system and apt database must be in good shape before we proceed with the dist upgrade. If there are any broken or “on hold” packages, then fix these issues before the upgrade.

Check that no packages are on hold by querying the package database with the dpkg command:

dpkg --audit
dpkg --get-selections | grep hold

When both commands did not return any packages, proceed with the upgrade.

Update the sources.list for Debian 12
Edit the /etc/apt/sources.list file again:

nano /etc/apt/sources.list

and replace its content with the following lines:

# deb http://asi-fs-d.contabo.net/debian bullseye main

deb http://asi-fs-d.contabo.net/debian bookworm main non-free contrib
deb-src http://asi-fs-d.contabo.net/debian bookworm main non-free contrib

deb http://security.debian.org/debian-security bookworm-security main contrib non-free
deb-src http://security.debian.org/debian-security bookworm-security main contrib non-free

# bullseye-updates, to get updates before a point release is made;
# see https://www.debian.org/doc/manuals/debian-reference/ch02.en.html#_updates_and_backports
deb http://asi-fs-d.contabo.net/debian bookworm-updates main contrib non-free
deb-src http://asi-fs-d.contabo.net/debian bookworm-updates main contrib non-free
## These are official Contabo mirrors providing faster updates for your server ##
## (the files from the official servers are synced to it) ##

Save the file and run the following command to update the sources database:

apt-get update

Check if packages are upgradable
We can test with the apt command if the installed packages are upgradeable. Run this command to perform the test:

apt list --upgradable

Debian 11 (bullseye) to Debian 12 (bookworm) Update in two steps
It is recommended to do the upgrade in two steps, first, run “apt-get upgrade” to install the base packages and then run “apt-get dist-upgrade” to do the actual distribution upgrade,

Start with the update by running this command:

apt-get upgrade

Next, we will do the distribution upgrade by running:

apt-get dist-upgrade

A reboot is required to finish the upgrade and load the new kernel:

reboot

Check the update
To check which Debian version is currently installed on the system, look at the file /etc/os-release.

cat /etc/os-release

The result of a Debian bookworm system is:

PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"