Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a Windows executable from WSL (Ubuntu) Bash

Along with Windows 10 Anniversary update for summer 2016, came the possibility to run ubuntu binaries inside the new Windows Subsystem for Linux (WSL), a "lightweight" virtualized subsystem.

Unfortunately, launching C:\Windows\System32\bash.exe, another bash ELF binary starts a process inside the WSL, from where you cannot escape! You may launch only other ELF binaries.

So how can I execute *.exe files from Windows Bash?[1]

[1] Question asked also in Microsoft's "official" GH support repo.

like image 470
ankostis Avatar asked Aug 12 '16 14:08

ankostis


People also ask

How do I run a Windows executable from WSL?

Run Windows tools from Linux WSL can run Windows tools directly from the WSL command line using [tool-name].exe . For example, notepad.exe .

Can you access Windows files from WSL?

One of the benefits of WSL is being able to access your files via both Windows and Linux apps or tools. Using your mounted drives, you can edit code in, for example, C:\dev\myproj\ using Visual Studio / or VS Code, and build/test that code in Linux by accessing the same files via /mnt/c/dev/myproj .

Can you run Windows binaries in WSL?

In the Windows 10 Creators Update (build 1703, April 2017), this is natively supported. So you can now run Windows binaries from Linux... wsl command_to_run i.e: wsl "ls" ; or specify the distro you want to use to run it using: ubuntu run ls.

Can you run a Windows executable in Linux?

A straightforward answer to the question is—Yes. You can run EXE files and other Windows programs on Linux, and it's not as complicated as it sounds. By the end, you'll have a brief understanding of executable files, along with different ways to run said programs on Linux.


2 Answers

In the Windows 10 Creators Update (build 1703, April 2017), this is natively supported. So you can now run Windows binaries from Linux...

notepad.exe or any other .exe (the extension is needed and it needs being on your path, some older versions need the whole path)

...and vice versa using one of the following:

  • bash.exe -c command_to_run i.e: bash.exe -c ls
  • bash -c command_to_run i.e: bash -c ls
  • wsl command_to_run i.e: wsl "ls"; or specify the distro you want to use to run it using:
    • ubuntu run ls

For more information, see the above linked article.

like image 122
Andrew Mao Avatar answered Oct 02 '22 15:10

Andrew Mao


Native solution

The official solution provided with Windows 10 Insider Preview Update (14951) is based on the almost forgotten binfmt_msc Linux facility for launching binaries. The registration command for the binfmt_misc would be like this (where /init is the provisional binfmt_misc "interpreter" for the win-executables):

sudo echo ":WSLInterop:M::MZ::/init:" > /proc/sys/fs/binfmt_misc/register 

And then win-executable would be launched like regular programs:

$ export PATH=$PATH:/mnt/c/Windows/System32 $ notepad.exe $ ipconfig.exe | grep IPv4 | cut -d: -f2 $ ls -la | findstr.exe foo.txt $ cmd.exe /c dir 

Not that any win-executable must reside in the windows (DrvFs) file-system - not on the Linux's file-system (VolFs) - in order to inherit a proper Windows working-directory.

The cbwin alternative

Untill you get the latest build, project cbwin offers a workaround, by installing 3 new linux commands inside WSL:

  • wcmd: call a win-executable through cmd.exe.
  • wrun: call a win-executable synchronously with CreateProcess, and wait to die (not using cmd.exe).
  • wstart: launch a detached (asynchronously) command (with the use of cmd.exe).

In order to use them, you must:

  1. Install cbwin:
    • a new outbash.exe will be installed in your regular Windows filesystem (somewhere in your %PATH%), plus
    • the 3 linux-commands in the WSL filesystem.
  2. Use this outbash.exe (wherever you installed it) to start WSL, NOT C:\Windows\System32\bash.exe!
  3. Prefix any win-executables with one of those commands, e.g. wrun notepad.

Tip: If the executable launched with wcmd or wrun spawns any children, these survive only for as long that executable remains alive.

In other words, trying to start notepad.exe with wcmd won't work, because notepad will be killed just after having been launched -- Use wrun (synchronously) or wstart (asynchronously) in this case.

like image 38
ankostis Avatar answered Oct 02 '22 15:10

ankostis