devAlice
← Windows

WSLg — 在 Windows 上运行 Linux GUI 应用

配置 WSL2 的 GUI 集成(WSLg),让 Linux 桌面应用像原生 Windows 窗口一样打开。无需外部 X server 或 VcXsrv。

曾几何时,从 WSL 运行 Linux GUI 应用需要安装 VcXsrv 或 X410、设置 DISPLAY 环境变量、调整 Windows 防火墙。自从 WSLg(WSL GUI) 随 Windows 11 + WSL 0.65+ 推出,这一切都成了历史——Linux 应用直接作为 Windows 窗口打开。

我认为 WSLg 的意义不只是技术便利,而是 Windows 开发工作流的质的转变。以前 WSL 和 Windows GUI 应用是两个独立世界;如今通过 WSLg,Linux GUI 工具直接无缝集成,由于那个集成,Windows 开发者得以使用最优的工具而无需妥协。

本指南在 Windows 11 22H2+ + WSL 2 + Ubuntu 24.04 上启用 WSLg,并演示如何运行常见 Linux GUI 应用(Firefox、gedit、GIMP、GUI 调试器)。

TL;DR

  1. wsl --version — 如果同时显示 WSL version 2.0+WSLg 1.0+,说明已就绪
  2. wsl --update 获取最新版本
  3. sudo apt install -y x11-appsxcalc — 如果计算器以 Windows 窗口打开,成功
  4. 无需配置 DISPLAY — WSLg 自动注入
  5. Wayland 应用获得 WAYLAND_DISPLAY,X11 应用获得 DISPLAY=:0——两者都自动设置

前置条件

  • Windows 11 22H2+(Windows 10 21H2+ 也可以,但 11 稳定性/性能更好)
  • WSL 2 + Ubuntu 24.04(其他发行版也可以——Debian、Fedora 等)
  • 最新 GPU 驱动(NVIDIA/AMD/Intel——WSLg 使用 vGPU)

1. WSLg 工作原理(简述)

[Linux GUI 应用] ──Wayland/X11──> [WSLg 系统合成器]
                                       │
                                       ▼
                                [Windows RDP]
                                       │
                                       ▼
                              [Windows 桌面]
  • Linux 应用表现为普通的 Wayland 或 X11 客户端
  • WSLg 运行 Weston 合成器并通过 RDP 转发到 Windows
  • 从 Windows 角度看,该应用只是一个 RDP 客户端——无需单独的 X server

2. 验证——是否已经可用?

wsl --version
# WSL version: 2.x.x
# WSLg version: 1.x.x  ← 两者都有表示 OK

如果没有 WSLg 行,更新:

wsl --update
wsl --shutdown

测试:

# 在 WSL Ubuntu 内
sudo apt update && sudo apt install -y x11-apps
xcalc          # 或 xeyes / xclock

Windows 中出现 GUI 窗口即成功。

如果没有出现,请参见 第 8 节 故障排查

3. 常用 GUI 应用

3.1 Firefox

sudo apt install -y firefox
firefox

通过 WSL Ubuntu 的 Firefox 浏览 HTTPS 网站并使用 DevTools。GPU 加速不如 macOS Safari 或 Windows 原生 Firefox,重型网站会稍慢一些。

3.2 文本编辑器(gedit、gnome-text-editor)

sudo apt install -y gnome-text-editor
gnome-text-editor /etc/hosts

当你偏好 GUI 而非 nano 时,便于快速 sudo 编辑。

3.3 图像编辑(GIMP)

sudo apt install -y gimp
gimp

不如 Photoshop 快,但 Linux 版本可以直接使用。

3.4 GUI 调试器(ddd、gdb-gui)

sudo apt install -y ddd
ddd ./my_program

如果你用 VS Code Remote-WSL,其实不需要 ddd,但如果你想要经典 GUI 调试器,它就在这里。

3.5 Wireshark(网络分析)

sudo apt install -y wireshark
sudo wireshark

可以捕获 WSL 的虚拟网络接口。如需直接捕获 Windows 主机流量,需要额外配置(wsl --networkingMode=mirrored——Windows 11 22H2+)。


4. Wayland vs X11 — 自动选择

WSLg 同时支持两种协议,两个环境变量均自动设置:

echo $DISPLAY
# :0
 
