外观
配置 WSL2 开发环境
本文介绍在 Windows 上安装和配置 WSL2,并搭建 zsh + Git 基础开发环境。
前提条件
- Windows 10 版本 2004+(内部版本 19041+)或 Windows 11
- BIOS 中已开启 CPU 虚拟化(Intel VT-x / AMD-V)
TIP
可在任务管理器 → 性能 → CPU 中确认「虚拟化」是否为「已启用」。
安装 WSL2
开启 Windows 功能
在「启用或关闭 Windows 功能」中勾选以下两项,然后重启电脑:
- 适用于 Linux 的 Windows 子系统
- 虚拟机平台
也可以通过 PowerShell(管理员)开启:
powershell
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart安装发行版
打开 PowerShell(管理员):
bash
# 查看可用的 Linux 发行版
wsl --list --online
# 安装指定发行版并设为默认(不加 -d 则默认安装 Ubuntu)
wsl --install -d <发行版名称>
# 安装后设置默认发行版
wsl --set-default <发行版名称>常用管理命令
bash
# 查看所有已安装的发行版及运行状态
wsl --list -v
# 启动默认 / 指定发行版
wsl
wsl -d <发行版名称>
# 关闭所有正在运行的发行版
wsl --shutdown
# 卸载发行版(数据会丢失)
wsl --unregister <发行版名称>如需将 WSL 迁移到其他磁盘,参考 迁移 WSL 位置到其他盘。
配置 zsh
zsh 配合 oh-my-zsh 可以提供命令高亮、自动补全、Git 状态提示等功能。
安装 zsh 和 oh-my-zsh
bash
sudo apt update && sudo apt upgrade -y
# 安装 zsh
sudo apt install zsh -y
# 安装 oh-my-zsh
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 设置 zsh 为默认 shell
chsh -s /bin/zsh国内网络问题
如果无法直接访问 GitHub,可以使用 Gitee 镜像安装 oh-my-zsh:
bash
sh -c "$(wget -O- https://gitee.com/pocmon/ohmyzsh/raw/master/tools/install.sh)"安装常用插件
bash
# 命令语法高亮
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# 命令自动补全
git clone https://github.com/zsh-users/zsh-autosuggestions \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions编辑 ~/.zshrc,找到 plugins= 行,改为:
bash
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)常用别名配置
在 ~/.zshrc 末尾追加:
bash
alias ll="ls -alFh"
alias rm="rm -i"修改后执行 source ~/.zshrc 生效。
配置 Git
bash
sudo apt install git -y
# 基本信息
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# 启用大小写敏感(WSL 文件系统区分大小写,建议开启)
git config --global core.ignorecase false
# 生成 SSH 密钥
ssh-keygen -t ed25519 -C "your@email.com"
# 查看公钥,添加到 GitHub / Gitee 等平台
cat ~/.ssh/id_ed25519.pub如需在 WSL 和 Windows 之间共享 Git 凭据,参考 在 Windows 和 WSL 之间共享 Git 凭据。
