takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

Windows11 に WSL2 Ubuntu 環境構築

検証環境

Windows11 Home Edition (version 22H2)

WSL の有効化

PowerShell を管理者として実行
下記のコマンドを入力

PS C\> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

処理終了後、Windows を再起動

仮想マシンプラットフォームの有効化

チェックを入れて OK を押した後、再起動する

インストール可能なディストリビューション確認

PS C:\> wsl --list --online
インストールできる有効なディストリビューションの一覧を次に示します。
'wsl --install -d <Distro>' を使用してインストールします。

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
OracleLinux_8_5                        Oracle Linux 8.5
OracleLinux_7_9                        Oracle Linux 7.9
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
openSUSE-Leap-15.4                     openSUSE Leap 15.4
openSUSE-Tumbleweed                    openSUSE Tumbleweed

Ubuntu のインストール

PowerShell を管理者として実行
下記コマンドを入力

PS C\> wsl --set-default-version 2

PS C\> wsl --install -d Ubuntu-22.04

インストール後、Ubuntu 起動時に下記エラーが発生した場合

WslRegisterDistribution failed with error: 0x800701bc

下記コマンドを入力することでエラーが解消する

PS C\> wsl --update

ログインユーザー登録

Ubuntu インストール後、初回起動時にログインユーザーを登録する

PS C:\> wsl --install -d Ubuntu-22.04
Ubuntu 22.04 LTS を起動しています...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: takaya030
New password:
Retype new password:
passwd: password updated successfully
この操作を正しく終了しました。

その後、下記メッセージが表示されればインストール成功

Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.90.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


This message is shown once a day. To disable it please create the
/home/takaya030/.hushlogin file.

/etc/wsl.conf の変更

デフォルトでは Windows のパス設定が PATH に追加されるのでそれを無効化する

/etc/wsl.conf を下記内容に変更する

[boot]
systemd=true

[interop]
appendWindowsPath=false

Ubuntu をログアウト後、PowerShell で下記コマンドを実行して Ubuntu を再起動する

PS C\> wsl --shutdown
PS C\> wsl

参考サイト

memoryclip.uchiida.com

leokun0210.hatenablog.com