LoginSignup
3
4

More than 1 year has passed since last update.

WSL2~WindowsでLinux~「その1:導入編」(旧バージョン)

Last updated at Posted at 2020-12-13

WSL2は「Windows Subsystem for Linux2」の略で、Windows上でLinuxが動くものです。
Linux(WSL2)がとても手軽だった為、導入手順をまとめました。

本記事は、Windows10のバージョン20H2以前の手順になります

1. はじめに

この記事の概要

  1. WLS2の導入手順
  2. 遭遇したエラーの回避方法

基本的にはマイクロソフトの手順でWSL2を導入しました。
その際、Linuxディストリビューション(Ubuntu-20.04)のインストールでエラーになった端末があった為、その回避方法について書き留めます。

記事の前提となる環境

  • 導入するディストリビューション
    Ubuntu-20.04(Windows Storeから導入)

  • 導入対象のホスト(エラーが発生した)

    • Windows10 Pro 64bit 1909(OSビルド 18363,1198)
      • uname -r の結果:4.19.128-microsoft-standard
      • CPU:Core-i7 5500U
      • プログラム:Windows Subsystem for Linux Update 4.19.128
    • Winodws10 Pro 20H2(OSビルド 19042,630)
  • 導入対象のホスト(こちらはエラーが発生しなかった)

    • Windows10 Pro 64bit 1909(OSビルド 18363,1198)
      • uname -r の結果:4.19.128-microsoft-standard
      • CPU:Core-i7-7500U
      • プログラム:Windows Subsystem for Linux Update 4.19.128
      • KB4023057

エラーに至る環境的な違いが明確にわからなかったものの、なんとなくウィルス対策ソフトが原因のような気がしています。
ご参考になればと思い掲載しています。

WSLの概要

2. 導入本編

WSL2の導入

(1) 管理者権限でプロンプトを起動

管理者権限でプロンプトを起動します。

powershell start-process cmd -verb runas

(2)システム要件を満たすかを確認する

x64 システムの場合: バージョン 1903 以降、 ビルド 18362 以上。
ARM64 システムの場合: バージョン 2004 以降、 ビルド 19041 以上。
18362 より前のビルドは WSL 2 をサポートしていません。とのこと。

バージョンとビルド番号を確認するにはプロンプトで「winver」を実行する。
Windows ロゴ キー + R キー を押して「winver」を実行してもよい。

winver

image.png

(3) Linux用Windowsサブシステムを有効にする

C:\>dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  • 実行結果
展開イメージのサービスと管理ツール
バージョン: 10.0.18362.1139

イメージのバージョン: 10.0.18363.1198

機能を有効にしています
[==========================100.0%==========================]
操作は正常に完了しました。

(4)仮想マシンの機能を有効にする

C:\>dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  • 実行結果
展開イメージのサービスと管理ツール
バージョン: 10.0.18362.1139

イメージのバージョン: 10.0.18363.1198

機能を有効にしています
[==========================100.0%==========================]
操作は正常に完了しました。

(5)再起動

再起動します。

C:\>shutdown /r /t 0 

(6)Linuxカーネル更新プログラムのインストール

(7)WSL2を既定のバージョンとして設定する

wsl --set-default-version 2
  • 実行結果
WSL 2 との主な違いについては、https://aka.ms/wsl2 を参照してください

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

  1. WindowsStoreにアクセスする。→Windows Store

  2. [Ubuntu 20.04 LTS]を選択してインストール。

    ※Ubuntsu20.04への直接リンク:https://www.microsoft.com/store/apps/9n6svws3rx71

(9)WSLを起動する。

WindowsStoreでUbuntsu20.04TLSの起動ボタンを押します。

うまくいけばusernameを尋ねられてLinuxのプロンプトが表示される。
筆者の場合は、以下のエラーに遭遇した。

Installing, this may take a few minutes...
WslRegisterDistribution failed with error: 0x80370114
Error: 0x80370114 ???????????????????????????????????

Press any key to continue...

以下、エラーの回避方法を2つ記載します。

3. トラブルシュート

2種類の方法を紹介しています。

回避方法1(vmcomputeの設定を変更する)

この回避方法、マイクロソフトのCommunityに投稿されてた方法です。
https://answers.microsoft.com/en-us/insider/forum/all/wsl-2-installing-linux-failed-error-code/bae391d1-4215-4d93-b0c4-3d96404a7c74?auth=1

(1)[スタート]→[設定]→[更新とセキュリティ]

image.png

(2)[Windowsセキュリティ]→[Windowsセキュリティを開く]

image.png

(3)[アプリとブラウザーコントロール]→[Exploit Protectionの設定]

image.png

