I’m starting a new job in two days writing Go for Linux, so I thought I ought to do a little experimentation with debugging Go in VS Code on Windows Subsystem for Linux. I already had WSL enabled but had not used it much. After a little experimentation on my laptop, I discovered that debugging does not work in WSL (v1) but it does in WSL 2.
After a little experimenting, I got it working on my laptop and determined to do the process again while taking some notes on my desktop machine. This is as much to remind myself as to inform others. If you find it useful, so much the better.
Existing Start Point
I started with a current version of Windows 10 Pro with WSL enabled and Ubuntu installed from the Windows Store. I also had Go installed in Windows but not in WSL. This gives you WSL 1 and the Go Delve debugger will not run remotely via VS Code on WSL 1.
Windows Insider Program
You need a newer version of Windows 10 that comes with WSL 2. For that, you need to join the Windows Insider Program. Once you do that, you can use Windows Update to install the latest version of the OS, currently build 19041.1.
Enable WSL 2
Now you need to open PowerShell in Administrator mode and follow the instructions here.
You run these two commands to enable WSL 2:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Now if you run
You should see something like this:
PS > wsl --list
Windows Subsystem for Linux Distributions:
Now you need to convert to use WSL 2 rather than 1 which is the default by running the following with the output shown:
wsl --set-version Ubuntu 2
wsl --set-default-version 2
wsl --list --verbose
PS C:\WINDOWS\system32> wsl --list --verbose
NAME STATE VERSION
* Ubuntu Stopped 2
This step is optional but I recommend it. Go to the Windows Store and install the Windows Terminal. It makes switching between WSL bash and PowerShell and Windows cmd prompt very easy. You can learn more about it here.
Update Your Ubuntu WSL 2 Environment
Now open the Ubuntu bash shell and run the following commands to update, upgrade and install golang.
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install golang-go
Once you have updated and upgraded and installed Go, you can run go version and get go1.10.4, the current Ubuntu default which is not the version you need. This version will not run with the new version of the Delve debugger that the VS Code extension uses. But not worry, upgrading to a new Go is easy.
The above install will put the 1.10.4 version in the /usr/lib/go directory. Do a more VERSION in that directory and you’ll see that I’m right.
Upgrade Go to Latest
Now you need to download and install the latest Go version, currently 1.13.5. Execute the following commands:
sudo tar -xvf go1.13.5.linux-amd64.tar.gz
sudo mv go /usr/local
This will put the new version of Go in a different directory allowing you to revert to the previous installed version if you want. After the files are in place, you need only update your environment Go paths. Open the ~/.bashrc file in your favorite editor. For quick stuff like this I use nano. Now add the following lines at the end of the file:
Either close your bash shell and open a new one to get the udpated settings or execute source ~/.bashrc to update the current session. Then execute go version and you should see go version go1.13.5 linux/amd64.
VS Code from WSL
Open VS Code from Bash Shell with:
VS Code will open after the shell installs VS Code Server for x64. If you have the Go extension installed already, all you need to do is install it to WSL. Just open the extensions and click on the install button.
A reload of VS Code will be required. In fact, I found that a reload was required or at least needed to get some of the following steps to work properly but that was a minor inconvenience.
Create a new directory called gotest and a file in it called app.go using VS Code. Paste in the code from https://github.com/tylerjensen/d-go-rust/blob/master/Go/Hello/app.go and hit F5 to debug after adding a break point. VS Code will require that you install go-outline and the delve debugger, so just click Install on the pop up notifications.
Now you’re up and running, building and debugging Go in Linux in WSL 2.