VS Code + WSL2 exec format error: Fast Fix

Windows

WSL2 terminal showing an exec format error

If VS Code in WSL2 reports “exec format error,” Linux is being asked to run a file it cannot recognize as an executable. The most common cause is that the command points to a Windows program or a binary built for a different operating system or processor architecture.

Why this happens

WSL2 runs a Linux kernel, so commands launched in a WSL terminal must be runnable by Linux or explicitly handed to Windows through WSL interoperability. The error usually concerns the file being launched, rather than the VS Code editor itself.

  • The command resolves to a Windows executable or another non-Linux file.
  • A Linux binary was downloaded for a different architecture or is incomplete.
  • A script has no valid interpreter line, or its first line contains unexpected characters.

1. Identify exactly which command fails

In VS Code, select Terminal > New Terminal and check that the prompt is a WSL shell. Run the failing command again and note the exact filename in the error. Then check which executable your shell will use: run command -v name, replacing name with the command that failed. If it prints a path under /mnt/c or another Windows-mounted folder, inspect that file before changing VS Code settings.

2. Check the file type and architecture

For a specific file, run file /path/to/program. A Linux executable should be identified as an ELF file; a PE32 or PE32+ result identifies a Windows executable, while “HTML document” or “ASCII text” can indicate a bad download or a script. Check the WSL architecture with uname -m, then compare it with the binary’s architecture. Download the Linux build that matches your system from the project’s official release source. The fix is confirmed when file identifies a suitable Linux executable and the original command runs.

3. If the file is a script, inspect its first line

A script launched directly needs a valid interpreter line, such as #!/bin/bash, at the very start of the file. Inspect it with head -n 1 script.sh and check for a byte-order mark or unexpected characters before #!. If the script is plain text, test whether its contents run under the intended interpreter with bash script.sh. If that works but direct execution fails, add the correct interpreter line, save the file as UTF-8, and retry ./script.sh.

4. Convert Windows line endings when needed

Scripts edited on Windows can contain CRLF line endings, which may interfere with interpreter paths or script commands. In VS Code, open the file and select the CRLF indicator in the status bar, then choose LF and save. You can also run sed -i 's/\r$//' script.sh in WSL. Retry the script; if the interpreter is now found and execution proceeds, the line endings were the cause.

5. Confirm executable permissions

Permissions usually produce “Permission denied,” rather than “exec format error,” but they are worth checking after verifying the file type. Run ls -l /path/to/program. If the intended Linux script or binary lacks execute permission, use chmod +x /path/to/program, then run it again. A successful launch confirms permissions were also needed; chmod cannot convert a Windows program into a Linux executable.

6. Reinstall the VS Code Server if connection startup fails

If the error appears while VS Code is connecting to WSL, rather than when you run a project command, the remote server installation may be incomplete or damaged. First close the WSL window with File > Close Remote Connection. In a WSL terminal, remove the server installation with rm -rf ~/.vscode-server, then reconnect using the VS Code WSL command from the Command Palette. VS Code should install the server again. Confirm the fix when the WSL window opens and its integrated terminal works. This removes the remote server files, not your project folder.

7. Check whether the failing command should run in Windows

Some tools are installed only on Windows, while your VS Code terminal is running Linux. For a Windows command, run it from PowerShell or Command Prompt, or install its Linux version inside the WSL distribution. To identify the active shell, run uname -s; output beginning with Linux confirms the command is running in WSL. Once the tool is installed for the environment where it runs, it should start without the format error.

Likely causeTypical symptomQuick test
Windows executable in WSLError names a file under /mnt/cfile /path/to/program
Wrong or damaged binaryOne downloaded tool failsfile and uname -m
Malformed script headerDirect run fails; interpreter run may workhead -n 1 script.sh
Broken VS Code ServerError occurs during WSL connectionReconnect after server reinstall

FAQ

Why does VS Code show exec format error in WSL2?

WSL2’s Linux environment cannot execute every file that Windows can. The command may point to a Windows program, an incompatible Linux binary, or a script Linux cannot interpret. Check the named file with file first.

Can Windows line endings cause exec format error in WSL?

They can disrupt scripts, especially when the line ending affects the interpreter line or a command. Change the file’s line endings to LF in VS Code and retry. If the error remains, inspect the script header and file type.

How do I fix exec format error when VS Code connects to WSL?

If it occurs during connection startup, the VS Code Server installation may need to be rebuilt. Close the remote window, remove ~/.vscode-server from the WSL distribution, and reconnect so VS Code can install it again. If the error names a project file instead, troubleshoot that file rather than reinstalling the server.

Start by checking the exact file named in the error; its type and architecture usually point to the right fix. If nothing worked, note the failing command and file output, then check the relevant tool’s Linux installation instructions or VS Code’s WSL troubleshooting guidance.

Photo: Ó29c / CC BY-SA 4.0 (Wikimedia Commons)

Leave a Comment