【WSL】WSLを使ってLinux環境を構築する

WSL(Windows Subsystem for Linux)を使って、Windows 10/11にLinux環境を構築します。インストールするディストリビューションは既定のUbuntuです。
WSL(Windows Subsystem for Linux)
WSL1とWSL2
WSL(Windows Subsystem for Linux)は、Windows上でLinuxの動作環境を提供する仕組みです。開発者のツールとしてマイクロソフトから提供され、2017年10月にバージョン1(WSL1)、2019年2月にバージョン2(WSL2)がリリースされています。
WSL1は、LXCore/LXSSという2つのカーネルドライバによってLinuxシステムコールとWindowsシステムコールを変換するサブシステム、およびBashシェルなどのプログラムを提供するもので、Linux環境(Linuxカーネル)のエミュレーションとして動作します。WSL1は、少ないリソースで動作するメリットがある反面、未対応のシステムコールを利用するプログラムは動作しないなどのデメリットがあります。
WSL2は、WSL1の仕組みとは異なり、Windowsの仮想環境であるHyper-Vハイパーバイザー上で専用の仮想マシン(Lightweight Utility VM)として動作するため、完全なLinux環境(Linuxカーネル)であり、システムコールの完全な互換性を保っています。
本記事では、バージョン2(WSL2)を使ったLinux環境の構築について紹介します。インストールするディストリビューションは既定のUbuntuです。
WSLを使ったLinux環境の構築
前提条件
本記事で紹介するインストール手順の前提条件は次の通りです。
- Windows 10 バージョン 2004以上(OSビルド 19041以上)
- Windows 11
- Windows Server 2022
Windows 10のバージョンを確認するには[Windowsキー]+[R]を押して「winver」を実行します。2004以降なので、2004・20H2・21H1・21H2・22H2となります。
インストール
「スタート」ボタンを右クリックして、管理者のコマンドプロンプト、またはPowerShellを起動します。
- Windows 10 ・・・ コマンドプロンプト(管理者)
- Windows 11 ・・・ ターミナル(管理者)
- Windows Server 2022 ・・・ Windows PowerShell(管理者)
以下のコマンドを実行します。
1 | wsl --install |
コマンド実行後、自動的に「仮想マシンプラットフォーム」「Linux用Windowsサブシステム」「Ubuntu」がインストールされるのでしばらく待ちます。正常に終了した旨のメッセージが表示されたらシステムを再起動します。
1 2 3 4 5 6 7 | インストール中: 仮想マシン プラットフォーム 仮想マシン プラットフォーム はインストールされました。 インストール中: Linux 用 Windows サブシステム Linux 用 Windows サブシステム はインストールされました。 インストール中: Ubuntu Ubuntu はインストールされました。 要求された操作は正常に終了しました。変更を有効にするには、システムを再起動する必要があります。 |
再起動後、自動的にUbuntuが起動するのでしばらく待ちます。ユーザーアカウントを作成する旨のメッセージが表示されるので、ユーザー名(以下の例では「wsladmin」)とパスワードを入力します。ここで作成するユーザーアカウントが既定となり、Ubuntu起動時に自動的にログインされます。また、このユーザーアカウントはLinux管理者と見なされ、sudoコマンドが実行できます。
1 2 3 4 5 6 7 8 | Ubuntu は既にインストールされています。 Ubuntu を起動しています... 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:wsladmin New password: Retype new password: |
「Installation successful!」が表示されたら、インストールは完了です。もし、パスワードの再入力を間違った場合は、リトライして正しく設定してください。
1 2 3 4 | passwd: password updated successfully Installation successful! To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. |
作成したユーザーアカウントでログインした状態となります。プロンプトには「ユーザーアカウント名@ホスト名(以下の例では「wsladmin@hostname」)」が表示されます。
1 2 3 4 5 6 7 8 9 | Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.153.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/wsladmin/.hushlogin file. wsladmin@hostname:~$ |
パッケージの更新
インストールが完了したら、aptコマンドでパッケージの更新を行います。aptコマンドの実行には、root権限が必要なので「sudo apt update」と入力します。実行すると「/etc/apt/sources.list」に記述されたUbuntuの公式リポジトリーから、パッケージのバージョンや依存関係などが記述されたパッケージインデックスを取得して、更新可能なパッケージを確認します。パッケージインデックスの格納先は「/var/lib/apt/lists」です。以下の例では、更新可能なパッケージが128個見つかりました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | wsladmin@hostname:~$ sudo apt update [sudo] password for wsladmin: Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB] Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB] Get:4 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1769 kB] Get:5 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [127 kB] Get:6 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages [14.1 MB] Get:7 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [290 kB] Get:8 http://security.ubuntu.com/ubuntu jammy-security/main amd64 c-n-f Metadata [13.2 kB] (途中省略) Get:36 http://archive.ubuntu.com/ubuntu jammy-backports/main Translation-en [11.1 kB] Get:37 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 c-n-f Metadata [388 B] Get:38 http://archive.ubuntu.com/ubuntu jammy-backports/restricted amd64 c-n-f Metadata [116 B] Get:39 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [28.8 kB] Get:40 http://archive.ubuntu.com/ubuntu jammy-backports/universe Translation-en [16.5 kB] Get:41 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 c-n-f Metadata [672 B] Get:42 http://archive.ubuntu.com/ubuntu jammy-backports/multiverse amd64 c-n-f Metadata [116 B] Fetched 33.4 MB in 51s (649 kB/s) Reading package lists... Done Building dependency tree... Done Reading state information... Done 128 packages can be upgraded. Run 'apt list --upgradable' to see them. |
更新可能なパッケージは「sudo apt list --upgradable」で確認することができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | wsladmin@hostname:~$ sudo apt list --upgradable Listing... Done apport/jammy-updates 2.20.11-0ubuntu82.6 all [upgradable from: 2.20.11-0ubuntu82.5] apt-utils/jammy-updates 2.4.12 amd64 [upgradable from: 2.4.11] apt/jammy-updates 2.4.12 amd64 [upgradable from: 2.4.11] base-files/jammy-updates 12ubuntu4.6 amd64 [upgradable from: 12ubuntu4.4] bash/jammy-updates,jammy-security 5.1-6ubuntu1.1 amd64 [upgradable from: 5.1-6ubuntu1] bind9-dnsutils/jammy-updates,jammy-security 1:9.18.28-0ubuntu0.22.04.1 amd64 [upgradable from: 1:9.18.18-0ubuntu0.22.04.1] bind9-host/jammy-updates,jammy-security 1:9.18.28-0ubuntu0.22.04.1 amd64 [upgradable from: 1:9.18.18-0ubuntu0.22.04.1] (途中省略) vim-common/jammy-updates 2:8.2.3995-1ubuntu2.17 all [upgradable from: 2:8.2.3995-1ubuntu2.13] vim-runtime/jammy-updates 2:8.2.3995-1ubuntu2.17 all [upgradable from: 2:8.2.3995-1ubuntu2.13] vim-tiny/jammy-updates 2:8.2.3995-1ubuntu2.17 amd64 [upgradable from: 2:8.2.3995-1ubuntu2.13] vim/jammy-updates 2:8.2.3995-1ubuntu2.17 amd64 [upgradable from: 2:8.2.3995-1ubuntu2.13] wget/jammy-updates,jammy-security 1.21.2-2ubuntu1.1 amd64 [upgradable from: 1.21.2-2ubuntu1] xxd/jammy-updates 2:8.2.3995-1ubuntu2.17 amd64 [upgradable from: 2:8.2.3995-1ubuntu2.13] |
続けて「sudo apt upgrade」と入力します。更新の情報が表示された後、続けるかどうか聞いてくるので「Y」を入力します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | wsladmin@hostname:~$ sudo apt upgrade Reading package lists... Done Building dependency tree... Done Reading state information... Done Calculating upgrade... Done The following packages have been kept back: python3-update-manager ubuntu-advantage-tools ubuntu-pro-client-l10n update-manager-core The following packages will be upgraded: apport apt apt-utils base-files bash bind9-dnsutils bind9-host bind9-libs binutils binutils-common binutils-x86-64-linux-gnu bsdextrautils bsdutils busybox-static coreutils cpio curl distro-info distro-info-data dpkg e2fsprogs eject git git-man iptables irqbalance less libapt-pkg6.0 libbinutils libblkid1 libc-bin libc6 libcom-err2 libcryptsetup12 libctf-nobfd0 libctf0 libcurl3-gnutls libcurl4 libexpat1 libext2fs2 libglib2.0-0 libglib2.0-bin libglib2.0-data libgnutls30 libgssapi-krb5-2 libip4tc2 libip6tc2 libk5crypto3 libkrb5-3 libkrb5support0 libldap-2.5-0 libldap-common libmount1 libnetplan0 libnghttp2-14 libnss-systemd libpam-modules libpam-modules-bin libpam-runtime libpam-systemd libpam0g libperl5.34 libpython3-stdlib libpython3.10 libpython3.10-minimal libpython3.10-stdlib libsmartcols1 libsqlite3-0 libss2 libssh-4 libssl3 libsystemd0 libudev1 libuuid1 libuv1 libxml2 libxtables12 locales login logsave motd-news-config mount netplan.io openssh-client openssl passwd perl perl-base perl-modules-5.34 python-apt-common python3 python3-apport python3-apt python3-cryptography python3-distro-info python3-distupgrade python3-minimal python3-problem-report python3-software-properties python3-zipp python3.10 python3.10-minimal snapd software-properties-common systemd systemd-hwe-hwdb systemd-sysv systemd-timesyncd tar tcpdump tzdata ubuntu-minimal ubuntu-release-upgrader-core ubuntu-standard ubuntu-wsl udev util-linux uuid-runtime vim vim-common vim-runtime vim-tiny wget xxd 124 upgraded, 0 newly installed, 0 to remove and 4 not upgraded. 71 standard LTS security updates Need to get 101 MB of archives. After this operation, 1596 kB of additional disk space will be used. Do you want to continue? [Y/n] Y |
更新処理が終了するまでしばらく待ちます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 motd-news-config all 12ubuntu4.6 [4352 B] Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libc6 amd64 2.35-0ubuntu3.8 [3235 kB] Get:3 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 base-files amd64 12ubuntu4.6 [62.5 kB] Get:4 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 bash amd64 5.1-6ubuntu1.1 [769 kB] Get:5 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 bsdutils amd64 1:2.37.2-4ubuntu3.4 [80.9 kB] Get:6 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 coreutils amd64 8.32-4.1ubuntu1.2 [1437 kB] Get:7 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libnss-systemd amd64 249.11-0ubuntu3.12 [133 kB] Get:8 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libsystemd0 amd64 249.11-0ubuntu3.12 [319 kB] (途中省略) Fetched 101 MB in 27s (3816 kB/s) Extracting templates from packages: 100% Preconfiguring packages ... (Reading database ... 24208 files and directories currently installed.) Preparing to unpack .../motd-news-config_12ubuntu4.6_all.deb ... Unpacking motd-news-config (12ubuntu4.6) over (12ubuntu4.4) ... Preparing to unpack .../libc6_2.35-0ubuntu3.8_amd64.deb ... Unpacking libc6:amd64 (2.35-0ubuntu3.8) over (2.35-0ubuntu3.4) ... Setting up libc6:amd64 (2.35-0ubuntu3.8) ... (Reading database ... 24208 files and directories currently installed.) Preparing to unpack .../base-files_12ubuntu4.6_amd64.deb ... Unpacking base-files (12ubuntu4.6) over (12ubuntu4.4) ... Setting up base-files (12ubuntu4.6) ... Installing new version of config file /etc/issue ... Installing new version of config file /etc/issue.net ... (以下省略) |
プロンプトが表示されると完了です。再度「sudo apt update」で確認します。「All packages are up to date.」と表示されるとすべてのパッケージが更新されています。
1 2 3 4 5 6 7 8 | Hit:1 http://security.ubuntu.com/ubuntu jammy-security InRelease Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease Hit:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done All packages are up to date. |