Running Ubuntu on Windows 10 with WSL2 — Part 3: Visual Studio Code and go

Mauro Giusti
7 min readSep 3, 2020

Note: This is the third of a series of articles about running Ubuntu on Windows 10, check out the first two chapters: Running Ubuntu on Windows 10 with WSL2 and Running Ubuntu on Windows 10 with WSL2 — Part 2: Windows Terminal

For the third episode of our saga about running Ubuntu on Windows 10 with WSL2, after installing and configuring Windows Terminal, we are going to focus on Visual Studio Code, how to connect it with WSL2 / Ubuntu and work directly on the Linux file system from Windows. We will also install and configure golang (including debugging) and show our favorite VS Code extensions.

Install Visual Studio Code

The first step is to install VS Code on Windows 10; go to https://code.visualstudio.com/download and download the latest installer for Windows:

Then run the VSCodeUserSetup-x64-<version number>.exe downloaded file (version was 1.48.2 at the time of writing):

Follow the installation wizard to set up VS Code.

Connect VS Code to WSL

The magic trick to connect to the Ubuntu file system is to install the Remote: WSL extension.

When you launch VS Code, it will detect that you are running WSL on your PC and recommend to install the Remote: WSL extension.

If it does not show up or you dismiss the popup, you can find it in the VS Code Extensions window (it will also be one of the recommended ones)

Once you install it, a small double-arrow icon in the bottom left corner will allow you to open a remote folder:

Click on the remote >< icon and then select Remote-WSL: New Window

You will be now connected to the Ubuntu file system, with no open folders:

Open a folder and you will be good to go:

Make VS Code look good!

I don’t know you but I need my IDE to look good, I use the Cobalt 2 Theme Official that is a dark theme with good contrast and great code readability (after installed, remember to set the color theme):

I also use the vscode-icons extension for noice icons:

Now, if you open the shell inside VS Code, something looks weird:

The command prompt is not using the Powerline fonts, to change this, open File -> Preferences -> Setting and type fontFamily in the Search settings box, then select Features/Terminal in the tree view and type Source Code Pro for Powerline and save, the command prompt will automatically update:

Other VS Code Settings

I use other VS Code settings that I want to share with you, to make things faster, I will edit them in VSCode settings.json file.

To edit your VS Code configuration in JSON, open the command palette (View -> Command Palette, or Ctrl+Shift+P), type Open Settings JSON and select Preferences: Open Settings (JSON):

The settings.json file with all the non-default VS Code settings will open:

I use these additional settings:

"editor.wordSeparators": "`~!@#$%^&*()=+[{]}\\|;:'\",.<>/?",
"editor.renderWhitespace": "all",
"diffEditor.ignoreTrimWhitespace": false,
"update.mode": "none",
"extensions.autoUpdate": false,
"window.title": "[${folderName}]${separator}${dirty}${activeEditorShort}${separator}${appName}",
"files.trimTrailingWhitespace": true,
"editor.tabSize": 2
  • editor.wordSeparators: I removed the from the word separators so I can select identifiers with a in it via double-click on a word (sometimes these can be used in bash scripts)
  • editor.renderWhitespace: I always want to see all the spaces in my source files
  • diffEditor.ignoreTrimWhitespace : when I do a git merge, I want to see the differences due to space changes
  • update.mode and extensions.autoUpdate: I don’t want the extensions to autoupdate, I need to control when they are updated as sometimes with WSL they break
  • window.title : I added the [foldername] to the beginning of the window title, so I can recognize different go projects instances by their folder names (I can have 5 or 6 open at the same time, this is a saviour!)
  • files.trimTrailingWhitespace : when a file is saved, all the extra spaces at the end of a line are trimmed out
  • editor.tabSize: I like to use 2 spaces for tabs

Add the ones you want to use to your settings.json file and save it:

Install golang and gvm (go version manager)

During the lifetime of our project, we changed the version of go a few times. To make it easy to change it, I use gvm (go version manager), let’s use it to install go 1.14.

First, install the Ubuntu gvm prerequisites:

$ sudo apt-get update
$ sudo apt-get install curl git mercurial make binutils bison gcc build-essential

Then install gvm:

$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

Source the gvm file on the current shell so we can to install the first version of go:

$ source $HOME/.gvm/scripts/gvm

Install a version of go (1.14 here), note that we are using binary for the first installation, and set it as default:

$ gvm install 1.14 -B
$ gvm use go1.14 --default

Now, add this to your ~/.zshrc :

[[ -s "${HOME}/.gvm/scripts/gvm" ]] && source "${HOME}/.gvm/scripts/gvm"

Note that when running gvm use, your $GOPATH environment variable is changed to what gvm wants, this is not cool with me, I want it to point to $HOME/go, so I also added the following lines to my ~/.zshrc :

gvm use go1.14
export GOPATH="${HOME}/go"
export GOROOT="${GOROOT:-$(go env GOROOT)}"
export PATH=$PATH:$GOPATH/bin

Open a new shell in VS Code to validate that your settings are correct:

And now you can build the golang hello world example:

Debugging golang with VS Code

If you want to debug golang in VS Code, install the Go for VS Code extension, then enable Go: Use Language Server Setting in Remote WSL:

After you restart VS Code, you will be prompted with the suggested gopls extensions, go ahead and install them.

Then add a breakpoint to your go code and press F5. You will be asked to create a default Run and Debug VS Code profile:

You will be prompted with the dlv command — go ahead and install it, now you can step-by-step debug golang code — sweet!

Best VS Code Plugins

To finish up our VS Code configuration, we will install our favorite extensions.

Project Manager by Alessandro Fragnani allows me to quickly switch between different projects in separate folders — it installs locally and manages the folders on Ubuntu:

I use three git extensions (I try to use git from the command line but I understand pictures better, what can I do :D).

Git Graph helps me visualize those complicated git branches and merges graphically :

Git Tree Compare by Maik Riechert shows me the differences between my working branch and master for easier merges

GitLens — Git Supercharged by Eric Amodio for those amazing inline git blame and git code lens

So far so good

That brings us to the end of this long third chapter. Now you should have a powerful configuration of Ubuntu, VS Code and Windows Terminal on your Windows 10 machine that will allow you to do coding directly on the Linux file system while having a PC available for gaming (err….. :D).

In a follow up chapter, we will install and configure Docker, Kubernetes and the Azure cli, to complete our work setup.

Until then,

Ciao.

--

--