Technically Impossible

Lets look at the weak link in your statement. Anything "Technically Impossible" basically means we haven't figured out how yet.

Ubuntu 20.04 LTS+WSL2でGnomeデスクトップを起動するまで。


gnome-sessionはSystemdに依存しており、WSLはSystemdを採用していない。そこでgnome-shellを直接起動し、無理矢理GNOMEデスクトップ環境を表示しているのが、この投稿で紹介している方法だ。

もしGNOMEにこだわる必要が無ければ、以下の選択肢もあることを最初にお伝えしておきたい。
impsbl.hatenablog.jp
impsbl.hatenablog.jp
impsbl.hatenablog.jp

この投稿では、WSL 2 + Ubuntu 20.04 LTSで最低限のGnomeデスクトップ環境を導入し、起動するまでの手順を紹介する。あくまでも起動するまでを目的としており、日本語対応やMaXX Interactive Desktopの導入は、別の機会に投稿する。
またデスクトップ環境は、Windows上で動作するX serverを通じて利用することを前提としており、リモートデスクトップ接続やVNCでの利用は、この投稿の範囲外の話題だ。

WSL2、Ubuntu 20.04 LTSがすでに導入されている環境を前提としており、それらの導入手順は紹介しない。
特にUbuntu 20.04 LTSは導入し、ユーザーとパスワードの設定を終えた直後の環境を想定している。必要ならば、次の投稿を参考にしてイメージを「リセット」してほしい。
impsbl.hatenablog.jp

X server

何らかのX serverをWindowsで導入しておく必要がある。次の3つが有名どころだ。

🔎X serverの有名どころ
sourceforge.net
mobaxterm.mobatek.net
x410.dev

私はMobaXtermを利用している。ターミナルとX serverが統合されているだけでなく、様々なセッションに対応しており、とにかく多機能だ。WSL環境への対応も想定されている。
🔎画像 - MobaXterm



事前確認

作業を始める前に、UbuntuがWSL2での稼働を前提に実装されているかを確認する。PowerShellで次のコマンドを実行し、VERSION欄に”2”と表示されていれば次の手順へ進むことができる。
”1”の場合は、WSL2への変換をする必要がある。
PowerShellは管理者として実行することに注意。

#バージョン確認
wsl --list --verbose

#変換
wsl --set-version <distribution name> <versionNumber>

aptミラー・サイトの設定、aptパッケージの更新

Ubuntuにログインしたら、まずaptレポジトリの参照先を日本国内のミラー・サイトへ変更する。具体的には、次のURLに登録されている一覧を参照するようにする。

http://mirrors.ubuntu.com/mirrors.txt

これはパッケージ・ダウンロードに伴う通信を日本国内で完結し、導入作業の効率を上げるための措置だ。
ミラー・サイトへ変更したところで、レポジトリを更新する。一連のコマンドは次のようになる。

sudo sed -i.bak -e 's%http://[^ ]\+%mirror://mirrors.ubuntu.com/mirrors.txt%g' /etc/apt/sources.list

sudo apt update
sudo apt upgrade

一連のコマンド実行直後の状態で導入されているubuntux11gnome関連パッケージ一覧の出力を、投稿末尾の「パッケージ一覧」に掲載している。
この時点では、gnome関連パッケージは一切導入されていないことが分かる。

Gnomeデスクトップの導入

ここではUbuntuデスクトップとして最低限のパッケージがまとめられた”ubuntu-desktop-minimal”をインストールする。次のコマンドを実行する。

sudo apt install ubuntu-desktop-minimal

先程と同様、コマンド実行直後の状態で導入されているubuntux11gnome関連パッケージ一覧の出力を、投稿末尾の「パッケージ一覧」に掲載している。
ここで多くのgnome関連パッケージが導入される。

X serverの指定、.profileの編集

