tech

WindowsにWSL2をインストールしてLinux(Ubuntu)環境を構築する

WSL(Windows Subsystem for Linux)とはWindows上でLinuxの実行環境を構築できるシステムのことです。ここではそのインストール手順を記述しています。

WSLを使用するのに必要な機能を有効化します

1.Linux 用 Windows サブシステムを有効にする

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
PowerShell

2. 仮想マシンの機能を有効にする

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
PowerShell

なお上記の二つの機能の有効化はWindows システムツールのコントロール パネルからプログラム > Windowsの機能の有効化または無効化 で上記の項目にチェックを入れることでも可能です。

有効化作業が完了すると再起動をする必要があります。

Linux カーネル更新プログラム パッケージをインストールする

x64 マシン用 WSL2 Linux カーネル更新プログラム パッケージ(https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi)をダウンロードしてインストールします。

WSL 2 を既定のバージョンとして設定します。

wsl --set-default-version 2
PowerShell

WSLにUbuntu(Linux ディストリビューション)をインストールする

Microsoft Store(https://aka.ms/wslstore)を開き、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: <ユーザー名>
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 20.04.2 LTS (GNU/Linux 5.10.16.3-microsoft-standard-WSL2 x86_64)

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

  System information as of Sun Aug  1 16:08:31 JST 2021

  System load:  0.0                Processes:             8
  Usage of /:   0.4% of 250.98GB   Users logged in:       0
  Memory usage: 1%                 IPv4 address for eth0: 172.20.185.240
  Swap usage:   0%

1 update can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable


The list of available updates is more than a week old.
To check for new updates run: sudo apt update


This message is shown once a day. To disable it please create the
/home/<ユーザー名>/.hushlogin file.
PowerShell

動作確認

以下のコマンドでwslでUbuntuが動いているのを確認できます。

wsl --list --verbose
  NAME      STATE           VERSION
* Ubuntu    Running         2
PowerShell

LinuxからWindowsのファイルにアクセスするには、WindowsのCドライブがLinuxの/mnt/cにマウントされているので以下のコマンドでWindowsのCドライブに移動できます。

$ cd /mnt/c
Bash

逆にWindowsからLinux上のファイルにアクセスするには以下のコマンドでLinuxの配下に移動できます。

cd \\wsl$\Ubuntu
PowerShell

以下のように表示が変わります。

PS Microsoft.PowerShell.Core\FileSystem::\\wsl$\Ubuntu>
PowerShell

また、Linuxで以下のコマンドでWindowsのファイルエクスプローラーが立ち上がりフォルダをGUIで確認することができます。

explorer.exe .
PowerShell
return Tech