PHP/Symfony development with Windows Subsystem for Linux (WSL)
UPDATE: WSL2 with a native Linux kernel succeeds WSL1 in Windows 10. To learn how it works with Symfony and PHP, please read the following blog post: Set up a PHP development environment for Symfony with Windows and WSL2
The Windows Subsystem for Linux allows the execution of Linux binaries in Windows 10. The feature is not enabled by default and is targeted for developers. The Linux distribution in use is Ubuntu 14.04 LTS, but interestingly there is no Linux kernel included in the package. Instead Microsoft has written a layer that translates Linux kernel calls to their Windows NT equivalents.
Microsoft originally built the technology for something they called "Project Astoria", which allowed Windows Mobile devices to run Android applications. Microsoft since decided to double down on the desktop market and put it's mobile endeavors on ice, but the technology lives on. You could think of WSL as a reverse version of Wine, a compatibility layer that allows applications designed for Microsoft Windows to run on Unix-like operating systems.
The approach is possible because the Windows NT kernel was originally designed to run software from IBM's OS/2 and other competing OS architectures. This was since forgotten as Microsoft descended into the Win32 hegemony that reigned supreme for two decades or so. Now with Windows at risk, they're tapping into the technology again - this time for Linux compatibility. Times have certainly changed...
There is no separate virtual machine you need to spin up, instead you simply open up a shell window that runs the Linux shell. You've got access to your files on the Windows machine and you have full access to apt-get and even full build tools, if you so wish. Graphical applications are not supported and neither is any kernel level activity like communicating to USB interfaces.
Using WSL for PHP development
Microsoft has documented the details on how to enable WSL on Windows 10, but in short the steps are:
- Enable developer mode in System Settings
- Enable the Windows Subsystem for Linux in Windows Features
- Launch "Bash" to install Ubuntu from Windows Store
After that you can start the application "Bash on Ubuntu on Windows" to access your native shell:
From here on you can do what you would usually do to get started on a Linux server. In this case I did a few basic operations, for which you can simply follow instructions for Linux:
- Upgraded to PHP 7 using APT
- Installed the Symfony Installer and created a test app
- Installed Composer
After that I ran the built in web server in Symfony, which is accessible through localhost (127.0.0.1). This is because it's the both are running native on the host as can be seen from the task manager:
PHP in this case obviously thinks it's running in a regular Ubuntu installation, since the translation to Windows NT kernel is done behind the scenes. In the example we ran a basic setup, but if you would like to install a full stack on your localhost, you can follow tutorials like How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 14.04
Obviously there's quite a bit of things going on here and one tricky thing is disk access. Disks are is shared with Windows, but in two different ways. The Linux "partition" is under the Windows users home directory and it's automatically got all the Linux specific metadata (permissions, ownership...) stored in NTFS as extended attributes to make it appear like a real Linux FS.
Users also have access to the traditional lettered drives in Windows (C, D, E...), but they are mounted separately under /mnt, with all files belonging to root:
This means that at this point using your favourite Windows IDE with the WSL PHP environment might not be as straightforward as you hope. Microsoft is planning improvements to this, but for now you'll need to try how the disk resource sharing works for your workflow. If you're using Vim or Emacs, on the console you're set :)
If you're a UNIX geek you might be interested there's also a third File System component that is mounted to /proc to emulate system resources as files, as per the Everything is a File approach of UNIX.
All of the above might not be something you'll embrace full on at this point, but in a pinch it can be very useful. In the example we did not install a full LAMP stack, but there's nothing stopping you from installing Redis or other tools you might want access to. You can also mix and match Windows and Linux daemons that communicate over sockets.
The first version of WSL shipped in Windows 10 Anniversary Update is still labelled as beta and there are quirks to be fixed in the underlying complexity. But for developers wanting to do some development work, have a native SSH shell in Windows or just mess around with their filesystems with GNU Tools, it's pretty much there already.
The Windows Subsystem for Linux is hardly the only way to run Linux application in windows. In addition to classic Virtual Machine options powered by Vagrant and Virtual Box, Docker has also matured greatly for both macOS and Windows during 2016, both now supporting built in hypervisors. WSL is not intended to replace these, nor is it targeted as a being production environment.
Even with it's shortcomings with transparency of disk access and possible quirks in Beta, WSL is a great option for developers running Windows to get access to a native Linux shell on their workstations.
Learn more about the Windows Subsystem for Linux: