Windows Subsystem for Linux 配置记录

写在前面

Windows Subsystem for Linux(简称 WSL)顾名思义是在 Windows 上运行的 Linux 子系统。搭配 Windows Terminal,开发者可以获得直接在 Windows 上运行 GNU/Linux 环境的极佳体验。

需要注意的是,WSL 本身并不是一个 Linux 发行版,我们需要挑选一款我们中意的 Linux 发行版进行安装。本文将记录我配置 WSL 的 Debian 镜像的部分操作。

安装 WSL

首先用 Powershell 列出可用的发行版:

1
2
# list all online distributions
wsl --list --online

然后选择安装 Debian:

1
wsl --install --distribution debian

安装完成后可以在 Windows Terminal 中看到 WSL 的 Profile。

配置代理

让 WSL 使用 Windows 的代理,如果你不需要,可以跳至下一节。

首先向 ~/.bashrc 追加写入以下内容(这里假设 Windows 端代理端口为 1080):

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
# proxy operation
proxy() {
if [[ $@ == 'enable' ]]; then
# Get host ip
export HOST_IP=$(ip route | grep default | awk '{print $3}');
export PROXY_PORT=1080;
export {all_proxy,ALL_PROXY}="socks5://${HOST_IP}:${PROXY_PORT}";
export {ftp_proxy,FTP_PROXY}="http://${HOST_IP}:${PROXY_PORT}";
export {http_proxy,HTTP_PROXY}="http://${HOST_IP}:${PROXY_PORT}";
export {https_proxy,HTTPS_PROXY}="http://${HOST_IP}:${PROXY_PORT}";
elif [[ $@ == 'disable' ]]; then
unset {all_proxy,ALL_PROXY};
unset {ftp_proxy,FTP_PROXY};
unset {http_proxy,HTTP_PROXY};
unset {https_proxy,HTTPS_PROXY};
else
echo 'all_proxy, ALL_PROXY =' ${all_proxy:-'none'};
echo 'ftp_proxy, FTP_PROXY =' ${ftp_proxy:-'none'};
echo 'http_proxy, HTTP_PROXY =' ${http_proxy:-'none'};
echo 'https_proxy, HTTPS_PROXY =' ${https_proxy:-'none'};
fi
}

# enable proxy
proxy enable

# add sudop alias
alias sudop='sudo --preserve-env=all_proxy,ALL_PROXY,ftp_proxy,FTP_PROXY,http_proxy,HTTP_PROXY,https_proxy,HTTPS_PROXY'

重启 Terminal,检查代理是否生效:

1
proxy

APT 包管理

首先更新包管理:

1
2
sudop apt update
sudop apt upgrade

最后安装一些常用的包:

1
sudop apt install -y vim curl wget git

为 Bash 配置 Oh My Posh

首先下载 Oh My Posh

1
2
sudop wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-amd64 -O /usr/local/bin/oh-my-posh
sudo chmod +x /usr/local/bin/oh-my-posh

然后检查安装是否成功:

1
oh-my-posh --version

接下来我们下载 Oh My Posh 的主题,首先安装 unzip 用于解压:

1
sudop apt install -y unzip

然后下载主题:

1
2
3
4
5
mkdir ~/.poshthemes
wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/themes.zip -O ~/.poshthemes/themes.zip
unzip ~/.poshthemes/themes.zip -d ~/.poshthemes
chmod u+rw ~/.poshthemes/*.omp.*
rm ~/.poshthemes/themes.zip

最后在 ~/.bashrc 中追加写入以下内容:

1
2
# enable oh-my-posh
eval "$(oh-my-posh init bash --config ~/.poshthemes/ys.omp.json)"

配置开发环境

推荐在 VS Code 中安装插件 Remote - SSH 以获得最佳开发体验。

配置 Git

参考:Get started using Git on Windows Subsystem for Linux

首先进行基本配置:

1
2
3
4
5
git config --global user.name "0xfaner"
git config --global user.email "0xfaner@gmail.com"
git config --global init.defaultBranch master
git config --global core.editer "code --wait"
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager-core.exe"

然后将 Windows 端的 SSH 密钥拷贝到 WSL 中使用:

1
2
cp -r /mnt/c/Users/0xfaner/.ssh ~
chmod 600 ~/.ssh/id_rsa

最后检查 SSH 密钥是否配置成功:

1
ssh -T git@github.com

配置 C/C++ 环境

参考:Using C++ and WSL in VS Code

首先安装必要的包:

1
sudop apt install -y build-essential gdb

然后检查是否安装成功:

1
2
3
gcc --version
g++ --version
gdb --version

配置 Node.js 环境

参考:Install Node.js on Windows Subsystem for Linux (WSL2)

首先安装 NVM:

1
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

然后重启 Terminal,检查安装是否成功:

1
nvm --version

接着安装 LTS 版本的 Node.js:

1
nvm install --lts

检查当前状态:

1
nvm ls

配置 Rust 环境

参考:Getting started - Rust Programming Language

只需运行如下命令即可:

1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

配置 Ruby (Jekyll) 环境

参考:Jekyll on Ubuntu

首先安装必要的包:

1
sudop apt install ruby-full build-essential zlib1g-dev

然后检查安装是否成功:

1
2
ruby --version
gem --version

最后在 ~/.bashrc 中追加写入以下内容:

1
2
3
# Install Ruby Gems to ~/gems
export GEM_HOME="$HOME/gems"
export PATH="$HOME/gems/bin:$PATH"