'WSL isn't working in VS code. What could be the problem here?

Hello I'm newbie using an Linux environment and VS code. I've used windows terminal to access on WSL2 and it worked well.

I'm trying to set up development environment but WSL isn't working in VS code. How can I fix the problem here? It keeps showing "VS Code Server for WSL closed unexpectedly." message.

[2020-12-03 07:24:35.010] Resolving wsl+docker-desktop, resolveAttempt: 1
[2020-12-03 07:24:35.106] Starting VS Code Server inside WSL (docker-desktop)
[2020-12-03 07:24:35.106] Extension version: 0.51.4, Windows build: 19041. Multi distro support: available. WSL path support: enabled
[2020-12-03 07:24:35.107] No shell environment set or found for current distro.
[2020-12-03 07:24:35.317] Probing if server is already installed: C:\WINDOWS\System32\wsl.exe -d docker-desktop -e sh -c "[ -d ~/.vscode-server/bin/e5a624b788d92b8d34d1392e4c4d9789406efe8f ] && printf found || ([ -f /etc/alpine-release ] && printf alpine-; uname -m)"
[2020-12-03 07:24:37.900] Probing result: found
[2020-12-03 07:24:37.901] Server install found in WSL
[2020-12-03 07:24:37.901] Launching C:\WINDOWS\System32\wsl.exe -d docker-desktop sh -c '"$VSCODE_WSL_EXT_LOCATION/scripts/wslServer.sh" e5a624b788d92b8d34d1392e4c4d9789406efe8f stable .vscode-server 0  '}
[2020-12-03 07:24:38.017] Setting up server environment: Looking for /root/.vscode-server/server-env-setup. Not found.
[2020-12-03 07:24:38.017] WSL version: 4.19.128-microsoft-standard docker-desktop
[2020-12-03 07:24:38.017] WSL2-shell-PID: 11
[2020-12-03 07:24:38.017] Starting server: /root/.vscode-server/bin/e5a624b788d92b8d34d1392e4c4d9789406efe8f/server.sh  --port=0 --use-host-proxy --enable-remote-auto-shutdown 
[2020-12-03 07:24:38.018] /root/.vscode-server/bin/e5a624b788d92b8d34d1392e4c4d9789406efe8f/server.sh: line 12: /root/.vscode-server/bin/e5a624b788d92b8d34d1392e4c4d9789406efe8f/node: not found
[2020-12-03 07:24:38.018] VS Code Server for WSL closed unexpectedly.
[2020-12-03 07:24:38.018] For help with startup problems, go to
[2020-12-03 07:24:38.018] https://code.visualstudio.com/docs/remote/troubleshooting#_wsl-tips
[2020-12-03 07:24:38.021] C:\WINDOWS\System32\wsl.exe -d docker-desktop -e kill 11
[2020-12-03 07:24:38.171] WSL Daemon exited with code 0


Solution 1:[1]

  1. In Vs Code ctrl+schift+p
  2. Open Preferences: Open Settings (JSON)
  3. Add "terminal.integrated.shell.windows": "wsl.exe",
  4. Reopen terminal.

If you need to open a specific distribution, then additionally for example:

  1. "terminal.integrated.shellArgs.windows": ["-d", "Ubuntu-20.04"],
  2. Reopen terminal.

Solution 2:[2]

I encountered the same problem, but it was solved by restarting the WSL2 distribution. I'm using Ubuntu-20.04, so I rebooted with the following command. After reboot, reopen VS Code and hopefully your problem will be solved.

wsl -t Ubuntu-20.04

Solution 3:[3]

In vscode setting, Remote WSL setting >> Extension setting Remote.WSL2:Connection Method select wsl2VMAddress

enter image description here

WSL2 version 2 uses a virtual private network which is different from Windows 10 host IP. it changes its IP address every time so... if VS code running inside WSL2 can pick up the change then it might work better. otherwise, we have to disable the autogeneration of IP in WSL2 side.

Solution 4:[4]

In vscode terminal:

which node

and pasted the path to launch.json:

"runtimeExecutable": "<path>/node/v14.15.5/bin/node",

Solution 5:[5]

The issue was solved in my case by opening the Ubuntu terminal and starting vscode from there. There was a missing package in wsl that I couldn't install by editing any config file from vscode. After installing that package, vscode was able to start the remote connection with wsl

Solution 6:[6]

Windows update disabled Virtualization in my BIOS. You can run the wsl command in PowerShell to check if the wsl environment is ok.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Tomotsugu Kaneko
Solution 3 Jae Mun Choi
Solution 4 rufreakde
Solution 5 Alberto Baraza
Solution 6 MosesSoftEng