Setting up Development Env in Win10 and WSL2
Flashback
Long back, I was a hardcore Linux enthusiast having been brain washed by the FSF folks. I have long been out of that hate mindset where you are supposed to hate everything which is not free (FSF Free).
Still I love the *nix env and it is one of the best ways a developer can be productive. I had used Mac in the past and the developer friendliness that ecosystem offers is top notch. No wonder developers around the world flash their macs - even though some use it only as a status symbol while doing presentations 😉
But MS has lately been embracing Open Source and they have made a commitment to make Windows machine a developer’s machine by bringing the Linux goodness with the WSL2. I have been using the WSL2 for around an year now and its awesome!
My Dell XPS 15 with WSL2 costs half as much a mac but is equally productive and attractive - the only thing I miss is the force multi gesture touchpad from Apple! But hey, Win10 has the AoE 😁. In WSL2, I have been doing Java development, running docker containers, angular development etc. It also works great for most of the shell scripts - but occassionally there will be some glitches for which you will have to spent time googling. Following outlines my setup.
Set it Up!
Install WSL2
MS already has their docs up for doing this over here. Follow the same.
Install WSL2 (its the faster superior version than the previous WSL). Choose an OS you like. I went with Ubuntu.
Install Some Helpers
Windows Terminal
Windows Terminal is a new terminal emulator from MS. Its in preview mode as I write this but still worthwhile to have it. Get it from Windows Store
VS code Remote Development
VS Code is one of the best editors out there. If you don’t have VS Code already, install it. It supports working directly with WSL2. For this enable the extension pack Remote Development
GUI Support
For majority applications which doesn’t support remote development like VS Code, you can get over it by running the application directly in WSL env and then stream the desktop to the windows side. We will be using the good old X Server for this.
WSL side configuration
Add the following in ~/.bashrc
:
# Enable GUI applications
export DISPLAY=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'):0.0
export LIBGL_ALWAYS_INDIRECT=1
Win10 side configuration
VcXsrv
- Install VcXsrv
You can create a configuration via the GUI, but I will provide my working configuration for easier setup. Copy following configuration to a new file -
config.xlaunch
:<?xml version="1.0" encoding="UTF-8"?> <XLaunch WindowMode="MultiWindow" ClientMode="NoClient" LocalClient="False" Display="0" LocalProgram="xcalc" RemoteProgram="xterm" RemotePassword="" PrivateKey="" RemoteHost="" RemoteUser="" XDMCPHost="" XDMCPBroadcast="False" XDMCPIndirect="False" Clipboard="True" ClipboardPrimary="True" ExtraParams="" Wgl="True" DisableAC="True" XDMCPTerminate="False"/>
Open
config.xlaunch
and let it run in the background. Try running a GUI application from WSL and it should show up in the Win 10.Troubleshooting: Firewall might interfere with X Server. So disable it if you face any issue.
Run VcXsrv automatically
Remembering to run VcXsrv beforehand is an easy thing to forget. So we will automate it to start automatically on Win10 startup.
- Open
Task Scheduler
from Start menu. - Create a new Task from the menu on the right
Create Task
wih task nameLaunchVcXsrv
Triggers
- add a new Trigger -At log on
ofSpecific user
(select current user) with statusEnabled
Actions
- add a new action -Start a program
and provide the path of theconfig.xlaunch
inProgram/script
to executeConditions
- uncheck all boxes
Following is the Task setting which I have configured (I have replaced some IDs with ...
). Compare your setting with this if you are facing any issue.
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>...</Date>
<Author>...</Author>
<Description>Launch VcXsrv on startup</Description>
<URI>\LaunchVcXsrv</URI>
</RegistrationInfo>
<Triggers>
<LogonTrigger>
<Enabled>true</Enabled>
<UserId>...</UserId>
</LogonTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>...</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\path\to\config.xlaunch</Command>
</Exec>
</Actions>
</Task>
WSL - Install programs
Install all necessary programs/libraries/IDEs and you are good to go. I installed Intellij IDEA in WSL and once I run it from idea.sh
it will open up in my Win10 given I have my VcXsrv running.
Handy commands
- Open current folder in Explorer:
explorer.exe .
- Open current folder in VS Code:
code .