WSL2 + Ubuntu 22.04 全流程安装与避坑指南(适配 D 盘)
适用于:Windows 10/11 用户 目标:在 D 盘上部署完整可联网的 Ubuntu 22.04 + GPU 支持的开发环境 作者:jiahao(实际踩坑实践) 更新时间:2025-10
📋 一、前置条件检查
- Windows 已升级到 21H2 或更高版本;
- 已安装 NVIDIA 显卡驱动 ≥ 510(支持 CUDA 12);
- Windows 已安装 PowerShell 7+;
- 本机具备管理员权限;
- 可联网。
🧹 二、彻底清理旧版 WSL 环境
很多用户安装不干净是因为旧版本遗留。执行以下命令可完全重置。
1️⃣ 查看已安装发行版
wsl --list --all如果看到:
Ubuntu Ubuntu-20.04 docker-desktop说明旧版本仍存在。
如果没有直接跳转到3.2
2️⃣ 注销所有旧发行版
wsl --unregister Ubuntu wsl --unregister Ubuntu-20.04 wsl --unregister docker-desktop wsl --unregister docker-desktop-data3️⃣ 卸载系统组件(可选彻底清理)
wsl --shutdown dism.exe /online /disable-feature /featurename:Microsoft-Windows-Subsystem-Linux /norestart dism.exe /online /disable-feature /featurename:VirtualMachinePlatform /norestart在“应用和功能”中卸载:
- Windows Subsystem for Linux Update
- Ubuntu
- Docker Desktop
⚙️ 三、重新启用并安装 WSL2
1️⃣ 启用系统组件
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart重启电脑:
shutdown /r /t 02️⃣ 安装 WSL 内核
wsl --install --no-distribution3️⃣ 设置默认版本
wsl --set-default-version 2💾 四、在 D 盘安装 Ubuntu 22.04
默认情况下,WSL 会安装在 C 盘。我们将其导出迁移到 D 盘,释放空间。
1️⃣ 安装 Ubuntu 22.04
wsl --install -d Ubuntu-22.04首次启动后根据提示创建用户(如 jiahao)。如果提示:“wsl: 检测到 localhost 配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost。”这是正常的不用管他。
2️⃣ 导出系统镜像
wsl --shutdown wsl --export Ubuntu-22.04 D:\WSL\ubuntu.tar3️⃣ 注销旧系统
wsl --unregister Ubuntu-22.044️⃣ 导入到 D 盘
wsl --import Ubuntu-22.04 D:\WSL\Ubuntu D:\WSL\ubuntu.tar --version 25️⃣
del D:\WSL\ubuntu.tar验证迁移:
wsl --list --verbose应显示:
Ubuntu-22.04 Stopped 2🐧 五、首次进入系统与基础配置
启动:
wsl -d Ubuntu-22.04更新系统:
sudo apt update && sudo apt upgrade -y sudo apt install -y build-essential git curl wget vim python3.11 python3.11-venv🧠 六、解决无法联网
⚠️ 默认 WSL 网络为 NAT 模式,不能自动继承 Windows daili。 你会遇到 curl 卡住、apt update timeout 的问题。
1️⃣ 在 Windows 的🐱中启用 LAN
进入🐱设置 → 开启局域网
确认配置中有:
allow-lan: true bind-address: 0.0.0.0 mixed-port: 7897重点是第二行:bind-address: 0.0.0.0,这一步很关键,🐱的配置文件是config.yaml,同时需要注意端口没错。
然后重启🐱。
2️⃣ 在 PowerShell 验证端口监听
我们需要得到两条信息:WSL的内网ip和🐱的转发端口(第一步已经得到了)
netstat -ano | findstr 7897✅ 期望输出:
TCP 0.0.0.0:7897 0.0.0.0:0 LISTENING ……3️⃣ 在 WSL 中获取主机 IP
cat /etc/resolv.conf | grep nameserver输出示例:
nameserver 172.19.48.1测试是否连通,在WSL里面执行:
nc -zv 172.19.48.1 7897如果输出:
Connection to 172.19.48.1 7897 port [tcp/*] succeeded! 说明端口没被挡住。如果失败,则管理员权限单开一个Powershell,然后输入(注意替换端口):
这条命令会让 Windows 接受来自 WSL 的请求访问端口 7897。
之后再回到 WSL 执行:
nc -zv 172.19.48.1 7897 应当返回 succeeded ✅。
4️⃣ 手动设置
export http_proxy="http://172.19.48.1:7897" export https_proxy="http://172.19.48.1:7897"验证:
curl -I https://www.google.com返回 HTTP/2 200 即表示成功。
然后执行:
echo 'Acquire { HTTP::proxy "http://172.19.48.1:7897"; HTTPS::proxy "http://172.19.48.1:7897"; };' | \ sudo tee /etc/apt/apt.conf.d/proxy.conf 能更新包列表就说明 apt 已走DL。
5️⃣ 永久生效(添加到 .bashrc)
编辑:
nano ~/.bashrc添加(注意端口需要替换成自己的🐱实际端口):
win_ip=$(grep nameserver /etc/resolv.conf | awk '{print $2}') export http_proxy="http://$win_ip:7897" export https_proxy="http://$win_ip:7897" export HTTP_PROXY=$http_proxy export HTTPS_PROXY=$https_proxy echo "[WSL] Proxy set to $http_proxy"保存并执行:
source ~/.bashrc👤 七、恢复默认登录用户(迁移后变 root 的修复)
迁移导入后 WSL 默认用 root 登录。
1️⃣ 查看用户
ls /home例如看到:
jiahao2️⃣ 切换回用户
su - jiahao3️⃣ 设置默认启动用户
在 PowerShell 中:
Ubuntu-22.04.exe config --default-user jiahao下次启动 WSL 就会自动进入你的账户。
🧰 八、测试 GPU / CUDA(可选)
确保驱动版本 ≥ 510:
nvidia-smi输出显示显卡信息即代表 GPU 直通成功。
🧩 九、安装开发依赖(可选)
创建 Python 环境:
python3 -m venv ~/venv source ~/venv/bin/activate pip install -U pip常见依赖:
pip install fastapi uvicorn faiss-cpu qdrant-client pymupdf transformers sentence-transformers🧱 十、常见问题与解决
问题 | 原因 | 解决方案 |
curl 卡住 | 🐱未 Allow LAN | 打开 Allow LAN 并设置 bind-address: 0.0.0.0 |
Connection refused | Windows 阻挡 | PowerShell: |
默认进入 root | WSL 导入时未保存默认用户 | Ubuntu-22.04.exe config --default-user jiahao |
.bashrc 无daili配置 | 修改了 root 的 .bashrc | 编辑 /home//.bashrc |
无法访问网络 | 🐱仅监听 127.0.0.1 | 在配置文件添加 bind-address: 0.0.0.0 |
GPU 不生效 | 驱动或 CUDA 版本过低 | 更新至 NVIDIA ≥510,CUDA 12+ |
🧾 十一、最终结构验证
查看系统
cat /etc/os-release确认代理
echo $http_proxy确认 GPU
nvidia-smi确认挂载路径(应在 D 盘)
df -h | grep /输出路径指向:
/mnt/d/WSL/Ubuntu/✅ 十二、总结
模块 | 说明 |
系统 | WSL2 + Ubuntu 22.04 |
安装位置 | D:\WSL\Ubuntu |
默认用户 | jiahao |
网络设置 | 🐱(Allow LAN + 0.0.0.0:7897) |
GPU 支持 | CUDA 12.6 + A800 / 4060 |
验证命令 | curl -I https://www.google.com 、 nvidia-smi |
推荐开发工具 | VSCode + Remote WSL 插件 |
如果测试过程中有任何问题欢迎沟通交流!