When I try to execute code from WSL, like this:
cmd.exe
wsl code .
I get the following error:
/bin/bash: code: command not found
However, if I execute the same command but first run cmd.exe as an Administrator, vscode opens as expected.
The question is why "code" is not recognizable when I run as non-admin, and how can I solve it?
Additional info: WSL2 version 41959 vscode version 1.38 vscode Remote WSL extension version 0.39.5
Alternatively, you can open a Remote WSL window directly from VS Code: Start VS Code. Press F1, select Remote-WSL: New Window for the default distro or Remote-WSL: New Window using Distro for a specific distro. Use the File menu to open your folder.
Visual Studio's WSL 2 toolset allows you to use Visual Studio to build and debug C++ code on WSL 2 distros without adding a SSH connection. You can already build and debug C++ code on WSL 1 distros using the native WSL 1 toolset introduced in Visual Studio 2019 version 16.1.
You can also run VS Code from the terminal by typing 'code' after adding it to the path: Launch VS Code. Open the Command Palette (Cmd+Shift+P) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command.
As @Biswapriyo mentioned, this is an open bug where WSL cannot access Windows C drive. Workaround that helped me is to restart WSL like this:
wsl --shutdown
wsl
wsl --list --version
Show as wsl 2
wsl
chmod u+x vscode_dir/code.exe
chmod u+x vscode_dir/bin/code
ln -s vscode_dir/bin/code code
mv code ~/.local/bin
.bashrc
, add a line as follows:export PATH="$HOME/.local/bin:$PATH"
source .bashrc
or restart terminal.Then you can happily play code in wsl2-ubuntu environment or terminal of VSCode.
My problem was that I was using the root user after running sudo -i
. Exiting and using my normal user on WSL solved the error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With