LoginSignup
9
8

More than 3 years have passed since last update.

WSL2環境構築トラブルシューティング

Last updated at Posted at 2020-08-16

/etc/wsl.conf が効かない

wsl.conf によって wsl起動時のmountやwindows PATH 環境変数の引き継ぎなどを設定できるが,
管理者権限のpower shell で明示的に再読み込みに相当と思われる処理を実行しないと, wsl.confファイルが有効にならない.
編集するたびに毎回Restart-Serviceが必要なので, 要注意である.

すべてのterminalを終了したのち, 管理者権限で実行(Ctrl + shift + Enterで実行)したpowershell 上で

Restart-Service LxssManager

設定値については
https://devblogs.microsoft.com/commandline/automatically-configuring-wsl/ に記述あり.
コメントにRestart-Serviceについて書かれている.

WSLのshell上でvscode を実行しようとしてもvscodeのGUIが開かない

気が付くと, 以下のような状態になっているかもしれない.

/home/your_home_directory/code .
Command is only available in WSL or inside a Visual Studio Code terminal. 

このメッセージは、vscode-server のcodeであるコマンドライン (CLI) 版が実行されていることを示している.
vscode-serverのコマンドラインとは, Remote-WSL (やRemote-SSH) でvscodeから接続したときに生成されるものである. 指定したdistributionのterminal上で、localのWindowsのvscode を実行しても生成される. これらはvscodeのGUIを起動し、Remote-WSLで接続するために必要なもののようである. vscodeのGUIから接続したとき, vscode中のterminalではenvを見るとPATHの先頭にvscode-serverのbinが指定されている.
では実行すべきcodeはどれかというと, localのWindowsのvscode である. これに対するpathが通っていれば良い.
特に何も設定していなければ自然にWindowsの%PATH%がdistributionの\$PATHに引き継がれているため, codeを実行できる. 何らかの\$PATHの変更処理を実施しているために生じる問題である.
対策としては, "/mnt/c/Program Files/Microsoft VS Code/bin" をWindows上のcodeのパスとすると,

export PATH=\$PATH:"/mnt/c/Program Files/Microsoft VS Code/bin" # double quote で囲むのがポイント.

\$PATHを通せばよい(\\$PATHとして$をエスケープしているのは, bashrc中で既存の設定を毎回引き継ぐため).
/etc/wsl.conf にて Windows の %PATH% を引き継ぐ設定にしていれば特に対応不要であるが, 恐らく引き継がない設定にしたうえで, 明示的に必要なパスを追加指定するほうが無難だろう.

参考

docker コマンドが見つからない

WSL2対応のdockerをインストールして, WSL Integration の設定がされていれば, distributionのPATH上でWindows上にインストールしたdockerとおそらく等価なexeを実行できる.

$ which docker
/usr/bin/docker
$ ls -l /usr/bin/
...
lrwxrwxrwx  1 root   root          48  8月  8 19:48  docker -> /mnt/wsl/docker-desktop/cli-tools/usr/bin/docker
lrwxrwxrwx  1 root   root          56  8月  8 19:48  docker-compose -> /mnt/wsl/docker-desktop/cli-tools/usr/bin/docker-compose
...

となっている. docker daemonが起動すると /mnt/wsl にファイルが配置されるようである.
docker daemonを起動していないと, docker コマンドは見つからない. /usr/bin/docker 自体はあるが, /mnt/wsl/docker-desktop/cli-tools/ までしか存在しないため.

参考

docker-compose 実行時にエラー

現象

> docker-compose up
Creating network "hello-world_default" with the default driver
Building hello
Traceback (most recent call last):
  File "bin/docker-compose", line 6, in <module>
  File "compose/cli/main.py", line 72, in main
  File "compose/cli/main.py", line 128, in perform_command
  File "compose/cli/main.py", line 1078, in up
  File "compose/cli/main.py", line 1074, in up
  File "compose/project.py", line 548, in up
  File "compose/service.py", line 367, in ensure_image_exists
  File "compose/service.py", line 1106, in build
  File "site-packages/docker/api/build.py", line 261, in build
  File "site-packages/docker/api/build.py", line 308, in _set_auth_headers
  File "site-packages/docker/auth.py", line 301, in get_all_credentials
  File "site-packages/docker/auth.py", line 287, in _get_store_instance
  File "site-packages/docker/credentials/store.py", line 25, in __init__
docker.credentials.errors.InitializationError: docker-credential-desktop.exe not installed or not available in PATH
[2300] Failed to execute script docker-compose

docker.credentials.errors.InitializationError: docker-credential-desktop.exe not installed or not available in PATH

ということなので, (上で明示的に追加していたvs code の他にも)PATHに追加しておく必要があった.

export PATH=$PATH:"/mnt/c/Program Files/Microsoft VS Code/bin:/mnt/c/Program Files/Docker/Docker/resources/bin"

参考

いつの間にかvscodeを使っていると, user/password 認証を使っているremote repository に対して git push 出来なくなった

現象

$ git push origin master
fatal: cannot run /home/your_home_directory/.vscode-server/bin/91899dcef7b8110878ea59626991a18c8a6a1b3e/extensions/git/dist/askpass.sh: そのようなファイルやディレクトリはありません
Username for 'https://github.com':

$ ls ~/.vscode-server/bin/
3dd905126b34dcd4de81fXXXXXXXXXXXXXXXXXX/

vscode-serverの下のフォルダ名が変わっている(一部ダミー値).
今回の場合は, vscodeのバージョンを上げて, vscode-serverが更新されたことが原因と思われる.

対策

  git config --global credential.helper cache
  git push origin master # などでusername/password 認証を実行. 以後はキャッシュされるらしく, username/password入力不要になる.

public/private key を使うべきという話ではある.

参考

9
8
1

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
9
8