Pitfalls Setting Up Go on WSL

At the time, getting a Go environment together on Windows seemed to require a lot of separate installations. Without a package manager like apt, working in Linux sounded more appealing. I chose Ubuntu 18.04 LTS under WSL with VS Code and wrote down the snags I encountered.

The following applies only to Windows 10 version 1903 and later.

Installing WSL

There are plenty of detailed WSL installation guides online, so I will only outline the steps here.

Open Windows Programs and Features, enable Windows Subsystem for Linux, and confirm the change. Restart when installation finishes, then open the built-in Microsoft Store and install Ubuntu. This setup does not include a graphical desktop.

I also recommend changing the Ubuntu package mirror. From my connection in China, the default source was unreliable enough that I could not even install GCC.

I used the Tsinghua mirror. It worked well for me, and its setup guide is detailed:

https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/

Configuring Go environment variables on WSL

Once Ubuntu is ready, install Go with sudo apt-get install golang.

Next I needed the Go environment settings. Tutorials I found on CSDN gave conflicting instructions and led me into several dead ends. Eventually I found that I could inspect the installed settings with go env:

The command prints values like these:

ZF2Va9XOvir8ko3.png

Find GOROOT in the output and copy its value. Open your shell configuration with sudo vim ~/.bashrc, then add a line at the end using your own path:

export GOROOT="usr/lib/go-1.10" (replace the path with the GOROOT value you copied).

Reload the file with source ~/.bashrc so the current shell picks up the setting.

Finally, run go version. If it prints the version information, Go is installed.

Using VS Code on Windows with WSL

To edit from Windows and run code in Ubuntu, install the Remote-WSL extension in VS Code. Once it is installed, remote indicators appear in the left sidebar and lower-left corner:

pY1rTWo8EZisk5x.png

I followed the prompts, entered WSL, and ran code ., but got Command 'code' not found.

A Stack Overflow answer suggested adding the VS Code executable to the path:

In WSL, run export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin".

You can also put that environment-variable setting in ~/.bashrc so it is available later. If you are new to Vim, the previous article covers a few basics.

Run code . again to install the VS Code server on Linux.

From there, follow the Remote-WSL prompts to open your project and start coding.

The result is a comfortable setup: edit in VS Code on Windows, run Go in Ubuntu, and debug from the same editor.