05 April 2024

Intellij development on Windows with WSL and Docker and Ruby

WSL Setup

Having left Windows behind some 10 years ago, I recently heard about WSL via DHH tweeting about trying it and leaving Mac OS behind. So I decided to give it a try. Most of my development these days is using Intellij for Java (or Ruby on the side). I'm also trying to push my dev environment into containers to avoid complexities around environment setup.

So my requirements are attempting to setup a development environment:

  • Using a Windows 11 machine
  • With WSL2
  • With docker running inside WSL2 (ie not Docker Desktop)
  • Using Intellij as the IDE

Getting WSL Running.

Starting from a clean Windows 11 install:

  • Control Panel -> Turn Windows Features On and Off
  • Ensure Hyper V, Virtual Machine Platform and Windows Sub System for Linux are enabled.

Windows will most likely need restarted. Then in Windows Powershell:

wsl --update
wsl --install -d ubuntu

At this stage WSL2 should be running, and you can enter the WSL environment with the wsl command.

WSLg and Intellij

WSL allows for Linux GUI applications to run seamlessly inside Windows. First install the vGPU driver, then in wsl:

sudo apt-get install x11-apps libxrender1 libxtst6 libxi6
sudo snap install intellij-idea-ultimate --classic

Now running intellij-idea-ultimate in a terminal should open Intellij. On my system (Lenovo T490) Windows automatically scales the display and Intellij appears with very small fonts when started this way. It looks fine on my external monitor.

Following the comments in this post turned up the following instructions to get WSLg apps to scale too:

  • As administrator wsl –shutdown
  • Create / edit the file .wslgconfig in your users home directory, ie c:\users\<user>\ and add:
[system-distro-env]
WESTON_RDP_DISABLE_FRACTIONAL_HI_DPI_SCALING=false
WESTON_RDP_FRACTIONAL_HI_DPI_SCALING=true

However after this, Intellij still looked terrible. Rolling that back, it turns out using the zoom in Intellij sorted it.

Docker On Windows

The easiest path to Docker on Windows is of course Docker Desktop. This actually runs a VM in WSL to deploy docker on. But with Ubuntu available in WSL already, Docker Desktop seems unnecessary. As long as any docker commands are executed inside of WSL, docker can be in the same WSL instance, and run with virtually no overhead. Some years back, I read posts indicating it was difficult to run Docker inside WSL due to the lack of systemd, but that is no longer the case.

Where Does the Source Code Live?

With WSL, the Windows C drive is mounted into the Linux VM under /mnt/c so WSL can access source files inside Windows. However there appears to be quite a large performance penalty for this. It is better to keep the source inside WSL if the execution environment is inside WSL.

Intellij On Windows, source in WSL

What I ideally wanted, was Intellij running on Windows, avoiding WSLg. I fear it may be somewhat slow, but I have not used it much to validate that. Then have my source in WSL, mounted into a container running on Docker in WSL with all the dependencies.

Trying this out in Intellij, Docker can be configured to reside in WSL, and works fine. The source can be loaded from WSL directly. For Ruby development, attempting to configure a remote docker interpreter however fails as I reported with an error like:

Cannot run program "docker" (in directory "\\\\wsl.localhost\\Ubuntu\\home\\sodonnell\\rails\\shiny_new_app"): CreateProcess error=2, The system cannot find the file specified

It seems like the Ruby integration doesn't understand how to deal with code in WSL and docker in WSL. If the source was in Windows, it would run into a different problem, attempting to mount a windows path into the docker container inside WSL. This is where Docker Desktop would come in, translating the windows paths to make this work, but then the source would still be outside WSL.

Another option Intellij offers, is to have the interpreter inside WSL, but that would require abandoning docker and setting up a WSL instance with all dependencies.

So it looks like I am stuck with putting everything inside WSL - source, Docker and Intellij and running Intellij through WSLg! Or, just installing Ubuntu and going all in on Linux instead!

blog comments powered by Disqus