Cover image for WSL2安装Ubuntu-20.04与配置

WSL2安装Ubuntu-20.04与配置

字数 1.2k
阅读
访客

时间轴

时间轴

2025-12-29

init

本文介绍了在WSL2中安装Ubuntu-20.04并进行环境配置的完整过程。内容包括将WSL发行版迁移至D盘以节省系统空间,以及迁移后恢复默认用户的命令。文章详细说明了从源码编译安装最新版Emacs的步骤,包括配置选项如禁用X11、启用native-comp、动态模块、多线程和tree-sitter支持等。此外,还涵盖了其他开发工具的升级与安装方法,如通过编译获取最新版QEMU、安装较新的clangd、更新git版本、升级Python至3.9以上,以及使用Rust编写的bear替代旧版Python脚本。文章旨在帮助用户在WSL2中搭建高效、可定制的开发环境。

wsl下载ubuntu

1234
# 查看可以下载的镜像wsl --list --online# 下载ubuntu20.04wsl --install -d Ubuntu-20.04

迁移到D盘

在D盘创建一个目录用来存放新的WSL,比如D:\WSL\Ubuntu-20.04

1234567
# 1. 导出它的备份(比如命名为Ubuntu.tar)wsl --export Ubuntu-20.04 D:\WSL\Ubuntu-20.04.tar# 2. 注销原来的wsl --unregister Ubuntu-20.04# 3. 将备份文件恢复到D:\WSL\Ubuntu-20.04中去wsl --import Ubuntu-20.04 D:\WSL\Ubuntu-20.04 D:\WSL\Ubuntu-20.04.tar# 这时候可以把Ubuntu-20.04.tar删除了

如果这时候启动WSL,发现好像已经恢复正常了,但是用户变成了root。用以下命令恢复:

1
Ubuntu2004 config --default-user username

安装最新版emacs

可以安装较新的 gcc 编译器

123456789101112
sudo add-apt-repository ppa:ubuntu-toolchain-r/testsudo apt updatesudo apt install gcc-13 g++-13sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 130sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 130# 之后如果要更改使用下面的命令sudo update-alternatives --config gcc

编译 emacs

123456789101112131415161718192021222324252627282930313233343536373839
# Optional: 卸载snapdsudo apt-get purge snapd# 编译tree-sittergit clone https://github.com/tree-sitter/tree-sitter.gitcd tree-sittergit checkout release/0.25makemake install PREFIX="$(pwd)/_install"# 修复 .pc 文件中的 prefix(把 prefix 行改为下面的路径)nano _install/lib/pkgconfig/tree-sitter.pc# prefix=/home/zhaohang/repository/tree-sitter/_install# 设置环境变量~/.bashrc或~/.zshrcexport PKG_CONFIG_PATH="$HOME/repository/tree-sitter/_install/lib/pkgconfig:$PKG_CONFIG_PATH"# 下载emacs源码wget https://mirrors.ustc.edu.cn/gnu/emacs/emacs-30.2.tar.xzcd emacs-30.2sudo apt install cmake build-essentialsudo apt install zlib1g-dev pkg-config libgnutls28-dev libncurses-dev libjansson-dev libvterm-dev libgccjit-$(gcc -dumpversion)-devexport 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# 编译后测试,应该输出:RUNPATH /home/zhaohang/repository/tree-sitter/_install/lib:objdump -p /usr/local/bin/emacs | grep RUNPATH
  • --without-xemacs-nox,不使用 X11(Xorg),只能用 emacs -nw(终端)
  • --without-ns禁用 macOS 的 Cocoa / NS 窗口系统,只是防止 configure 自动探测到 macOS GUI
  • --without-pgtk禁用 Pure GTK(Wayland 下的新 GUI 后端)
  • --with-native-compilation启用 native-comp(.eln)将 Elisp 编译成机器码
  • --with-modules启用动态模块支持,需要 libjansson-dev
  • --with-threads启用多线程支持
  • --with-tree-sitter启用 tree-sitter
  • --prefix=/usr/local安装到 /usr/local

emacs配置参考:

qemu 最新版编译参考

另外通过apt下载的clangd版本比较老,可能不支持某些配置项,可通过下面命令安装较新的版本

123456789101112
wget https://apt.llvm.org/llvm.shchmod u+x llvm.shsudo ./llvm.sh 21sudo apt-get install clang-format-21# 安装 clang 到 alternatives 系统sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-21 100sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-21 100sudo update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-21 100sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-21 100# 安装 libstdc++-10sudo apt install libstdc++-$(gcc -dumpversion)-dev

git 版本比较低,不支持-b参数,可以添加apt源更新

1234
sudo add-apt-repository ppa:git-core/ppa -ysudo apt updatesudo apt upgradesudo apt install git -y

python 版本是3.8,某些软件可能依赖3.9及以上的版本:

1234567891011
sudo add-apt-repository ppa:deadsnakes/ppasudo apt-get updatesudo apt-get install python3.9 python3-pip# 注册 python3.9,优先级设置为2sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2# 注册 python3.8,优先级设置为1,这样优先级小于python3.9sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1# 选择默认版本sudo update-alternatives --config python3

bear 也可以使用最新版本,旧版本的bear是python脚本,效率偏低,而新版本是rust编写的二进制文件

1234567891011121314
# 下载rustcurl --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# 编译beargit clone https://github.com/rizsotto/Bear.gitcd bearcargo build --releasesudo install -m 755 target/release/bear /usr/local/bin/# For Debian based systemsexport INSTALL_LIBDIR=lib/x86_64-linux-gnusudo mkdir -p /usr/local/libexec/bear/$INSTALL_LIBDIRsudo install -m 755 target/release/libexec.so /usr/local/libexec/bear/$INSTALL_LIBDIR/

WSL2 Ubuntu20.04
WSL2 Ubuntu20.04

参考

评论加载中…