I was configuring the new Windows subsystem for Linux 2 developing environment. I have installed VSCode in Windows and WSL2 distribution of Ubuntu 18.04. I can open VSCode in Ubuntu(accessed through Windows Terminal) normally when logged as normal user, using command as follows:
simp1e@ZhuXian:/mnt/c/WINDOWS/system32$ code .
But when I add sudo command in the front, the error occurs as follows:
simp1e@ZhuXian:/mnt/c/WINDOWS/system32$ sudo code .
sudo: code: command not found
At first, the problem remains after I switched to root user. Later I found that the PATH of root user didn't contain VSCode. So I add the VSCode path to /etc/environment. After that root user can open VSCode normally, but sudo code .
still have the problem.
What should I do? Is this still related to the PATH?
sudo code . It is recommended to start vscode as a normal user. To run as root, you must specify an alternate user data directory with the --user-data-dir argument.
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.
From the Command Palette (Ctrl+Shift+P), use the View: Toggle Terminal command. You can create a new terminal via the Terminal menu with Terminal > New Terminal.
The answer here helped me: VSCode in WSL: how to sudo a root file so I can edit it
For some reason starting as root vs logging in and then switching to root worked for me
I think the Remote WSL extension pack is what you are looking for. Install that on your local (e.g. host) version of VS Code, and then from either the command pallet (Ctrl + Shift + p or F1) or from the "Open Remote Window" and select the "New WSL Window" option (screen shot below from command pallet).
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