echo $WAYLAND_DISPLAY
# wayland-0
  • 优先 Wayland — 现代 GTK/Qt 应用自动选择 Wayland(默认 GDK_BACKEND=wayland
  • X11 回退 — 不支持 Wayland 的应用回退到 DISPLAY=:0

为特定应用强制指定协议:

# 强制使用 X11
GDK_BACKEND=x11 firefox
 
# 强制使用 Wayland
GDK_BACKEND=wayland firefox

5. GPU 加速

WSLg 暴露虚拟 GPU(vGPU)用于 OpenGL/Vulkan 加速,CUDA 工作负载也可运行。

检查

sudo apt install -y mesa-utils
glxinfo | grep "OpenGL renderer"
# OpenGL renderer string: D3D12 (NVIDIA GeForce RTX ...) — 主机 GPU 已暴露

使用场景

  • Blender / Maya 等 3D 应用
  • ML 可视化(TensorBoard、Plotly Dash)
  • 游戏(性能有限,但能运行)

CUDA:NVIDIA WSL 驱动安装在 Windows 侧(https://developer.nvidia.com/cuda/wsl)。Linux 内只需安装 nvidia-cuda-toolkit


6. 音频、麦克风、摄像头

WSLg 也自动连接 PulseAudio——音频输出路由到 Windows 默认输出设备。

sudo apt install -y pavucontrol
speaker-test -c 2     # 左右声道测试

麦克风和摄像头稍微复杂:

  • 麦克风:打开 pavucontrol → Recording 标签页 → Windows 麦克风会出现(通常正常)
  • 摄像头:WSLg 不直接支持 USB 摄像头。使用 usbipd-win 挂接 USB 设备

7. 将 Linux 应用固定到 Windows 开始菜单

WSLg 自动注册它们:

Win → 搜索「Firefox」 → 出现 Firefox (Ubuntu) 条目 → 点击后 WSL 启动并打开 Firefox。

可见应用来自 ~/.local/share/applications/*.desktop(Linux 端)。创建自定义 .desktop 文件注册自己的应用:

cat > ~/.local/share/applications/my-app.desktop <<'EOF'
[Desktop Entry]
Type=Application
Name=My Tool
Exec=/usr/local/bin/mytool
Icon=utilities-terminal
EOF

重启 WSL 后即出现在开始菜单中。


8. 故障排查

xcalc 运行但没有窗口出现

  • wsl --version 是否显示 WSLg version 行?
  • wsl --update·wsl --shutdown 后重试
  • 更新主机 GPU 驱动(WSLg 1.0.62+ 有 mesa 软件回退,但部分配置仍需 GPU)

字体乱码(非拉丁文字显示为方块)

sudo apt install -y fonts-noto-cjk fonts-nanum
fc-cache -fv

剪贴板不同步

WSLg 自动双向同步文本剪贴板。如果停止工作:

  • 确认应用是 Wayland 还是 X11?Wayland 应用偶尔丢剪贴板
  • 强制 GDK_BACKEND=x11 后重试

出现「GTK theme not found」类型警告

sudo apt install -y adwaita-icon-theme

缺少默认 GTK 主题。少见;在某些最小化 Ubuntu 镜像中出现。

部分按键不能传递到 GUI 应用

Ctrl+Shift 组合键或中文输入法按键被 Windows 先捕获。解决方法:

  • 禁用 Windows IME(Win + Space 切换到英文)
  • 或在 Linux 内安装 ibus / fcitx5,完全在 Linux 内输入中文

应用卡顿(明显的 UI 延迟)

  • GPU 加速可能回退到软件渲染——用 glxinfo | grep -i hardware 检查
  • 更新 GPU 驱动
  • WSL2 内存不足——在 .wslconfig 中设置 memory=8GBwsl-tuning

9. 局限性

  • 仅支持 Windows 11 / WSL 2 — 不支持 WSL 1
  • 无系统托盘 / 小组件 — Linux 托盘图标不显示
  • 多显示器正常;分数缩放有问题 — 在 125%/150% 缩放下部分应用显示模糊
  • 自动启动 / 后台守护进程 — 要在 Windows 启动时运行 Linux systemd 服务,需要额外配置(systemctl --user + Windows Task Scheduler)

10. 下一步


参考资料

更新日志

  • 2026-05-16:初稿。WSLg 启用 · 常见 GUI 应用 · GPU 加速 · 音频/麦克风 · 开始菜单集成 · 六个故障排查案例。