WSL2を使って、Windows10にUbuntu環境を構築しました。


1 はじめに

会社でWindowsのPCが貸与されたのと、自分のパソコンにもUbuntu環境が欲しくなったので、環境構築しました。

2 WSL2とは

Windows Subsystem for Linux (WSL) とは、Linuxの(ELFフォーマット形式の)バイナリ実行ファイルをWindows 10およびWindows Server上でネイティブ実行するための互換レイヤーである。
引用元:Windows Subsystem for Linux - Wikipedia


従来、Windows OSでLinuxを使おうとすれば、仮想マシン環境として導入する必要があった。しかし、仮想マシン環境は動作のためのオーバーヘッドがあり、また起動にも時間がかかる。(中略)WSL自体の起動負荷がわずかで、起動時間も極めて短い。
引用元:【WSL入門】第1回 Windows 10標準Linux環境WSLを始めよう:ITの教室 - @IT

WSL2はWSLの新しいほうです。
Windows Subsystem for Linuxの略で、これを使うと、仮想マシンを起動しなくてもWindows上でパフォーマンスの高いLinuxの環境が作れちゃうというわけですね。

3 環境構築

以下のサイトを参考に、環境構築をしていきます。

Windows 10でLinuxを使う(WSL2) - Qiita

3.1 WSL2のインストール

管理者として以下を実行し、WSLを有効化します。

PS C:\windows\system32> dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

展開イメージのサービスと管理ツール
バージョン: 10.0.18362.1139

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

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

次に、Virtual Machine Platformを有効化します。
再起動しますかでYを選び再起動してください。

PS C:\windows\system32> dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all

展開イメージのサービスと管理ツール
バージョン: 10.0.18362.1139

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

機能を有効にしています
[==========================100.0%==========================]
操作は正常に完了しました。
Windows を再起動してこの操作を完了してください。
今すぐコンピューターを再起動しますか? (Y/N)

これでWSLをインストールする準備ができました。
ダウンロードサイトに行きましょう。

WSL 2 Linux カーネルの更新 | Microsoft Docs

「最新の WSL2 Linux カーネル更新プログラム パッケージをダウンロード」をクリックし、インストールまで行ってください。適当にNextを押していれば終わります。

ここで、もう一度PowerShellを開き、以下のコマンドを実行し、デフォルトでWSL2を使うように設定します。

PS C:\Users\komums> wsl --set-default-version 2
WSL 2 との主な違いについては、https://aka.ms/wsl2 を参照してください

3.2 Ubuntuのダウンロード

ここまでの作業により、ついにUbuntuが使えるようになりました。
Windows Storeからインストールします。

Ubuntu を入手 - Microsoft Store ja-JP

インストールが終了したら、起動しましょう。
初回起動時にUbuntu用ユーザIDとパスワードを設定して、環境構築終了です。

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: komums
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.1 LTS (GNU/Linux 4.19.128-microsoft-standard x86_64)

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

  System information as of Tue Nov 17 08:24:47 JST 2020

  System load:  0.13               Processes:             8
  Usage of /:   0.4% of 250.98GB   Users logged in:       0
  Memory usage: 0%                 IPv4 address for eth0: 192.168.28.107
  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 once a day. To disable it please create the
/home/komums/.hushlogin file.

あとはWindowsTerminalなりVSCodeなりインストールして楽しいWSLライフをお楽しみください!

4 最後に

これで今後またWindowsPCを買っても安心です。

Twitterのフォローもお願いします!