Auzy
Auzy

Reputation: 2155

Launch VS Code from WSL Bash

I have the Creators update installed. WSL is operational. I can execute most .exe files by simply calling notepad.exe But when it comes to VS Code.... I can't use the default code command or call code.exe... I have also tried code and code.cmd. Why doesn't VS Code execute like other programs? And is there a way to enable the code command?

EDIT: I now get these errors:

me@mypc:/mnt/c/Users/me/Documents/project_folder$ code . /mnt/c/Program Files (x86)/Microsoft VS Code/bin/code: line 7: realpath: comma nd not found /mnt/c/Program Files (x86)/Microsoft VS Code/bin/code: line 14: ./Code.exe: No such file or directory

Upvotes: 26

Views: 38123

Answers (12)

dejanualex
dejanualex

Reputation: 4328

According to official docs WSL is automatically added to PATH (more or less) and resources

microsoft docs

The catch is that it's doing it for the default WSL user (most of the time being root). To add code to PATH for the desired user create/update /etc/wsl.conf

[user]
default=username

And the from powershell:

# identify the instance
wsl -l -v

# stop it
wsl -t instance

Now when typing code . you should see VS Code fetching components needed to run in WSL. This should only take a short while and is only needed once.

Note: If this command does not work, you may need to restart your terminal or you may not have added VS Code to your path when it was installed.

enter image description here

Upvotes: 0

shaws
shaws

Reputation: 11

Nothing worked (and I tried everything) until I used the one liner here.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

Upvotes: 1

theSparky
theSparky

Reputation: 405

Using Windows 10 and WSL2, canceled the install that gets triggered when running code for the first time. I found per https://stackoverflow.com/a/64108942/1919793 and https://code.visualstudio.com/docs/remote/wsl-tutorial#_run-in-wsl that it is better to reinstall VS Code WSL server:

~$ rm -r ~/.vscode-server
~$ code
Installing VS Code Server for x64 (6261075646f055b99068d3688932416f2346dd3b)
Downloading: 100%
Unpacking: 100%
Unpacked 2457 files and folders to /home/user/.vscode-server/bin/6261075646f055b99068d3688932416f2346dd3b.
~$

Previously I was using this code in ~/.bash_aliases

function code {
    if [ -e "/mnt/c/Program Files/Microsoft VS Code/Code.exe" ]; then
        "/mnt/c/Program Files/Microsoft VS Code/Code.exe" &
    else
        username=$(cd /mnt/c && cmd.exe /c 'echo %username%')
        username="${username/$'\r'/}"
        echo "/mnt/c/Users/${username}/AppData/Local/Programs/Microsoft VS Code/code.exe" &
    fi
}

Upvotes: 0

ahsan dev
ahsan dev

Reputation: 31

In recent versions of WSL you just type code at the terminal and this will update the VSCode installation as needed.

See below for an example:

result of running code in a terminal

Upvotes: 0

Daniel
Daniel

Reputation: 9464

Another alternative is to use the following:

cmd.exe /c code

This also works for VSCodium:

cmd.exe /c codium

An alias can used so that the user only needs to call code:

alias code="cmd.exe /c code"

Upvotes: 3

DharmaTurtle
DharmaTurtle

Reputation: 8357

For some reason code . doesn't open in WSL for me (i.e. it would open using in Windows file system without the WSL extension). So I would have to constantly ctrl + shift + p then WSL: Reopen Folder in WSL. You could force it with the --remote wsl+Ubuntu-20.04 flag, but then for me the . wouldn't work; it just opens a VS Code instance in WSL, not in the current dir.

So I added a function to my .bashrc/.zshrc called c that opens the current dir in VSCode + WSL:

alias code="/mnt/c/Users/yourusernamehere/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe"
function c () { code --remote wsl+Ubuntu-20.04 -n "`pwd`" }

Use wsl --list to find your distro's name. It must be prefixed with wsl+; see docs.

Upvotes: 1

krisvibes
krisvibes

Reputation: 1

for me what worked was

alias code="/mnt/c/Users/<USERNAME>/AppData/Local/Programs/'Microsoft VS Code'/Code.exe"

and then you can simply invoke it with something like

code .

Upvotes: 0

UselessScreen
UselessScreen

Reputation: 11

I'm new so I don't have enough reputation to comment

I just want to point out that some people(like me) have visual studio code installed in C:\Users\YourUserNameGoesHere\AppData\Local\Programs\Microsoft VS Code instead of C:\Program Files\Microsoft Visual Studio If you cant get it to work, try checking if it is installed in Appdata instead of Program Files.

The escaped WSL path to the x86 visual studio executable is

/mnt/c/Users/UserName/AppData/Local/Programs/Microsoft\ VS\ Code/code.exe

Upvotes: 1

proximab
proximab

Reputation: 2463

I had the same problem after manually updating Ubuntu on wsl2. I solved this by adding this function to .bashrc config file. It's better solution than alias because it does not block the console.

Before adding it to your config test the function in bash and adjust the path if needed. For sure, you must change {username} to your Windows account username.

function code () { /mnt/c/Users/{username}/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe; }

If that work add it to your configuration script e.g. .bashrc, .zshrc. Here's a script that does that.

echo "function code () {
  /mnt/c/Users/{username}/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe \"\$@\"; 
}" >> ~/.bashrc
source ~/.bashrc
code .

Although my recommendation is to make backup and install fresh distro from Microsoft Store then open VS Code with Remote - WSL extension. If there came out an error with initializing WSL Extensions reinstall it first. If you used 19.04 Ubuntu and upgraded to 20.04 it sometimes needs to download initialization scripts for that version or it fails. That should help :)

Upvotes: 1

Chiramisu
Chiramisu

Reputation: 4726

WARNING: Do not change Linux files using Windows apps and tools

Update: The above is not relevant to version 1903 or newer.

With that out of the way, I recently experienced the exact same behavior. Turns out, in my noobish first whack at WSL with VS Code I also installed a native copy from apt which overwrote the Windows path.

So in WSL I removed it with something like this...

$ sudo apt remove code -y; sudo apt autoremove -y

Then confirmed the path was correct...

$ which code
/mnt/c/Program Files/Microsoft VS Code/bin/code

Then relaunched WSL terminal and all was well in the wonderful world of coding once again. :)

Note: I suppose another option would be to fix the path in WSL, but without a GUI package installed it will just launch in the background and never appear anyway.

Upvotes: 3

Auzy
Auzy

Reputation: 2155

The Creators update did install the interop functionality. However, it seems you need to install realpath in WSL in order for the path to be recognized. I'm not sure why this is the case but running sudo apt-get install realpath fixed it for me!

EDIT: After updating to the Fall Creators Update launching VSCode from WSL works out of the box 🎉

Upvotes: 27

Damo
Damo

Reputation: 6433

You could always create an alias in the WSL Bash shell

alias code="/mnt/c/Program\ Files/Microsoft\ VS\ Code/Code.exe"

If you add this to your .bashrc or .zshrc file then it will always be available when you start a new instance of your environment.

Upvotes: 24

Related Questions