(4)[プログラム設定]→[vmcompute.exe]→[編集]

image.png

(5)[制御フローガード(CFG)]

[システム設定の上書き]をオフにする
image.png

(6)サービスを起動する。

net start vmcompute

(7)個人的には念のためOSを再起動

(手順には再起動は指示されてないが、念のため)

C:\>shutdown /r /t 0

(8)再び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: dev
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

dev@pc:~$

回避方法2(WSL1で導入する)

WSL1モードで導入すればvmcomputeの変更は不要です。

(1)WSL1モードにする。

C:\> wsl --set-default-version 1

(2)起動する。

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

Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 4.4.0-18362-Microsoft x86_64)

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

  System information as of Fri Nov 27 12:15:55 JST 2020

  System load:    0.52      Users logged in:       0
  Usage of /home: unknown   IPv4 address for eth0: 172.16.91.38
  Memory usage:   41%       IPv4 address for eth2: 172.18.78.193
  Swap usage:     0%        IPv4 address for eth3: 172.17.234.113
  Processes:      7

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 once a day. To disable it please create the
/home/dev/.hushlogin file.

導入されたディストリビューションをWSLコマンドで確認する。(Windowsのコマンドプロンプトから確認)

C:\> wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-20.04    Stopped         1

以上でインストール出来ましたでしょうか?

4. 操作例

WSLの操作

ディストリビューションの一覧

C:\> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-20.04           Running         2

ディストリビューションを指定して起動

C:\> wsl -d Ubuntu-20.04
dev@pc:/$ ps
  PID TTY          TIME CMD
  262 pts/2    00:00:00 bash
  275 pts/2    00:00:00 ps
dev@pc:/$ uname -r
4.19.128-microsoft-standard
dev@pc:/$ cat /etc/issue
Ubuntu 20.04.1 LTS \n \l

LinuxからWindowsのファイルを見る

Cドライブは/mntにマウントされている。

C:\> wsl -d Ubuntu-20.04
dev@pc:/$ cd /mnt
dev@pc:/$ ls -l
total 4
drwxrwxrwx 1 dev     dev     4096 Dec 10 08:15 c
drwxrwxrwx 1 dev     dev     4096 Dec 10 08:14 d
drwxrwxrwx 2 root    root    4096 Nov 25 22:24 m
drwxrwxrwt 5 root    root     100 Dec 12 21:16 wsl

WSLのバージョンを変換する

WSL1で作成した後、WSL2にする例。

C:\> wsl --set-version Ubuntu-20.04 2

ディストリビューションのエクスポート

wsl --export <ディストリビューション> <ファイル名>

C:\> wsl --export Ubuntu-20.04 c:\work\Ubuntu-20.04.tar

ディストリビューションのインポート(コピー)

wsl --import <ディストリビューション> <インストール場所> <ファイル名> [オプション]

C:\> wsl --import MyUbuntu-20.04-dev c:\work\MyUbuntu-20.04-dev c:\work\Ubuntu-20.04.tar
C:\> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-20.04           Stopped         2
  MyUbuntu-20.04-dev     Stopped         2
C:\> dir /B c:\work\MyUbuntu-20.04-dev
ext4.vhdx

ディストリビューションの削除

C:\> wsl --unregister <ディストリビューション>

WSLが有効になっているか確認する方法。

管理者権限でパワーシェルを起動する。

powershell start-process powershell -verb runas

WSLの有効性を確認するコマンドを実行する。

Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

実行結果

FeatureName      : Microsoft-Windows-Subsystem-Linux
DisplayName      : Linux 用 Windows サブシステム
Description      : ネイティブなユーザー モードの Linux シェルおよびツールを Windows で実行するためのサービスと環境を提
                   供します。
RestartRequired  : Possible
State            : Enabled
CustomProperties :
                   ServerComponent\Description : ネイティブなユーザー モードの Linux シェルおよびツールを Windows で実
                   行するためのサービスと環境を提供します。
                   ServerComponent\DisplayName : Linux 用 Windows サブシステム
                   ServerComponent\Id : 1033
                   ServerComponent\Type : Feature
                   ServerComponent\UniqueName : Microsoft-Windows-Subsystem-Linux
                   ServerComponent\Deploys\Update\Name : Microsoft-Windows-Subsystem-Linux

参考:https://docs.microsoft.com/ja-jp/windows/wsl/troubleshooting

5. まとめ

Windows上でLinuxをかなり気軽に使えると思います。
DockerDesktopを導入する場合は、WSL1ではなくWSL2で導入するようにしてください。
WindowsStoreで使えるディストリビューションは限定的ですが増える事を期待しています。

6. 参考文献

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4