Migrate from Ubuntu 20.04 in WSL to 24.04 LTS

Published:
6 minute read

I have been using Ubuntu 20.04 in WSL for a while. Recently, I decided to upgrade it to the latest LTS version, which is 24.04 LTS. I have two options to do this: a fresh installation or an in-place upgrade using the do-release-upgrade command. ALthoguh I decided to go with the messy in-place upgrade first, I later changed my mind and went for a fresh installation, especially because I had to do two major version upgrades (20.04 -> 22.04 -> 24.04). So, anyways. Here are the steps I followed. I specifically put this post here for future reference in case I need to install all the various packages I need again.

Backup

First things, first.

Either back up your entire WSL instance using:

wsl --export Ubuntu-20.04 ubuntu-20.04-backup.tar

Or back the home dir:

cd ~
tar -czvf ubuntu-home-backup.tar.gz $HOME
mv ubuntu-home-backup.tar.gz /mnt/c/Users/YourWindowsUsername/some/dir/

Fresh Installation of Ubuntu 24.04 LTS in WSL

Install Ubuntu 24.04 LTS from Microsoft store. Open the installed instance once to finish the installation.

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=24.04
DISTRIB_CODENAME=noble
DISTRIB_DESCRIPTION="Ubuntu 24.04.3 LTS"

After installation, in powershell check the installed distros:

wsl --list --verbose

Login to the new Ubuntu 24.04 LTS instance and restore your home dir backup if you made one:

cd ~
mv /mnt/c/Users/YourWindowsUsername/some/dir/ubuntu-home-backup.tar.gz .
tar -xzvf ubuntu-home-backup.tar.gz

Check if everything is okay, especially hidden files like .bashrc, .vimrc, .ssh, etc.

Installing Packages

Now, you need to reinstall all the packages you need.

sudo apt update && sudo apt upgrade -y
sudo apt install build-essential cmake git vim wget curl htop net-tools unzip zip -y
# some of my frequently used packages:
sudo apt install ninja-build gdb valgrind -y
sudo apt install aspell tldr colordiff tree lolcat neofetch fastfetch -y
sudo apt install zenity evince -y

Other Configurations

CUDA

If you need CUDA:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb
dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt update
sudo apt install cuda-toolkit
# If you need the drivers too
sudo apt install cuda-drivers

LaTeX

I use texlive for LaTeX documents. To install it:

sudo apt install texlive-latex-recommended -y
# or 
sudo apt install texlive-latex-extra -y
# For full installation (7 GB+)
sudo apt install texlive-full -y

Pyenv

Install pyenv for managing python versions:

curl https://pyenv.run | bash

LLVM

For more info, visit here.

To, install LLVM from the official apt repository, first add the repository key and source:

wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb http://apt.llvm.org/noble/ llvm-toolchain-noble main"

Add the following lines to /etc/apt/sources.list.d/llvm-toolchain-noble.list if not already added:

deb http://apt.llvm.org/noble/ llvm-toolchain-noble main
deb-src http://apt.llvm.org/noble/ llvm-toolchain-noble main
# 20
deb http://apt.llvm.org/noble/ llvm-toolchain-noble-20 main
deb-src http://apt.llvm.org/noble/ llvm-toolchain-noble-20 main
# 21
deb http://apt.llvm.org/noble/ llvm-toolchain-noble-21 main
deb-src http://apt.llvm.org/noble/ llvm-toolchain-noble-21 main

Then install LLVM 20:

sudo apt update
sudo apt install nstall clang-20 clang-tools-20 clang-20-doc libclang-common-20-dev \
  libclang-20-dev libclang1-20 clang-format-20 python3-clang-20 clangd-20 clang-tidy-20 \
  libllvm-20-ocaml-dev libllvm20 llvm-20 llvm-20-dev llvm-20-doc llvm-20-examples \
  llvm-20-runtime libomp-20-dev lld-20

fastfetch

At the time of writing this post, fastfetch is not available for Ubuntu 24.04 and some of its docs have inconsistencies. Here is what worked for me:

sudo apt install git cmake build-essential libpci-dev libvulkan-dev libwayland-dev libxrandr-dev libxcb-randr0-dev libdconf-dev
git clone https://github.com/LinusDierheimer/fastfetch.git
cd fastfetch
# I changed the branch to master
git checkout master
mkdir build && cd build
cmake ..
cmake --build . 
sudo cmake --install .
# Now you can run fastfetch by just typing:
fastfetch
# or 
flashfetch

How to make ll (ls -alF) show the year in the timestamp:

alias ll='ls -alF --time-style=long-iso'

Post-migration

After everything is done, you can remove the old WSL instance if you made a full backup:

wsl --unregister Ubuntu-20.04

Some tips on removing old packages and cleaning up to make the backup smaller.

sudo apt autoremove --purge
sudo apt autoclean

# Check which packages are large and remove if not needed 
# This command shows the 30 largest installed packages
dpkg-query -Wf='${Installed-Size}\t${Package}\n' \
  | sort -n | tail -n 30 \
  | awk '{printf "%10.2f MB %s\n", $1/1024, $2}'

# You can search for a group of packages like this:
dpkg-query -Wf='${Installed-Size}\t${Package}\n' | grep cuda

# You can also add the sizes of the packages from previous command to get total size
dpkg-query -Wf='${Installed-Size}\t${Package}\n' | grep cuda \
  | awk '{sum += $1} END {printf "Total size: %.2f MB\n", sum/1024}'

To remvove old log files:

sudo journalctl --vacuum-time=7d
sudo truncate -s 0 /var/log/syslog
sudo truncate -s 0 /var/log/auth.log
sudo rm -f /var/log/*.gz
sudo rm -f /var/log/*.[0-9]
sudo rm -f /var/log/*.[0-9].gz
sudo rm -f /var/log/Xorg.pid-*.log

Compact in WSL

To compact the WSL virtual disk after removing old files and packages, first exit all WSL instances and then run (in powershell with admin rights):

wsl --shutdown
diskpart 
# in diskpart prompt:
select vdisk file="Path\to\vdisk\file"
# For me it is:
select vdisk file="C:\Users\amirs\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx"
compact vdisk file

I also had several versions of CUDA, LLVM, ROCM, HIP, and other packages installed that I no longer needed. With these steps, I was able to reduce the size of the storage around 250 GB to about 50 GB.

For now, I haven’t encountered any compatibility issues with Ubuntu 24.04 LTS in WSL. Everything seems to be working fine so far. If some issues arise, I will update this post accordingly.