Cover image for Installing and Configuring Ubuntu-20.04 on WSL2

Installing and Configuring Ubuntu-20.04 on WSL2

Words 951
Views
Visitors

Timeline

Timeline

2025-12-29

init

This article introduces the detailed steps for installing and configuring Ubuntu-20.04 in a WSL2 environment, including downloading a WSL instance, migrating it to the D drive, and restoring user permissions. It also discusses how to manually build the latest version of Emacs using specific compilation options, and summarizes configuration references for updating development tools and dependencies such as gcc, QEMU, clangd, git, python, and bear.

Download Ubuntu via WSL

1
2
3
4
# View available images for download
wsl --list --online
# Download Ubuntu 20.04
wsl --install Ubuntu-20.04

Migrate to D drive

Create a directory on the D drive to store the new WSL, e.g., D:\WSL\Ubuntu-20.04

1
2
3
4
5
6
7
# 1. Export its backup (e.g., named Ubuntu.tar)
wsl --export Ubuntu-20.04 D:\WSL\Ubuntu-20.04.tar
# 2. Unregister the original one
wsl --unregister Ubuntu-20.04
# 3. Restore the backup file to D:\WSL\Ubuntu-20.04
wsl --import Ubuntu-20.04 D:\WSL\Ubuntu-20.04 D:\WSL\Ubuntu-20.04.tar
# Now you can delete Ubuntu-20.04.tar

If you start WSL at this point, it seems to have returned to normal, but the user has become root. Use the following command to restore it:

1
Ubuntu2004 config --default-user username

Install the latest version of Emacs

You can install a newer gcc compiler

1
2
3
4
5
6
7
8
9
10
11
12
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update

sudo apt install gcc-13 g++-13

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 130
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 130

# To change it later, use the following command
sudo update-alternatives --config gcc

Compile Emacs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Optional: Uninstall snapd
sudo apt-get purge snapd
# Compile tree-sitter
git clone https://github.com/tree-sitter/tree-sitter.git
cd tree-sitter
git checkout release-0.25
make
make install PREFIX="$(pwd)/_install"
# Fix the prefix in .pc files
emacs _install/lib/pkgconfig/tree-sitter.pc
prefix=/home/zhaohang/repository/tree-sitter/_install

# Set environment variables in ~/.bashrc or ~/.zshrc
export PKG_CONFIG_PATH="$HOME/repository/tree-sitter/_install/lib/pkgconfig:$PKG_CONFIG_PATH"

# Download Emacs source code
wget https://mirrors.ustc.edu.cn/gnu/emacs/emacs-30.2.tar.xz
cd emacs-30.2
sudo apt install cmake build-essential
sudo apt install zlib1g-dev pkg-config libgnutls28-dev libncurses-dev libjansson-dev libvterm-dev libgccjit-$(gcc -dumpversion)-dev


export LD_LIBRARY_PATH="$HOME/repository/tree-sitter/_install/lib:$LD_LIBRARY_PATH"

./configure \
--without-x \
--without-ns \
--without-pgtk \
--with-native-compilation \
--with-modules \
--with-threads \
--with-tree-sitter \
--prefix=/usr/local \
LDFLAGS="-Wl,-rpath,$LD_LIBRARY_PATH -L$LD_LIBRARY_PATH"

make -j$(nproc)
sudo make install
# Test after compilation, it should output: RUNPATH /home/zhaohang/repository/tree-sitter/_install/lib:
objdump -p /usr/local/bin/emacs | grep RUNPATH
  • --without-xemacs-nox, does not use X11 (Xorg), can only use emacs -nw (terminal)
  • --without-nsDisable macOS Cocoa / NS window system, just to prevent configure from automatically detecting macOS GUI
  • --without-pgtkDisable Pure GTK (the new GUI backend under Wayland)
  • --with-native-compilationEnable native-comp (.eln) to compile Elisp into machine code
  • --with-modulesEnable dynamic module support, requires libjansson-dev
  • --with-threadsEnable multi-threading support
  • --with-tree-sitterEnable tree-sitter
  • --prefix=/usr/localInstall to /usr/local

emacs configuration reference:

qemu latest version compilation reference

Additionally, the clangd version downloaded via apt is relatively old and may not support certain configuration items; you can install a newer version with the following command

1
2
3
4
5
6
7
8
9
10
11
12
wget https://apt.llvm.org/llvm.sh
chmod u+x llvm.sh
sudo ./llvm.sh 21
sudo apt-get install clang-format-21

# Install clang to the alternatives system
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-21 100
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-21 100
sudo update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-21 100
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-21 100
# Install libstdc++-10
sudo apt install libstdc++-$(gcc -dumpversion)-dev

git version is relatively low, does not support-bparameter, you can add apt source to update

1
2
3
4
sudo add-apt-repository ppa:git-core/ppa -y
sudo apt update
sudo apt upgrade
sudo apt install git -y

python version is 3.8, some software may depend on version 3.9 and above:

1
2
3
4
5
6
7
8
9
10
11
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.9 python3-pip

# Register python3.9, set priority to 2
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
# Register python3.8, set priority to 1, so the priority is lower than python3.9
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1

# Select default version
sudo update-alternatives --config python3

bear can also use the latest version; the old version of bear is a python script with lower efficiency, while the new version is a binary written in rust

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Download rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# cargo 换源 visit https://mirrors.tuna.tsinghua.edu.cn/help/crates.io-index.git/
rustup update
# Compile bear
git clone https://github.com/rizsotto/Bear.git
cd bear
cargo build --release

sudo install -m 755 target/release/bear /usr/local/bin/
# For Debian based systems
export INSTALL_LIBDIR=lib/x86_64-linux-gnu
sudo mkdir -p /usr/local/libexec/bear/$INSTALL_LIBDIR
sudo install -m 755 target/release/libexec.so /usr/local/libexec/bear/$INSTALL_LIBDIR/

WSL2 Ubuntu20.04
WSL2 Ubuntu20.04

Reference