Installing PhpStorm in WSL2 GUI (WSLg)

Problem

Microsoft has introduced some awesome stuff with WSL2 and in particular WSLg (Windows Subsystem for Linux GUI). In my opinion this is the last step needed to make Windows a much more viable environment for developing PHP applications.

When working with Docker on a non-Linux based environment , you're always dealing with some form of virtualization. Both Windows and macOS run a layer of virtualization to bridge the gap for Docker to work. This introduces a new problem; mounted volumes. Your project code lives on your Windows filesystem, but your docker environment runs from a virtualized environment, causing a lot of lag since it's reading from a mounted Windows volume. It also introduced other challenges like file permissions.

You also have the choice to put your project code inside your Linux filesystem and point your IDE to that folder. But now your IDE is suddenly slower because now your IDE is reading from a mounted volume.

Solution

WSLg, a new feature in the latest version of WSL2 on Windows 11, is a game changer. Thanks to WSLg, we can now run applications, like our IDE, from inside Linux, as if it was a normal Windows application. So now, we can have our project code live in Linux, so that your container is fast, and start our IDE from inside Linux as well, so that your IDE is fast too!

Prerequisites

Setup

Let's download JetBrains Toolbox, a great way to manage your JetBrains products. Go to the JetBrains Toolbox page and copy the download link for the .tar.gz. Then use wget to download it to your home directory.

$ wget https://download.jetbrains.com/toolbox/jetbrains-toolbox-1.27.0.13477.tar.gz

Now let's unpack this archive.

$ tar xvpf jetbrains-toolbox-1.27.0.13477.tar.gz

Let's move this binary to a logical place in our filesystem.

$ mkdir ~/.local/share/jetbrains-toolbox
$ mv jetbrains-toolbox-1.27.0.13477/jetbrains-toolbox ~/.local/share/jetbrains-toolbox/jetbrains-toolbox

We can symlink it to a directory that's in our $PATH, so we can execute it regardless of where we are.

$ sudo ln -s ~/.local/share/jetbrains-toolbox/jetbrains-toolbox /usr/local/bin/jetbrains-toolbox

Now that we've properly installed the jetbrains-toolbox, we can launch it!

$ jetbrains-toolbox

This should launch a new window with the JetBrains Toolbox. A Linux application inside your Windows desktop. From here we can install any JetBrains product we want. In my case that's PhpStorm.

After installing PhpStorm, we can launch it from the toolbox and it should work perfectly.

In order to make it easier to start, we can create a shortcut in our start menu (open %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs) or on our desktop. Open the directory you want it in, click New -> Shortcut. As the target, fill in the line below as your target.

C:\Windows\System32\wslg.exe -d Ubuntu --cd "~" -- ~/.local/share/JetBrains/Toolbox/scripts/phpstorm

Save it and try launching it. If it doesn't work, make sure the paths and name of the Linux dist are correct. If you like to use the proper PhpStorm icon, you can download it here (select ICO) and change the icon of the shortcut to that.

Et voilĂ ! A fast, native development environment from the comfort of your Windows installation. The best of both worlds! Having said that, I will still be sticking to my Pop_OS! installation because I'm quite comfortable in Linux land. But I imagine WSL2 is great for those attached to a more stable and broadly supported OS like Windows.