久々にWindows11にVSCode、WSL2の開発環境を整備する
前説
数年ぶりにWindow環境にWSL2の開発環境を整備したのでメモ。
WSL2は標準のUbuntu22.04 LTSを設定します。
注:書いている本人は業務でPC、OSを選べる時はXubuntu、docker(Not Docker Desktop)を入れて使っているので、WSL2も極力ubuntu、dockerメインで環境を作ってます。
PowershellのUpdate
Windows StoreからPowerShellをインストールする。(素の状態だと古いPowerShellが入っているのでアップデートしろと怒られる)
wsl2のインストール
ターミナルなどから
wsl --install -d Ubuntu-22.04
※以前インストールしたUbuntu22.04のID,PASSをど忘れしたので、Ubuntu22.04を一度アンインストールしてから入れ直しました💦
WSL2のUbuntuのUpdate
sudo apt update
sudo apt upgrade
Win側のVSCode,dev containerインストール
WSL2側のDockerを使って開発環境を整備するのが目的なので、Win側にVSCodeをインストール、機能拡張にRemote Development拡張機能パックをインストール
WSL2にDockerをインストール
sudo apt-get update
sudo apt-get install -y wget apt-transport-https ca-certificates curl gnupg lsb-release
# Dockerの公式GPGキー
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Dockerのリポジトリを追加
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# dockerインストール
# docker compose V2に移行しているのでdocker-composeのインストールは不要
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
# ユーザをdockerグループに追加する
sudo usermod -aG docker $USER
WSL2のdockerを自動起動する設定
# Ubuntu22.04LTSではsystemdは有効化していた以下の設定のみ
sudo systemctl enable docker
Windows側からWSL2を再起動
wsl --shutdown
WSL2のsystemctlがエラーになる場合
systemctl
> System has not been booted with systemd as init system (PID 1). Can't operate.
> Failed to connect to bus: Host is down
古いWSL2(Ubuntu20.04)を入れていたりすると起きるようです。
$ wsl -l
Ubuntu-20.04 (既定)
Ubuntu-22.04
wslの既定を22.04に変更します
$ wsl -s Ubuntu-22.04
$ wsl -l
# Linux 用 Windows サブシステム ディストリビューション:
# Ubuntu-22.04 (既定)
# Ubuntu-20.04
他にWindows再起動後にWSLが古いという旨の警告が出たので
wsl --update
wsl --shutdown
をしてみたら直りました💦
WSL2からvscodeが起動できない時
WSL2側からVSCodeを実行すると何故かエラーになった
$ code .
Resolving update.code.visualstudio.com (update.code.visualstudio.com)... failed: Temporary failure in name resolution.
wget: unable to resolve host address ‘update.code.visualstudio.com’
ERROR: Failed to download https://update.code.visualstudio.com/commit:f30a9b73e8ffc278e71575118b6bf568f04587c8/server-linux-x64/stable to /home/USER1/.vscode-server/bin/f30a9b73e8ffc278e71575118b6bf568f04587c8-1615290693.tar.gz
どうもDNSの名前解決が出来ていないらしい
$ cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.17.80.225
nameserverにGoogle Public DNSを設定
nameserver 8.8.8
したら直ったっぽい。
WSL2から
$ code .
をし直したらubuntu側のvscodeがアップデートされた。
長くなったのでdev container関連は別ページでまとめます。
参考
wsl2 docker関連
wsl2でvscodeがエラーになる件(DNS名前解決)https://qiita.com/kkato233/items/1fc71bde5a6d94f1b982.
Discussion