A Blog
by Pim Veelders

Setting up a nice dev environment using WSL2 in Windows 10

November 6, 2019
If you use Windows and Visual Studio Code, but want to run your code on Linux, Windows 10 with WSL2 is the way to go! You can install and configure a full webserver on Linux and simultaneously use Windows.

Introduction

I think I started using php/mysql/apache in 2004. In those days you would install this yourself on your Windows machine which was quite a hassle. Later, pre-compiled solutions like wamp and xampp became popular and made things a lot easier. However, at some point I got the feeling I was missing a true Linux environment to run my code on. That's when I started using VirtualBox. This gives you a true Linux environment, running on your local machine. The downside of using a virtual machine like VirtualBox is that it takes a long time to boot and individual web requests are very slow. The newest way to use Linux on Windows is through WSL (Windows Subsystem for Linux). This let's you install a Linux distribution inside of Windows with much better performance.

My first experience

After reading about WSL2 on internet I got excited to give it a try. So, when I bought a new laptop about two months ago I started using it. And I must say I'm getting more enthusiastic about my current dev environment every day. The major downside at this moment is that you have to participate in the insider program of Microsoft since WSL2 isn't available in the current stable release of Windows 10. This means you'll get Windows updates that aren't thoroughly tested. For example, a few weeks ago I got an update that failed to install and resulted in a "rebooting" loop. It seems fixed now, but fingers crossed until they officially release WSL2 at which point this guy will return to the regular Windows releases. I believe this will be in spring 2020.

One more heads-up before I drop in my installation steps: accessing the other OS's file system is still quite slow. Which means, if you're in Windows it's slow to access files in Linux and vice versa. It's nice that you have direct access to files on the other system, but it's not very useful for development because most IDE's make heavy use of the file system. However, if you use VS Code it's not a problem, just follow this principle:

  • Store your code on the Linux file system
  • Install and run Visual Studio Code on Windows
  • Use the "Remote - WSL" extension

Installing Linux

To use WSL2 you need to have Windows 10 build 18917 or higher. Currently this requires you to join Microsoft's insider program. If you have this, you can enable WSL by starting PowerShell as Administrator and run:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

You'll probably need to restart your computer after this and then run:

wsl --set-default-version 2

Next, you can install a Linux distribution of choice from the Microsoft store. I chose "Ubuntu" (which defaults to the latest available version) but there are a few others to choose from.

Now you have Linux running and can access it from the terminal. I recommend installing Windows Terminal (also in the Microsoft store). This is a nice terminal that's pretty customizable.

Installing Nginx, Mysql & PHP

Once you can access Linux from your terminal it's time to install some actual useful software. These are the steps I took to install Nginx, Mysql and PHP:

# Nginx
sudo apt install nginx -y
# Mysql
sudo apt-get install mysql-server
# PHP
sudo apt install php7.2-common php7.2-fpm php7.2-cli php7.2-gd php7.2-mysql php7.2-curl \
php7.2-intl php7.2-mbstring php7.2-bcmath php7.2-imap php7.2-xml php7.2-zip unzip php-pear
# Composer
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

To prevent some annoying conflicts between users I recommend using the same user (in my case pim) everywhere. I made the following changes for this:

# For PHP: change user/group under [www]
sudo vi /etc/php/7.2/fpm/pool.d/www.conf
# For Composer
sudo chown -R pim:pim ~/.composer/

Visual Studio Code

As mentioned earlier to truly use the full potential of WSL2 you'll need to use VS Code (unless you use something like Vim). This is because accessing the file system from an IDE running in Windows is still too slow. VS Code solves this problem by installing an extension, that runs on Linux, which does all the heavy lifting. At first this felt a little strange, I wanted to access my files directly, but I've accepted it and it works.