Timeline

2025-12-29

init


Download Ubuntu via WSL

1
2
3
4
# List available images
wsl --list --online
# Download Ubuntu 20.04
wsl --install Ubuntu-20.04

Migrate to D Drive

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

1
2
3
4
5
6
7
# 1. Export a backup (e.g. named Ubuntu.tar)
wsl --export Ubuntu-20.04 D:\WSL\Ubuntu-20.04.tar
# 2. Unregister the original
wsl --unregister Ubuntu-20.04
# 3. Restore the backup to D:\WSL\Ubuntu-20.04
wsl --import Ubuntu-20.04 D:\WSL\Ubuntu-20.04 D:\WSL\Ubuntu-20.04.tar
# You can now delete Ubuntu-20.04.tar

If WSL appears to start normally after this but the user has become root, restore with:

1
Ubuntu2004 config --default-user username

Install Latest Emacs

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 later, use:
sudo update-alternatives --config gcc

Build 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: remove snapd
sudo apt-get purge snapd
# Build 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 the .pc file
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
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
# After build, verify: should output RUNPATH /home/zhaohang/repository/tree-sitter/_install/lib
objdump -p /usr/local/bin/emacs | grep RUNPATH
  • --without-x: emacs-nox, no X11 (Xorg), terminal-only (emacs -nw)
  • --without-ns: disable macOS Cocoa/NS window system, just prevents configure from auto-detecting macOS GUI
  • --without-pgtk: disable Pure GTK (new GUI backend for Wayland)
  • --with-native-compilation: enable native-comp (.eln), compiles Elisp to machine code
  • --with-modules: enable dynamic module support, requires libjansson-dev
  • --with-threads: enable multi-threading support
  • --with-tree-sitter: enable tree-sitter
  • --prefix=/usr/local: install to /usr/local

Emacs configuration reference:

Latest Qemu build reference:

The clangd version from apt is relatively old and may not support some configuration options. Install a newer version with:

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

# Register clang in 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

If git version is too low and doesn’t support the -b flag, add the 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 3.8 may be too old for some software requiring 3.9+:

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, priority 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
# Register python3.8, priority 2 (higher priority than python3.9)
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2

# 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 Rust binary:

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 mirror: visit https://mirrors.tuna.tsinghua.edu.cn/help/crates.io-index.git/
rustup update
# Build 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

References