How to Upgrade Ubuntu from 20.04 to 22.04#

High-Level checklist:

  • create backup

  • check available disk space

  • disable third-party packages

  • update packages for the current release

  • set new release

  • update packages for new release

  • re-enable third-party packages

Remember to create your backup and check the available disk space.

Consider cleaning up your kernels. During the upgrade update-initramfs will be run for every kernel installed. Not having to run it could save some time. 🙂

Now configure the system to prompt as little as possible, then run the first update. If there were kernel updates, reboot.

cp /etc/dpkg/dpkg.cfg /etc/dpkg/dpkg.cfg.bak
cat <<'EOF' >> /etc/dpkg/dpkg.cfg
force-confold
force-confdef
EOF

export DEBIAN_FRONTEND=noninteractive
export APT_LISTCHANGES_FRONTEND=none
apt-get update && apt-get -yf dist-upgrade && apt-get -yf --purge autoremove
reboot

Upgrade your distribution.

# http://changelogs.ubuntu.com/meta-release
cat <<'EOF' > /tmp/get_next_lts_release.py
#!/usr/bin/env python3
# encoding: utf-8
import sys

RELEASES = 'xenial bionic focal jammy'.split()

current_codename = sys.argv[1]
idx = RELEASES.index(current_codename)
print(RELEASES[idx+1])
EOF

current_codename=$(lsb_release -cs)
next_codename=$(python3 /tmp/get_next_lts_release.py $current_codename)
echo "$current_codename --> $next_codename"

git init /etc/apt
cd /etc/apt
git add .
git commit -m "backup for $current_codename"
for f in sources.list.d/*.list; do
  mv $f $f.save
done
git add sources.list.d/
git commit -m 'disable third-party sources'

sed -i.bak "s/$current_codename/$next_codename/g" /etc/apt/sources.list
git add sources.list*
git commit -m "update to $next_codename"

export DEBIAN_FRONTEND=noninteractive
export APT_LISTCHANGES_FRONTEND=none
apt-get update && apt-get -yf dist-upgrade && apt-get -yf --purge autoremove

git add .
git commit -m "updated to $next_codename"
reboot

Repeat the above step as many times as needed to get to the latest LTS version.

Fix third-party sources:

cd /etc/apt/sources.list.d/
for f in *.save; do mv $f $(basename $f .save); done
vi -p *.list
git add .
git commit -m "re-enable third-party sources for $(lsb_release -cs)"
apt-get update && apt-get -yf upgrade

Restore previous dpkg settings

cp /etc/dpkg/dpkg.cfg.bak /etc/dpkg/dpkg.cfg