WSL2が稼働しているWindows環境、そしてUbuntuが動作しているWSL2は、物理的には同じノード上で動作しているのだが、ネットワーク上では異なるアドレスが割り当てられている。そのため、Ubuntu上で127.0.0.1localhostを指定したとき、暗にWindows側を指定することができたのだが、それはWSL1までの話だ。
WSL2では、明示的にWindows側のアドレスを指定しなければならない。ここではX serverのアドレスということになる。

WindowsのアドレスがDHCPによって動的割当される環境では、そのアドレスをUbuntu起動時に取得して、X serverの指定に用いる必要がある。そこでWSLならではのコマンドを実行する。

export DISPLAY=$(ipconfig.exe | grep -m 1 IPv4 | awk '{print $14}' | awk 'sub(/\r$/,"")'):0.0

Windows側のコマンドであるipconfigをWSL上で実行し、続けてLinuxコマンド群を実行してWindowsのアドレスを取得する。最終的に環境変数DISPLAYへ”192.168.1.1:0.0”のような情報を登録する。
これを”~/.profile”末尾に記録しておく。

gnome起動用スクリプトの作成

gnomeを起動するためのシェル・スクリプトを作成する。Ubuntuを落とすことなく、X serverの開始、停止を繰り返せるように、関連サービスは”start”ではなく、”restart”するようにした。
次のコマンドを、シェル・スクリプトファイルとして保存する。私は”win.sh”とした。

sudo service dbus restart
sudo service x11-common restart
gnome-shell --x11 -r

Ubuntuの再起動、そしてGnomeの起動

ここで一度、Ubuntuを再起動させる。コンソールからログオフしたら、PowerShellで次のコマンドを実行する。State欄が”Stopped”と表示されていることを確認する。
PowerShellは管理者として実行することに注意。

wsl --terminate Ubuntu
wsl --list --verbose

ここでX serverを起動しておく。
起動を確認したら、改めてUbuntuにログインする。ログイン後、先のシェル・スクリプトを実行すると、Gnomeデスクトップが起動する。この投稿の冒頭の画像だ。
画像では、”Display Settings”を開いている。X serverのWindowサイズが、解像度として適切に反映されていることが分かる。

なお”gnome-shell”起動時に、いくつかのエラーが出力されているが、実用上の問題は生じていない。

🔎エラー出力

wsluser@paulownia:~$ ./win.sh
 * Stopping system message bus dbus                                                                                                                                                                       [ OK ]
 * Starting system message bus dbus                                                                                                                                                                       [ OK ]
 * Setting up X socket directories...                                                                                                                                                                     [ OK ]
Xlib:  extension "DPMS" missing on display "192.168.1.7:0.0".
Xlib:  extension "DPMS" missing on display "192.168.1.7:0.0".

(gnome-shell:765): Gjs-WARNING **: 20:11:56.442: Some code accessed the property 'CredentialManager' on the module 'credentialManager'. That property was defined with 'let' or 'const' inside the module. This was previously supported, but is not correct according to the ES6 standard. Any symbols to be exported from a module must be defined with 'var'. The property access will work as previously for the time being, but please fix your code anyway.
GNOME Shell-Message: 20:11:56.947: Failed to register AuthenticationAgent
GNOME Shell-Message: 20:11:56.950: Telepathy is not available, chat integration will be disabled.

(gnome-shell:765): Gjs-WARNING **: 20:11:57.125: JS ERROR: error initializing the NetworkManager Agent: NM.SecretAgentError: NetworkManager is not running
NetworkAgent/<@resource:///org/gnome/shell/ui/components/networkAgent.js:626:30

GNOME Shell-Message: 20:11:57.128: Error looking up permission: GDBus.Error:org.freedesktop.portal.Error.NotFound: No entry for geolocation
GNOME Shell-Message: 20:11:57.804: GNOME Shell started at Sun Apr 04 2021 20:11:56 GMT+0900 (JST)
GNOME Shell-Message: 20:11:57.805: Registering session with GDM
GNOME Shell-Message: 20:11:57.878: Error registering session with GDM: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.DisplayManager was not provided by any .service files


