Raspberry Pi OS: Dare to upgrade InPlace from Bullseye to Bookworm
Raspberry Pi OS follows the Debian version jumps. The official upgrade requires a new installation. However, inplace upgrades are possible.
(Image: Rowan Morgan/Shutterstock.com)
If you want to upgrade from older Raspbian or Raspberry Pi OS versions based on Debian Bullseye (Debian 11), for example, to the newer Bookworm base (Debian 12) with longer support, you can do this easily by reinstalling the operating system. However, anyone using self-written or customized scripts and software is faced with the problem of having to set everything up again. Due to a lack of documentation for DIY solutions or simply forgetfulness, this is sometimes an impossible task.
In such cases, Raspberry Pi users can try to upgrade the existing installation to the new version with better and, above all, longer support using an InPlace upgrade. This procedure is not without danger; if you simply change the package sources to Bookworm and attempt an upgrade, you are very likely to destroy the system. There are several instructions on the Internet for a functioning upgrade. The article documents the commands and sequence that successfully led to our goal. Nevertheless, the explicit note: This upgrade variant is expressly not the officially recommended one and it can fail!
Backup is mandatory!
Therefore, a backup of the previously used software versions is mandatory in the first place. To do this, shut down the Raspi as planned using sudo shutdown -h now, which should prevent any inconsistencies from occurring in databases or file systems, for example. The contents of the SD card can be transferred to an IMG file under Windows using the Win32Imager tool, for example. Under Linux, this is done with the command sudo dd if=/dev/sd<letter> of=~/raspi-backup.img, where the SD device corresponds to the SD card. In the event of an error, the old system can be quickly restored and an executable state restored.
Videos by heise
The actual migration can be carried out with a little patience and a few commands on the command line. First, the running Raspberry Pi OS must be updated to the current version. This is done with the commands sudo apt-get update && sudo apt-get dist-upgrade.
Start upgrade
In the package sources, the "Bullseye" entry must now be changed to "Bookworm". You can either edit the files manually or do this conveniently with the commands
sudo sed -i -e 's/bullseye/bookworm/g' /etc/apt/sources.listsudo sed -i -e 's/bullseye/bookworm/g' /etc/apt/sources.list.d/raspi.list
The upgrade itself is carried out with the commands
sudo apt updatesudo apt -y full-upgradesudo apt -y cleansudo apt -y autoremove
These can also be concatenated using "&&", so that after the upgrade has been installed, the cleanup is also carried out immediately. In between, there are queries that are not intercepted by this, for example regarding the replacement of previously changed configuration files. In the test, the replacement was not a problem even on a system that had grown over the years –, for example the SSH or users files gave cause for concern here, but logging in via SSH was still possible with the accounts set up after the update. Depending on the Internet connection, the update now takes a few hours.
During the test, one or two packages in the sources were apparently already updated during this time, resulting in error messages. However, the error messages usually also state a command that can be used to remedy the situation. In our case, it helped to start again with sudo apt update. Don't worry, the packages that have already been updated will not be downloaded and touched again, the process was much faster.
If the upgrade has now largely been completed without any further error messages, a first reboot is now required. This is done with sudo reboot on the command line.
Cleaning up and firmware and kernel updates
After logging in after the reboot, sudo apt purge ?config-files cleans old configuration files from the system. The commands
sudo dpkg --purge --force-depends raspberrypi-kernel raspberrypi-bootloadersudo umount /bootsudo fsck -y /bootsudo mkdir /boot/firmwaresudo sed -i.bak -e "s#boot#boot/firmware#" /etc/fstabsudo systemctl daemon-reloadsudo mount /boot/firmwaresudo apt install raspi-firmware
remove the old firmware from the system, reformat the boot partition and install the current firmware there. The next step is to install the current kernel. This depends on which Raspberry Pi you are using. The v6 kernel should run on all devices, the v7l version from Raspberry 3b and the 64-bit v8 version from Raspberry Pi 4.
The safe version is therefore
sudo apt install linux-image-rpi-v6 linux-headers-rpi-v6
If you are using a newer Raspi, you can use the newer v7l kernel:
sudo apt install linux-image-rpi-v7l linux-headers-rpi-v7l
while the 64-bit Raspis run optimally with this kernel:
sudo apt install linux-image-rpi-v8 linux-headers-rpi-v8
Finally, the configuration file for booting requires an entry to activate the automatic search for an initramfs file –, a new feature since the Bookworm-based release from 2023:
sudo sed -i.bak '$ a\auto_initramfs=1' /boot/firmware/config.txt
Another reboot starts the new kernel:
sudo reboot
The Raspberry Pi should now run with the new Bookworm-based Raspberry Pi OS. Old scripts should also continue to work, even cron jobs are retained. In our scripts, the call of the Python interpreter still had to be adapted. The reference in the first line of the script requires the explicit naming of the version, such as
#!/usr/bin/env python3
Warning messages from apt
The keys for the repositories that the software administration accesses with "apt" are now organized differently. Previously they were all in one file, /etc/apt/trusted.gpg. Now there is one key file per repository under /etc/apt/trusted.gpg.d/. apt may therefore issue warning messages. These could be resolved on the test system as follows.
First, the keys for the standard repositories had to be downloaded and imported into the key management:
wget http://raspbian.raspberrypi.org/raspbian.public.key -O - | sudo gpg --no-default-keyring --keyring gnupg-ring:/etc/apt/trusted.gpg.d/raspbian.gpg --import
wget https://archive.raspberrypi.org/debian/raspberrypi.gpg.key -O - | sudo gpg --no-default-keyring --keyring gnupg-ring:/etc/apt/trusted.gpg.d/archive-raspbian.gpg --import
The next step is to clean up the old key file. The call of
sudo apt-key list
shows the keys contained in the repositories. The old keys, which now replace the two downloaded and imported keys, are deleted by the command
sudo apt-key del "<number>"
where the numbers are the long strings displayed by the list command for raspbian.gpg and archive-raspbian.gpg. The key for Bookworm/InRelease remains in the old key file. This is moved by calling
sudo mv /etc/apt/trusted.gpg /etc/apt/trusted.gpg.d/bookworm_InRelease.gpg
to the correct location and give it the correct name. Finally, the rights and owners for the key files must be assigned appropriately. For the individual files in the directory, these are therefore
sudo chown _apt:root /etc/apt/trusted.gpg.d/raspbian.gpgsudo chmod 644 /etc/apt/trusted.gpg.d/raspbian.gpg
This procedure must be repeated for the other two files – and any other repositories, if you have entered any –. Afterwards, apt instructions work without warning messages on our system.
However, if other complicated problems occur for which no solution can be found even after searching the net, the backup is still available, which can be used to quickly restore the old, executable status.
(dmk)