Wsl2 – run intelliJ, hassle free (warning : non convential way)

Have you tried to use Jetbrains IntelliJ recently with WSL2 (the windows subsystem for linux) ?
Yes, it worked. At least for the five first minutes, then it started to blink and to stagger… then it collapsed abruptly.

– With the dedicated WSL2 mode, gradle has some troubles : do we run the instance on the host or on the machine ? the toolchain does not work great unless if you use… the terminal.
Otherwise, gradle starts installing jar dependencies and a cache in %USERPROFILE% ; so it is installing files in your other disk partition, that is a waste of disk usage.

– So you’ve tried SSH mode instead, it is compatible with WSL2 but not only. So you installed openssh-server, you added the client public key to connect seamlessly without a password prompt.
The client connects perfectly to a gateway. There is a server process that behaves like a session manager.
It consumes gigabytes of memory… Then it makes your window blink. When the server runs out of memory, the intelliJ session window closes unexpectedly, a progress bar is displayed for one minute and you get back your environment.
The first time it happens is ok, but the more you use your dev environment, the more frequent this annoyance happens.
In the end, the session manager runs out of memory, and as a consequence, your entire machine freezes, including the mouse pointer.

That is it for IntelliJ and WSL2.
Well, really ?

No, actually there is a third option that works slightly better. No miracle there, but intelliJ has crashed only a few times (5 or 6 times) in two weeks, so I consider that as acceptable.

Howto ?!
1. Download and run vcxsrv and IntelliJ for *Linux* (and not for windows).
2. Add a script on your $HOME :

/mnt/c/Program\ Files/VcXsrv/vcxsrv.exe -ac -multiwindow -wgl &

Of course, don’t forget to

chmod a+x ~/vcxsrv

No, -wgl is not necessary (and it does not work either) but we never know. 3d rendering with WSL2 ?

3. Create a script to guess the DISPLAY variable :
Below is a complex example of the display script where sometimes I am connected to a corporate VPN (virtual private network)
Notice that it only works for french, you’ll probably want to adapt it to look for the right pattern in your own locale
If you don’t understand how the script below works, just know that it consists in using the output of ipconfig.exe to find an IP adress in a section. Here either from the section Carte Ethernet Ethernet 2 :or else in the section "Wi-Fi". As you can guess, you can use different sections titles to match your needs.

IP_VPN=$(echo "$IPCONFIG_RESULT" | grep --binary-files=text "Carte Ethernet Ethernet 2 :" -A6 | grep IPv4 | tail -n 1 | cut -d':' -f2 | sed 's/[^0-9.]//g')
IP_NONVPN=$(echo "$IPCONFIG_RESULT" | grep --binary-files=text "Wi-Fi" -A6 | grep IPv4 | tail -n 1 | cut -d':' -f2 | sed 's/[^0-9.]//g')
echo "export DISPLAY=$DISPLAY"
echo "export XCURSOR_SIZE=64"

Of course, don’t forget to

chmod a+x ~/display

That script (to be run inside the virtual machine) can actually succesfully run a DOS command (ipconfig.exe)
ifconfig does not contain the necessary information because the network cards in the WSL are emulated by the Hyper-V kernel,
so the information must be looked in ipconfig.exe instead.

4. Integrate the ~/display script in your ~/.bashrc
at the end of your .bashrc file, add that line

5. Add a run-idea script with this content :

/<your-home-dir>/vcxsrv || true &
sleep 5
IDEA_DIR=$(ls -d /<your-home-dir>/idea-*)

Why didn’t I use ./display or ./vcxsrv ? Because we will start the run-idea command from wsl.exe, which does not know what will be the current work directory.

Of course, don’t forget to

chmod a+x ~/run-idea

6. Add a shortcut for intelliJ on your task bar
the command to execute is

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -command "&{ bash -c '/<your-home-dir>/run-idea' }"

Download the IntelliJ icon, and use it for your newly created shortcut
(I guess you will completely delete the intelliJ installation on windows, so better save the icon file somewhere)

7. Use IntelliJ for linux in WSL2
And stop wasting time configuring your IDE, focus on work…

Good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.