余談

Ubuntu + WSL2はノートPC用のデスクトップ環境として良好、何より画面いっぱいにデスクトップ利用できるのが良い、と言う話題。
X serverとしてVcXsrv、MobaXtermを用い、MaXX Interactive Desktopを動かしている画像も掲載している。
impsbl.hatenablog.jp

パッケージ一覧

apt update, apt upgrade、実行直後

🔎出力結果

wsluser@paulownia:~$ apt list --installed | grep ubuntu-

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

fonts-ubuntu-console/focal,now 0.83-4ubuntu1 all [installed,automatic]
plymouth-theme-ubuntu-text/focal-updates,now 0.9.4git20200323-0ubuntu6.2 amd64 [installed,automatic]
ubuntu-advantage-tools/focal,now 20.3 amd64 [installed,automatic]
ubuntu-keyring/focal-updates,now 2020.02.11.4 all [installed,automatic]
ubuntu-minimal/focal-updates,now 1.450.2 amd64 [installed]
ubuntu-release-upgrader-core/focal-updates,now 1:20.04.30 all [installed,automatic]
ubuntu-server/focal-updates,now 1.450.2 amd64 [installed]
ubuntu-standard/focal-updates,now 1.450.2 amd64 [installed]
ubuntu-wsl/focal-updates,now 1.450.2 amd64 [installed]
wsluser@paulownia:~$ apt list --installed | grep x11

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

dbus-x11/focal-updates,focal-security,now 1.12.16-2ubuntu2.1 amd64 [installed]
libx11-6/focal-updates,focal-security,now 2:1.6.9-2ubuntu1.1 amd64 [installed,automatic]
libx11-data/focal-updates,focal-security,now 2:1.6.9-2ubuntu1.1 all [installed,automatic]
libx11-xcb1/focal-updates,focal-security,now 2:1.6.9-2ubuntu1.1 amd64 [installed]
x11-common/focal,now 1:7.7+19ubuntu14 all [installed]
x11-utils/focal,now 7.7+5 amd64 [installed]
wsluser@paulownia:~$ apt list --installed | grep gnome

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

wsluser@paulownia:~$


apt install ubuntu-desktop-minimal、実行直後

🔎出力結果

wsluser@paulownia:~$ apt list --installed | grep ubuntu-

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

fonts-ubuntu-console/focal,now 0.83-4ubuntu1 all [installed,automatic]
gnome-shell-extension-ubuntu-dock/focal-updates,now 68ubuntu1~20.04.1 all [installed,automatic]
gsettings-ubuntu-schemas/focal,now 0.0.7+17.10.20170922-0ubuntu1 all [installed,automatic]
plymouth-theme-ubuntu-text/focal-updates,now 0.9.4git20200323-0ubuntu6.2 amd64 [installed,automatic]
ubuntu-advantage-tools/focal,now 20.3 amd64 [installed,automatic]
ubuntu-desktop-minimal/focal-updates,now 1.450.2 amd64 [installed]
ubuntu-docs/focal-updates,now 20.04.3 all [installed,automatic]
ubuntu-drivers-common/focal-updates,now 1:0.8.6.5~0.20.04.1 amd64 [installed,automatic]
ubuntu-keyring/focal-updates,now 2020.02.11.4 all [installed,automatic]
ubuntu-minimal/focal-updates,now 1.450.2 amd64 [installed]
ubuntu-mono/focal,now 19.04-0ubuntu3 all [installed,automatic]
ubuntu-release-upgrader-core/focal-updates,now 1:20.04.30 all [installed,automatic]
ubuntu-release-upgrader-gtk/focal-updates,now 1:20.04.30 all [installed,automatic]
ubuntu-report/focal,now 1.6.1 amd64 [installed,automatic]
ubuntu-server/focal-updates,now 1.450.2 amd64 [installed]
ubuntu-session/focal,now 3.36.0-2ubuntu1 all [installed,automatic]
ubuntu-settings/focal,now 20.04.5 all [installed,automatic]
ubuntu-standard/focal-updates,now 1.450.2 amd64 [installed]
ubuntu-wallpapers-focal/focal,now 20.04.2-0ubuntu1 all [installed,automatic]
ubuntu-wallpapers/focal,now 20.04.2-0ubuntu1 all [installed,automatic]
ubuntu-wsl/focal-updates,now 1.450.2 amd64 [installed]
wsluser@paulownia:~$ apt list --installed | grep x11

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

