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. BenjaminLee BenjaminLee

      I've been using WSL for years and still learned new things. The DNS tunneling and firewall settings were new to me. Great article!

    2. 王思远 王思远

      建议把.wslconfig和wsl.conf的路径写清楚,第一次配置的时候找了半天。教程里两个路径都给了,很贴心。

    3. IsabellaWhite IsabellaWhite

      The troubleshooting section at the end is gold. netsh winsock reset combined with wsl shutdown fixed my WSL not starting issue.

    4. 刘子涵 刘子涵

      内核命令wsl uname -a这个验证方法很直接,能清楚看到当前内核版本,方便排查问题。

    5. WilliamTaylor WilliamTaylor

      This guide covers everything from basic upgrade to advanced configuration. The nested virtualization tip alone is worth bookmarking.