在 WSL 下运行 GUI 应用程序

Zai*_*rki 8 gui synaptic gedit windows-subsystem-for-linux

我无法在 WSL 下运行 GUI 应用程序。首先,我尝试运行 Synaptic,但它显示以下消息:

Unable to init server: Could not connect: Connection refused
Failed to initialize GTK.

Probably you're running Synaptic on Wayland with root permission.
Please restart your session without Wayland, or run Synaptic without root permission
Run Code Online (Sandbox Code Playgroud)

然后我尝试运行gedit并得到了这个:

Unable to init server: Could not connect: Connection refused

(gedit:36): Gtk-WARNING **: 12:46:33.434: cannot open display:
Run Code Online (Sandbox Code Playgroud)

有人建议我可以通过远程桌面连接来使用它,但我希望它与系统集成。

Not*_*1ds 16

有人建议我可以通过远程桌面连接来使用它,但我希望它与系统集成。

虽然在 WSL 下运行 GUI 应用程序有多种选项,但唯一“与系统集成”的选项是 WSLg。虽然您没有提及您正在使用哪个 Windows 版本,但从第二个错误中可以看出您可能正在运行 Windows 10。

Windows 11 上的 WSL 引入了一项名为 WSLg 的新功能,该功能提供 Wayland/Weston/Xwayland 发行版,以便能够直接在 Windows 桌面上运行 GUI 应用程序。然而,讽刺的是,它确实在幕后使用远程桌面协议(带有扩展)来做到这一点。

随着 WSL 应用程序(从 Microsoft Store 安装)的发布,WSLg 现在也可供 Windows 10 用户使用,只要您的内部版本号以.2311或更高版本结尾。这应该包括 19042.2311、19043.2311、19044.2311 和 19045.2311 (Windows 10 22H2),但直到 22H2 才得到确认。目前,您需要适用于 Windows 10 的KB5020030可选预览更新。请确保您的 Windows 10 系统是最新的,然后继续执行可选更新,并且 KB5020030 应该可供安装。

Windows 10 和 Windows 11 用户都应该能够通过以下方式安装或更新:

wsl --install
# or, if already installed
wsl --update
# confirm 1.0.0 or later via:
wsl --version
Run Code Online (Sandbox Code Playgroud)

除了 WSLg 之外,还有另外两种可能的方法可以在 WSL 中运行 GUI 应用程序:

  • xrdp正如我在这个答案中提到的。当运行 Gnome 或 Xfce4 等 Linux 桌面时,这可能是更好的选择。
  • 第三方 XServer 如本答案中所示。