How to Upgrade Debian 8 (Jessie) to 9 (Stretch) safely

This tutorial explains the procedure to upgrade Debian 8 to 9 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

For ISPConfig 3 users: Backup of your email server files.

tar -pczf vmail.tar.gz /var/vmail

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 Jessie updates first before we upgrade to Jessie. On some systems, the package source is defined as "stable" in the sources.list file instead of "jessie" or "stretch". To avoid an accidental early upgrade to Stretch, please check the sources.list now and ensure that it contains "jessie" and not "stable" as 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://httpredir.debian.org/debian jessie main contrib non-free
deb http://httpredir.debian.org/debian jessie-updates main contrib non-free
deb http://security.debian.org jessie/updates main contrib non-free

Update the packages for Debian 8

The following commands will install all pending Jessie updates. This ensures that your system is in a good shape for the update to Stretch.

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 half installed state

This test is important, we will check the package state to ensure that no packages are on hold or in half installed state. Your system and apt database must be in a 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, then proceed with the upgrade.

Update the sources.list for Debian 9

Edit the /etc/apt/sources.list file again:

nano /etc/apt/sources.list

and replace its content with the following lines:

deb http://httpredir.debian.org/debian stretch main contrib non-free
deb http://httpredir.debian.org/debian stretch-updates main contrib non-free
deb http://security.debian.org stretch/updates main contrib non-free

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 apt command if the installed packages are upgradable. Run this command to perform the test:

 apt list --upgradable

Debian 8 to 9 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, take a look at the file /etc/os-release.

cat /etc/os-release

The result on a Debian Stretch system is:

Debian updated to version 9

Till Brehm

About Till Brehm

Over 20 years experience as Software Developer and Linux System Administrator. Till Brehm is the founder and lead developer of the ISPConfig Hosting Control Panel software (since 2000) and he founded HowtoForge in 2005 as a place to share Linux knowledge with other Linux enthusiasts.

Share this page:

Suggested articles

12 Comment(s)

Add comment

Comments

By: diablo666 at: 2018-01-22 14:52:02

Works with ispconfig installed right?

Something to do for that?

By: Ben at: 2018-01-23 01:37:24

Some optional cleanup steps, before editing sources...

apt-get --purge remove `deborphan`  (purge some unneeded packages - repeat until nothing is removed )

apt-get --purge autoremove  ( purge auto-installed packages)

aptitude search "~o"  (to find obsolete packages, which might be worth removing)

By: Tomas at: 2018-01-23 10:27:16

Will this upgrade the kernel as well? It looks like it does not.

By: till at: 2018-01-23 11:43:04

Yes, a dist upgrade will upgrade the Linux Kernel,

By: Grant at: 2018-01-23 16:00:20

I would add one step. After doing the update, but before the upgrade do "apt-get dist-upgrade -d"  Having everything downloaded and local before you start can be a significant benefit, not dealing with network issues during an upgrade.

By: peter3332 at: 2018-02-13 16:54:53

Will this work over 2 versions from debian 7 to 9  or i shoud do 7 -> 8 -> 9 ?

By: till at: 2018-02-13 21:59:41

I won't skip a release in the upgrade process. But it might work, just haven't tested that.

By: SamTzu at: 2018-03-16 13:18:14

I would remove fail2ban before upgrading. It usually brakes during upgrade.

By: pattone at: 2018-06-14 12:50:59

What is better.... "apt-get update && apt-get upgrade"  or "apt update && apt upgrade" ??

By: toto1988 at: 2018-07-12 08:41:31

Thank you! :)

By: simon at: 2018-08-03 20:13:49

work ok 

Thank

By: Z3r0h0ur at: 2018-09-18 14:38:33

Postfix is not working after upgrade, as it's upgrading from 2.x to 3.x . Couldn't find a fix for that yet (