Windows WSL 升级与环境支持配置

Windows WSL 升级与环境支持配置

一、升级 WSL 到最新版本

1. 检查当前版本

# 查看 WSL 版本
wsl --version

# 查看 WSL 2 状态
wsl --status

2. 升级方法

方法1:通过命令行升级(推荐)

# 更新 WSL 到最新版本
wsl --update

# 更新并重启 WSL
wsl --update --web-download

# 查看可用的更新
wsl --update --status

方法2:手动安装最新版本

  1. 下载最新的 WSL 安装包:

  2. 安装:

    # 使用 PowerShell 安装
    Add-AppxPackage .\Microsoft.WSL_版本号_x64.msixbundle

二、开启完整环境支持

1. 启用必要的 Windows 功能

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

# 启用 WSL 功能
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

# 启用虚拟机平台(WSL 2 必需)
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

# 启用 Hyper-V 虚拟化(可选,增强性能)
dism.exe /online /enable-feature /featurename:Microsoft-Hyper-V /all /norestart

# 启用容器支持(可选)
dism.exe /online /enable-feature /featurename:Containers /all /norestart

2. 设置 WSL 2 为默认版本

# 设置默认版本
wsl --set-default-version 2

# 验证设置
wsl --status

3. 配置 WSL 全局设置

创建/编辑 C:\Users\你的用户名\.wslconfig 文件:

[wsl2]
# 内存限制(根据实际需求调整,单位 MB)
memory=8GB

# CPU 核心数限制
processors=4

# 交换空间大小
swap=4GB

# localhost 转发,允许 Windows 访问 WSL 内的服务
localhostForwarding=true

# 启用嵌套虚拟化(用于 Docker 等)
nestedVirtualization=true

# 启用内核调试
kernelCommandLine=vsyscall=emulate

# 设置 DNS 服务器
dnsTunneling=true
firewall=true

4. 配置发行版设置

编辑 C:\Users\你的用户名\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu...\LocalState\wsl.conf

[automount]
# 自动挂载 Windows 驱动器
enabled = true
# 挂载点路径
root = /mnt/
# 挂载选项
options = "metadata,umask=22,fmask=11"

[network]
# 设置主机名
hostname = ubuntu-wsl
# 生成 resolv.conf
generateResolvConf = true

[interop]
# 启用 Windows 互操作
enabled = true
# 启用 Windows 路径追加
appendWindowsPath = true

[user]
# 默认登录用户
default = 你的用户名

三、升级现有发行版

1. 备份现有发行版

# 导出当前发行版
wsl --export Ubuntu-22.04 D:\backup\ubuntu-backup.tar

# 关闭所有发行版
wsl --shutdown

2. 升级发行版内核

在 WSL 中运行:

# 更新包列表
sudo apt update

# 升级所有包
sudo apt upgrade -y

# 更新内核
sudo apt install --install-recommends linux-generic -y

# 清理不需要的包
sudo apt autoremove -y

3. 重启 WSL

# 完全重启 WSL
wsl --shutdown

# 重新启动发行版
wsl -d Ubuntu-22.04

四、开发环境增强支持

1. 启用 systemd(WSL 2 新特性)

/etc/wsl.conf 中添加:

[boot]
systemd=true

然后重启 WSL:

wsl --shutdown

2. 配置 GPU 加速(用于 AI/ML 开发)

安装 GPU 驱动:

3. 配置 Docker 支持

# 安装 Docker Desktop for Windows(推荐)
# 或直接在 WSL 中安装 Docker Engine

# 使用官方脚本安装 Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# 将用户加入 docker 组
sudo usermod -aG docker $USER

五、验证配置

# 检查 WSL 版本
wsl --version

# 查看发行版状态
wsl --list --verbose

# 检查 WSL 2 内核
wsl uname -a

# 检查 systemd 状态
wsl systemctl list-units --type=service

# 检查资源限制
wsl cat /proc/meminfo
wsl nproc

六、常见问题解决

1. WSL 无法启动

# 重置 WSL
wsl --shutdown
netsh winsock reset
# 重启电脑

2. 内核版本过旧

# 手动下载安装最新内核
# https://aka.ms/wsl2kernel

3. 网络连接问题

# 在 WSL 中重置网络
sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
sudo chattr +i /etc/resolv.conf

完成以上配置后,你的 WSL 环境将具备完整的开发能力,支持 Docker、GPU 加速、systemd 等高级功能。

已有 8089 条评论

    1. MasonBrown MasonBrown

      I appreciate that you covered both upgrade methods. Command line for power users and manual install for those who prefer more control.

    2. 吴思琪 吴思琪

      DNS配置那段很实用,有时候WSL里网络不通,按照教程重置resolv.conf就能解决。chattr +i防止被覆盖,学到了。

    3. LiamAnderson LiamAnderson

      The GPU acceleration section is perfectly timed. Just got an NVIDIA card and wanted to run ML models in WSL. This guide shows exactly how.

    4. 周雨桐 周雨桐

      wsl.conf里的[automount]配置很有用,挂载Windows驱动器时添加metadata选项,文件权限问题少了很多。

    5. AmeliaMartinez AmeliaMartinez

      I was stuck on WSL 1 for months because I didn't know about the kernel update. The link to download the latest kernel fixed everything.