1.安装wsl2WSL2 是 “运行环境”全称 Windows Subsystem for Linux 2是 Windows 系统自带的功能。它通过虚拟化技术让 Windows 能原生运行 Linux 内核无需单独装虚拟机如 VMware启动快、资源占用低。查看是否安装 wsl -vPS C:\Users\guyin wsl -v WSL 版本: 2.6.1.0 内核版本: 6.6.87.2-1 WSLg 版本: 1.0.66 MSRDC 版本: 1.2.6353 Direct3D 版本: 1.611.1-81528511 DXCore 版本: 10.0.26100.1-240331-1435.ge-release Windows: 10.0.26100.6899若未安装, 右键开始-搜索-wsl, 若未安装wsl按下任意键自动下载安装, 或者powershell执行wsl --install自动下载安装并启用wsl22.升级Windows系统我的系统是win11, 规格如下:版本 Windows 11 家庭中文版版本号 24H2安装日期 ‎2025/‎8/‎11操作系统版本 26100.6899如果操作系统版本为10.0.26100.6899, 需要升级系统, 这个版本的系统无法安装Ubuntu, 升级后最后. 10.0.26100.7171是可以安装ubuntu的(已验证).3.安装ubuntuUbuntu 是 “适配的系统”Ubuntu 是主流 Linux 发行版之一微软已将其打包成应用可在微软商店下载。安装后它会作为 WSL2 的 “客户端”借助 WSL2 的内核环境实现和物理机 Ubuntu 几乎一致的命令行操作、软件安装如 Docker、Python。3.1.确保BIOS/UEFI 中启用了虚拟化VT-x / AMD-V如何检查按Ctrl Shift Esc打开任务管理器切到 “性能” → “CPU”查看右下角“虚拟化: 已启用”如果显示“已启用”→ 跳到第二步如果显示“已禁用”→ 需要进 BIOS 开启如何开启虚拟化重启电脑按F2 / F10 / Del / Esc不同品牌键不同进入 BIOS找到类似选项Intel CPU:Intel Virtualization Technology (VT-x)→ EnabledAMD CPU:SVM Mode或AMD-V→ Enabled保存并退出通常按 F10 笔记本常见位置Dell:Processor Settings→VirtualizationLenovo:Security→VirtualizationHP:System Configuration→Virtualization Technology3.2.确保 Windows 功能已启用以管理员身份打开 PowerShell 或 CMD运行dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart然后重启电脑。⚠️ 注意必须启用VirtualMachinePlatform仅启用 WSL 是不够的你也可以通过图形界面检查按Win R→ 输入optionalfeatures确保勾选☑️适用于 Linux 的 Windows 子系统☑️虚拟机平台点确定并重启3.3.设置 WSL2 为默认版本重启后在 PowerShell 中运行wsl --set-default-version 23.4.安装 Ubuntu现在通过Microsoft Store安装 Ubuntu打开 Microsoft Store搜索 “Ubuntu”安装Ubuntu由 Canonical 提供安装完成后从开始菜单启动它打开之后要求创建普通用户和设置密码, root操作使用sudo执行3.5.验证是否成功安装成功后运行wsl -l -v输出应类似NAME STATE VERSION * Ubuntu Running 2进入 WSL(默认进入powershell所在目录挂载的位置)wsl并测试uname -a # 应包含 microsoft 和 WSL23.6.补充说明即使你在 Windows 11 InsiderBuild 26100也必须开启虚拟化否则 WSL2 无法工作。某些杀毒软件如 360、McAfee会阻止虚拟化可临时关闭测试。Hyper-V 不是必须的但VirtualMachinePlatform是 WSL2 的核心依赖。4.迁移ubuntu工作目录4.1.目录挂载说明ubuntu系统文件目录如/home, /usr 是在C盘Windows 的各个盘符自动挂载在 WSL2 的/mnt/目录下/mnt/c/ → 对应 Windows 的 C:\ /mnt/d/ → 对应 D:\ ...例如Windows 上的C:\Users\guyin\Documents在 Ubuntu 中路径是/mnt/c/Users/guyin/Documents⚠️ 注意不要在/mnt/c/...下运行 Python 项目或容器因为 NTFS ↔ Linux 文件系统跨层访问会导致文件监听失效如 Flask 自动重载不工作权限问题I/O 性能大幅下降最佳实践把代码放在Ubuntu 自己的文件系统里如/home/guyin/myproject4.2.迁移目录打开powershell, 创建目录并进入mkdir E:\ws2_space cd E:\ws2_space导出ubuntuwsl --export Ubuntu ubuntu.tar注销删除当前发行版wsl --unregister Ubuntu从 tar 重新导入到 E 盘注意路径wsl --import Ubuntu E:\ws2_space\Ubuntu ubuntu.tar --version 2设置默认用户否则会以 root 登录# 创建 /etc/wsl.conf 或运行 ubuntu config --default-user guyin # 如果是 Ubuntu 官方包4.3.Windows文件资源管理器打开ubuntu系统目录地址栏输入以下文本回车即可\\wsl$\Ubuntu5.安装docker需进入wsl, powershell执行wslcat /proc/1/comm 命令输出了systemd说明systemd已启用5.1.启动systemd(需要确认版本是否需要手动配置)查看是否启动了systemd, 执行 systemctl status 查看, 如下说明已启动systemdlylocalhost:/mnt/c/Users/guyin$ systemctl status ● localhost State: running Units: 359 loaded (incl. loaded aliases) Jobs: 0 queued Failed: 0 units Since: Fri 2026-02-27 15:03:37 CST; 4min 0s ago systemd: 255.4-1ubuntu8.11 CGroup: / ├─init.scope5.1.1.方法一 : 启用systemd(WSL 内核 ≥ 5.10.67)从Windows 11 22H2 WSL 内核 ≥ 5.10.67开始微软在部分版本中默认启用了 systemd 支持无需手动配置[wsl2]\nsystemdtrue若要配置可能由于编码相关问题出错自行搜索即可.按Win R输入notepad回车输入两行内容[wsl2] systemdtrue点击文件 → 另存为文件名powershell 执行 echo $env:USERPROFILE\.wslconfig 查看保存类型所有文件 (.)编码选择 “UTF-8”注意Windows 记事本的 “UTF-8” 实际是UTF-8 无 BOM尽管名字没写清楚点“保存”执行wsl进入执行 systemctl status 查看systemd是否启动5.1.2.方法二 : 内核版本过低, 更新wsl内核若wsl过于旧, 需要更新wsl, 执行 wsl --status 查看状态PS D:\my wsl --status 默认分发MyUbuntu 默认版本2 适用于 Linux 的 Windows 子系统最后更新于 2026/2/27 适用于 Linux 的 Windows 子系统内核可以使用“wsl --update”手动更新但由于你的系统设置无法进行自动更新。 若要接收自动内核更新请启用 Windows 更新设置:“在更新 Windows 时接收其他 Microsoft 产品的更新”。 有关详细信息请访问https://aka.ms/wsl2kernel。 内核版本 5.10.16执行 wsl --update --web-download 更新内核, 会出现进度条, 下载完成如下PS D:\my wsl --update --web-download 正在下载: 适用于 Linux 的 Windows 子系统 正在安装: 适用于 Linux 的 Windows 子系统 已安装 适用于 Linux 的 Windows 子系统。安装完成查看版本信息如下PS D:\my wsl --status 默认分发: MyUbuntu 默认版本: 2 PS D:\my wsl --version WSL 版本: 2.6.3.0 内核版本: 6.6.87.2-1 WSLg 版本: 1.0.71 MSRDC 版本: 1.2.6353 Direct3D 版本: 1.611.1-81528511 DXCore 版本: 10.0.26100.1-240331-1435.ge-release Windows: 10.0.19045.46515.2.安装docker安装依赖# 安装依赖 sudo apt update sudo apt install -y ca-certificates curl gnupg lsb-release下载GPG密钥(下载报错就多试几次)# 1. 下载 GPG 密钥到临时文件 curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o docker.gpg # 2. 检查是否下载成功应显示 Public Key file docker.gpg # 3. 转换为 gpg 格式并安装 sudo mkdir -p /etc/apt/keyrings sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg docker.gpg # 4. 删除临时文件 rm docker.gpg添加仓库 安装 Docker# 添加仓库 echo \ deb [arch$(dpkg --print-architecture) signed-by/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release echo $VERSION_CODENAME) stable | \ sudo tee /etc/apt/sources.list.d/docker.list /dev/null # 更新包列表 sudo apt update # 安装 Docker Engine Compose plugin sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin安装docker# 安装 Docker Engine sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin # 将当前用户加入 docker 组避免每次 sudo(需要重启wsl2) sudo usermod -aG docker $USER设置开机自启并设置镜像仓库地址# 切换root sudo su - # 设置开机自启 systemctl enable docker.service # 查看Docker状态 systemctl status docker # 设置docekr镜像 mkdir -p /etc/docker cat /etc/docker/daemon.json EOF { registry-mirrors: [ https://docker.1ms.run, https://docker-0.unsee.tech, https://docker.m.daocloud.io ], live-restore: true, features: { buildkit: true } } EOF # 重启docker systemctl restart docker # 查看docker状态 systemctl status docker # 退出root exit退出WSL2exit重启 WSL2wsl --shutdown然后重新打开 Ubuntu 终端。wsl验证docker run --rm hello-world docker-compose --version如下说明安装成功lylocalhost:/mnt/e/ws2_space$ docker run --rm hello-world docker compose version Unable to find image hello-world:latest locally latest: Pulling from library/hello-world 17eec7bbc9d7: Pull complete Digest: sha256:f7931603f70e13dbd844253370742c4fc4202d290c80442b2e68706d8f33ce26 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the hello-world image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ Docker Compose version v2.40.3