タイトル: WSL (Windows Subsystem for Linux)
SEOタイトル: WSL2 (Windows Subsystem for Linux 2) 完全ガイド
| この記事の要点 |
|
WSL (Windows Subsystem for Linux) とは
WSL は Microsoft が提供する Windows 上で Linux ユーザランドを動かす仕組み。WSL1 は Windows カーネルが Linux システムコールを変換する方式、WSL2 は軽量 Hyper-V VM 上で本物の Linux カーネル (Microsoft 配布) を動かす方式で、互換性・性能ともに大幅に向上しました。
| 項目 | WSL1 | WSL2 |
|---|---|---|
| カーネル | Windows (互換層) | ★ 本物の Linux カーネル |
| システムコール互換 | 一部不完全 | ★ 完全 |
| ファイル I/O (Linux 側) | 速い (Windows FS 直アクセス) | ★ 桁違いに速い (ext4 仮想ディスク) |
| ファイル I/O (Windows 側) | ★ 高速 | 遅い (9P 共有経由) |
| Docker 対応 | 制限あり | ★ Docker Desktop 統合 |
| GPU (CUDA) | 不可 | ★ 対応 |
| VPN との相性 | 良 | NAT 設定要 |
インストール
# 管理者 PowerShell
wsl --install
# → 仮想マシンプラットフォーム + WSL + Ubuntu を一括インストール
# → 再起動
# 起動後、初回ログインで UNIX ユーザ作成
# Enter new UNIX username: alice
# New password: ********
Windows 10 21H2 / 11 以降は wsl --install ワンライナーで完結。古い OS では Microsoft Store から「WSL」「Ubuntu」を別途インストールします。
ディストリビューションの管理
# インストール可能なディストロ一覧
wsl --list --online
wsl -l -o
# NAME FRIENDLY NAME
# Ubuntu Ubuntu
# Debian Debian GNU/Linux
# kali-linux Kali Linux Rolling
# openSUSE-Tumbleweed openSUSE Tumbleweed
# AlmaLinux-8 AlmaLinux OS 8
# AlmaLinux-9 AlmaLinux OS 9
# Ubuntu-22.04 Ubuntu 22.04 LTS
# Ubuntu-24.04 Ubuntu 24.04 LTS
# 指定インストール
wsl --install -d Ubuntu-24.04
wsl --install -d Debian
# インストール済一覧 (バージョン付)
wsl -l -v
# NAME STATE VERSION
# * Ubuntu Running 2
# Debian Stopped 2
# デフォルトディストロを変える
wsl --set-default Ubuntu-24.04
wsl -s Ubuntu-24.04
# デフォルトバージョンを WSL2 に
wsl --set-default-version 2
起動・停止・操作
# 起動
wsl # デフォルト
wsl -d Debian # 指定
wsl -d Ubuntu -u root # root で
# コマンドを 1 発実行
wsl ls -la /home
wsl -d Ubuntu -- bash -lc 'apt update'
# 全 WSL を停止 (メモリ解放)
wsl --shutdown
# 単一ディストロを停止
wsl --terminate Ubuntu
wsl -t Ubuntu
# 状態確認
wsl --status
# WSL カーネル更新
wsl --update
wsl --update --pre-release # ベータ
wsl --version
エクスポート / インポート / アンインストール
# ディストロ全体を tar 出力
wsl --export Ubuntu D:\backup\ubuntu.tar
# 別名で復元
wsl --import Ubuntu-Work D:\WSL\UbuntuWork D:\backup\ubuntu.tar
# 削除
wsl --unregister Ubuntu # 注意: そのディストロのデータが消える
ファイル共有
Linux → Windows ファイルにアクセス
# Windows のドライブは自動マウント
ls /mnt/c/Users/alice/Documents
ls /mnt/d/
# Windows パスを直接実行
explorer.exe . # 今いるディレクトリを Explorer で開く
code . # VS Code で開く (要 Remote-WSL)
notepad.exe sample.txt
Windows → Linux ファイルにアクセス
# エクスプローラのアドレス欄に
\\wsl$\Ubuntu\home\alice\
\\wsl.localhost\Ubuntu\home\alice\ (新形式)
# PowerShell から cd も可
cd \\wsl$\Ubuntu\home\alice
VS Code Remote-WSL
Microsoft 公式の WSL 拡張(旧 Remote - WSL)をインストール → ステータスバー左下の緑アイコン → "Connect to WSL" でシームレスに Linux 側のファイルを編集可能。
# WSL から VS Code を起動 (初回は自動セットアップ)
cd ~/project
code .
# 拡張機能は Linux 側に別途インストールされる (Windows 側とは分離)
Docker Desktop との統合
Docker Desktop は WSL2 バックエンドを選ぶと、Linux コンテナを WSL2 上の VM で動かします。Linux 側ターミナルからも docker / docker compose が透過利用可能。
# WSL 内部で
docker version
docker run --rm -it alpine sh
# Compose
docker compose up -d
リソース制限 (.wslconfig)
Windows 側の %UserProfile%\.wslconfig で WSL2 全体のメモリ / CPU / Swap を制限できます。
# C:\Users\alice\.wslconfig
[wsl2]
memory=8GB
processors=4
swap=2GB
swapFile=D:\\wsl\\swap.vhdx
localhostForwarding=true
# Windows 11 22H2 以降
networkingMode=mirrored # ホストとネットワークを共有
firewall=true
dnsTunneling=true
autoProxy=true
# WSL1 デフォルト一部 (per-distro)
[user]
default=alice
変更後は wsl --shutdown → 再度起動で反映。
WSLg: GUI アプリ対応
Windows 11 では WSLg が標準搭載され、Linux GUI アプリ (Firefox / gedit / GIMP 等) を直接 Windows デスクトップに表示できます。
sudo apt update
sudo apt install -y firefox gedit
firefox # ウィンドウが出る
gedit hello.txt # 同上
# X11 アプリのテスト
sudo apt install x11-apps
xeyes
GPU (CUDA) を使う
NVIDIA 公式 Windows ドライバ + CUDA Toolkit for WSL でGPU 計算ができます。Hyper-V 上の WSL2 から GPU が見えるのは画期的。
nvidia-smi # Windows 側ドライバ越しに GPU が見える
nvcc --version
# PyTorch + CUDA
pip install torch --index-url https://download.pytorch.org/whl/cu121
python -c "import torch; print(torch.cuda.is_available())"
systemd を使う
Windows 11 / 最新 WSL では /etc/wsl.conf で systemd を有効化できます。
# /etc/wsl.conf
[boot]
systemd=true
[user]
default=alice
[interop]
appendWindowsPath=true # PATH に Windows EXE を含める
有効化後は wsl --shutdown → 再起動で systemctl が利用可能。
FAQ
Q: WSL1 と WSL2 を混在できる?
A: 可能。wsl --set-version Ubuntu 1 / 2 で個別に変更できます。Windows 側ファイルを多用するなら WSL1、それ以外は WSL2 がほぼ最適。
Q: WSL2 のメモリ使用量が肥大化する
A: .wslconfig で memory= を設定し、長時間使ったら wsl --shutdown でリセットすると効果的。Windows 11 では未使用ページを返却する機能で改善されています。
Q: 企業 VPN で WSL2 のネット接続が切れる
A: 21H2 以前は典型的な問題。Windows 11 22H2+ で networkingMode=mirrored にすると VPN 配下でもホストと同等の経路を使えます。