Windows 环境AI编程,WSL 安装踩坑记录与使用指南

2 阅读2分钟

本文记录我在 Windows 上安装和使用 WSL(Windows Subsystem for Linux)的完整过程,包括遇到的问题及解决方案。

环境说明

  • 操作系统:Windows 10
  • 已安装:Docker Desktop(使用 WSL 2 后端)
  • 目标:安装 Ubuntu 用于 AI 开发

一、安装 WSL

1.1 检查系统要求

确保 Windows 版本满足要求:

  • Windows 10 版本 21H2 或更高
  • Windows 11

查看版本:按 Win + R,输入 winver

1.2 手动安装 Ubuntu

由于在线安装可能卡住,建议手动下载安装。

步骤一:下载 Ubuntu 安装包

访问 Ubuntu WSL 镜像或微软 WSL 发行版列表,下载 .AppxBundle 文件。

步骤二:使用 PowerShell 安装

以管理员身份打开 PowerShell,执行:

Add-AppxPackage -Path "C:\path\to\Ubuntu2204-221101.AppxBundle"

步骤三:启动并配置

  1. 在开始菜单找到 Ubuntu 应用并打开
  2. 首次启动会初始化,等待一两分钟
  3. 按提示设置用户名和密码

步骤四:验证安装

在 PowerShell 中执行:

wsl -l -v

应该能看到 Ubuntu,VERSION 为 2。

二、基础配置

2.1 更新系统

sudo apt update && sudo apt upgrade

2.2 安装 Node.js

使用 NodeSource PPA 安装最新稳定版:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v

三、安装 ZSH

参考:ZSH!在 Windows 上使用 WSL+ZSH-腾讯云开发者社区

3.1 安装 ZSH

sudo apt install -y zsh

3.2 安装 Oh My Zsh

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

安装过程中会提示将 ZSH 设置为默认 Shell,输入 Y 确认。

3.3 常用插件

# 安装 zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

# 安装 zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

编辑 ~/.zshrc 启用插件:

plugins=(git docker node npm zsh-autosuggestions zsh-syntax-highlighting)

四、配置代理

4.1 问题说明

Windows 启动 Clash 代理后,WSL 不会自动使用代理,需要手动配置。

4.2 推荐方案:镜像网络模式

适用于 Windows 11 或 Windows 10 22H2 及以上版本。

在 Windows 用户目录(C:\Users\你的用户名)下创建 .wslconfig 文件:

[wsl2]
networkingMode=mirrored
autoProxy=true
firewall=false
dnsTunneling=true

重启 WSL:

wsl --shutdown

然后重新打开 Ubuntu 终端,验证:

curl -I https://www.google.com

4.3 备选方案:手动配置代理

如果镜像网络模式不可用,使用环境变量方式。

获取 Windows 主机 IP:

ip route | grep default | awk '{print $3}'

设置代理(假设代理端口为 7890):

export http_proxy="http://<Windows-IP>:7890"
export https_proxy="http://<Windows-IP>:7890"
export all_proxy="http://<Windows-IP>:7890"

写入 ~/.bashrc 永久生效:

echo 'host_ip=$(ip route show default | awk '\''{print $3}'\'')' >> ~/.bashrc
echo 'export http_proxy="http://$host_ip:7890"' >> ~/.bashrc
echo 'export https_proxy="http://$host_ip:7890"' >> ~/.bashrc
echo 'export all_proxy="http://$host_ip:7890"' >> ~/.bashrc
source ~/.bashrc

4.4 注意事项

  • 确保 Clash 代理开启了"允许局域网连接"
  • 检查防火墙是否放行

五、安装 Claude Code

5.1 解决 npm 全局安装权限问题

默认情况下,npm install -g 会尝试写入系统目录,导致权限错误。

配置 npm 使用用户目录:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

5.2 安装 Claude Code

npm install -g @anthropic-ai/claude-code

六、VS Code 连接 WSL

在 Windows 上安装好 VS Code 后,安装 Remote - WSL 扩展。然后在 WSL Ubuntu 终端中进入项目目录,执行:

code .

VS Code 会自动启动并连接到 WSL 环境,代码编辑在 Windows UI 中完成,编译运行在 Linux 环境中进行。

七、常用 WSL 命令

命令说明
wsl -l -v列出所有已安装的发行版
wsl -d Ubuntu进入 Ubuntu
wsl --shutdown关闭所有 WSL 实例
wsl --update更新 WSL 内核

八、常见问题

8.1 安装卡在 0%

如果在线安装卡住,建议直接手动下载 Ubuntu 安装包进行离线安装。

访问微软官方手动安装指南下载 Ubuntu:

WSL 手动安装步骤 - Microsoft Learn

下载 .AppxBundle 文件后,使用 PowerShell 安装:

Add-AppxPackage -Path "C:\path\to\Ubuntu2204-xxx.AppxBundle"

8.2 npm install 速度慢

确保已配置代理,或使用镜像源:

npm config set registry https://registry.npmmirror.com

8.3 权限错误

使用用户目录方案解决,避免使用 sudo npm install -g

九、性能优化

  • 项目文件存放在 WSL 文件系统(如 ~/projects/)中,而非 Windows 分区(/mnt/c/
  • 可通过 .wslconfig 限制 WSL 内存使用:
[wsl2]
memory=8GB
processors=4