Debug Go in Windows Subsystem for Linux 2

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

wsl --list 

You should see something like this:

PS > wsl --list
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

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

Windows Terminal

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:

wget https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz

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:

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

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:

code .

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.

InstallGoWSL

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.

Random Thoughts on Programming Languages

I noted with interest that Microsoft is delving into Rust for some things in the OS where they have memory related security holes. 

We seem to need a language for each of the following:

  • OS
  • Desktop GUI
  • Web front end
  • Web back end
  • Services (web backend without a front end)
  • Data querying 
  • Systems (data serving, near the metal)
  • CLI utils to listen to the GUI or the anti-GUI user or the scripters
  • Script for the scripters

C and C++ remain relevant for the OS but Rust seems to be scratching at the surface.

Javascript rules the browser (written in C++) but WebAssemby (also written in C++) is scratching at the door and will bring many languages to play there once WA has cut ties with JS interop dependency for DOM and HTTP coms.

The web backend and services is a mess of nearly every language on the planet. From JS to Ruby (wretch). Even here JS dominates with Node. The rest of the field is up for grabs.

Data querying is still SQL followed my a mishmash of things for querying noSQL datastores, usually some form of JSON over REST, though binary serialization in gRPC is growing for both data and services.

Systems are still primarily C and C++ but Java and C# play some Part in some database platforms. Go, Rust and others are also knocking on the door. Kubernetes is a good Go example, a tech that is red hot right now.

The CLI world is a free for all. Python is fast becoming a favorite for the automation and ML scripters. This category does seem to favor interpreted over compiled but even that is a pile of chaos and debate.

One thing is certain. There is only one master language in all of computing. Heaven help us. It's English. And there is no consistent compiler, interpreter or IDE for the foul thing.