dbus-x11/focal-updates,focal-security,now 1.12.16-2ubuntu2.1 amd64 [installed]
libx11-6/focal-updates,focal-security,now 2:1.6.9-2ubuntu1.1 amd64 [installed,automatic]
libx11-data/focal-updates,focal-security,now 2:1.6.9-2ubuntu1.1 all [installed,automatic]
libx11-protocol-perl/focal,now 0.56-7 all [installed,automatic]
libx11-xcb1/focal-updates,focal-security,now 2:1.6.9-2ubuntu1.1 amd64 [installed]
libxkbcommon-x11-0/focal,now 0.10.0-1 amd64 [installed,automatic]
x11-apps/focal,now 7.7+8 amd64 [installed,automatic]
x11-common/focal,now 1:7.7+19ubuntu14 all [installed]
x11-session-utils/focal,now 7.7+4 amd64 [installed,automatic]
x11-utils/focal,now 7.7+5 amd64 [installed]
x11-xkb-utils/focal,now 7.7+5 amd64 [installed,automatic]
x11-xserver-utils/focal,now 7.7+8 amd64 [installed,automatic]
wsluser@paulownia:~$ apt list --installed | grep gnome

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

gir1.2-gnomebluetooth-1.0/focal-updates,now 3.34.3-0ubuntu1 amd64 [installed,automatic]
gir1.2-gnomedesktop-3.0/focal-updates,now 3.36.8-0ubuntu1 amd64 [installed,automatic]
gnome-accessibility-themes/focal,now 3.28-1ubuntu1 all [installed,automatic]
gnome-bluetooth/focal-updates,now 3.34.3-0ubuntu1 amd64 [installed,automatic]
gnome-calculator/focal,now 1:3.36.0-1ubuntu1 amd64 [installed,automatic]
gnome-characters/focal,now 3.34.0-1 amd64 [installed,automatic]
gnome-control-center-data/focal-updates,now 1:3.36.5-0ubuntu1 all [installed,automatic]
gnome-control-center-faces/focal-updates,now 1:3.36.5-0ubuntu1 all [installed,automatic]
gnome-control-center/focal-updates,now 1:3.36.5-0ubuntu1 amd64 [installed,automatic]
gnome-desktop3-data/focal-updates,now 3.36.8-0ubuntu1 all [installed,automatic]
gnome-disk-utility/focal,now 3.36.1-1ubuntu1 amd64 [installed,automatic]
gnome-font-viewer/focal,now 3.34.0-2 amd64 [installed,automatic]
gnome-getting-started-docs/focal-updates,now 3.36.2-0ubuntu0.1 all [installed,automatic]
gnome-initial-setup/focal-updates,now 3.36.2-0ubuntu2 amd64 [installed,automatic]
gnome-keyring-pkcs11/focal,now 3.36.0-1ubuntu1 amd64 [installed,automatic]
gnome-keyring/focal,now 3.36.0-1ubuntu1 amd64 [installed,automatic]
gnome-logs/focal-updates,now 3.34.0-1ubuntu1 amd64 [installed,automatic]
gnome-menus/focal,now 3.36.0-1ubuntu1 amd64 [installed,automatic]
gnome-online-accounts/focal,now 3.36.0-1ubuntu1 amd64 [installed,automatic]
gnome-power-manager/focal,now 3.32.0-2 amd64 [installed,automatic]
gnome-screenshot/focal,now 3.36.0-1ubuntu1 amd64 [installed,automatic]
gnome-session-bin/focal,now 3.36.0-2ubuntu1 amd64 [installed,automatic]
gnome-session-canberra/focal,now 0.30-7ubuntu1 amd64 [installed,automatic]
gnome-session-common/focal,now 3.36.0-2ubuntu1 all [installed,automatic]
gnome-session/focal,now 3.36.0-2ubuntu1 all [installed,automatic]
gnome-settings-daemon-common/focal-updates,now 3.36.1-0ubuntu1 all [installed,automatic]
gnome-settings-daemon/focal-updates,now 3.36.1-0ubuntu1 amd64 [installed,automatic]
gnome-shell-common/focal-updates,now 3.36.7-0ubuntu0.20.04.1 all [installed,automatic]
gnome-shell-extension-appindicator/focal-updates,now 33.1-0ubuntu0.20.04.1 all [installed,automatic]
gnome-shell-extension-desktop-icons/focal-updates,now 20.04.0-3~ubuntu20.04.1 all [installed,automatic]
gnome-shell-extension-ubuntu-dock/focal-updates,now 68ubuntu1~20.04.1 all [installed,automatic]
gnome-shell/focal-updates,now 3.36.7-0ubuntu0.20.04.1 amd64 [installed,automatic]
gnome-startup-applications/focal,now 3.36.0-2ubuntu1 amd64 [installed,automatic]
gnome-system-monitor/focal-updates,now 3.36.1-0ubuntu0.20.04.1 amd64 [installed,automatic]
gnome-terminal-data/focal-updates,now 3.36.2-1ubuntu1~20.04 all [installed,automatic]
gnome-terminal/focal-updates,now 3.36.2-1ubuntu1~20.04 amd64 [installed,automatic]
gnome-themes-extra-data/focal,now 3.28-1ubuntu1 all [installed,automatic]
gnome-themes-extra/focal,now 3.28-1ubuntu1 amd64 [installed,automatic]
gnome-user-docs/focal-updates,now 3.36.2+git20200704-0ubuntu0.1 all [installed,automatic]
language-selector-gnome/focal-updates,now 0.204.2 all [installed,automatic]
libgnome-autoar-0-0/focal-updates,focal-security,now 0.2.3-2ubuntu0.2 amd64 [installed,automatic]
libgnome-bluetooth13/focal-updates,now 3.34.3-0ubuntu1 amd64 [installed,automatic]
libgnome-desktop-3-19/focal-updates,now 3.36.8-0ubuntu1 amd64 [installed,automatic]
libgnome-menu-3-0/focal,now 3.36.0-1ubuntu1 amd64 [installed,automatic]
libgnomekbd-common/focal,now 3.26.1-1 all [installed,automatic]
libgnomekbd8/focal,now 3.26.1-1 amd64 [installed,automatic]
libpam-gnome-keyring/focal,now 3.36.0-1ubuntu1 amd64 [installed,automatic]
libsoup-gnome2.4-1/focal,now 2.70.0-1 amd64 [installed,automatic]
nautilus-extension-gnome-terminal/focal-updates,now 3.36.2-1ubuntu1~20.04 amd64 [installed,automatic]
network-manager-gnome/focal-updates,now 1.8.24-1ubuntu3 amd64 [installed,automatic]
network-manager-openvpn-gnome/focal,now 1.8.12-1 amd64 [installed,automatic]
network-manager-pptp-gnome/focal,now 1.2.8-2 amd64 [installed,automatic]
pinentry-gnome3/focal,now 1.1.0-3build1 amd64 [installed,automatic]
yaru-theme-gnome-shell/focal-updates,now 20.04.10.1 all [installed,automatic]
wsluser@paulownia:~$