How to install and setup Golang development under WSL 2

Ben Braunstein
3 min readApr 19, 2020

--

Go (commonly known as Golang) is a statically typed, compiled programming language designed at Google that was built to feel like a dynamically typed interpreted language.

Go is designed to be syntactically similar to C, but Go includes memory safety, garbage collection and structural typing.

I have been hearing about the popularity of Go recently and I decided that I wanted to give it a try myself. I have experience in C++ and being syntactically similar to C, I thought giving Go a try wouldn’t be too difficult.

The Go Playground

Golang’s website has a built in Code Playground, where you can start writing and running some Go code right in your browser without the need to download any tools. It makes it nice and simple to test the language and learn some of it’s syntax. You can find that over at https://play.golang.org/

Obviously, the goal is to download Go to your computer so you can write your code on your own computer and save/share any code you write. That is what I will teach you how to do today.

If you are using Linux, macOS or Windows this is easy enough as you the instructions from their website over at https://golang.org/dl/ are more than adequate.

WSL 2 users are always in a strange situation, we are using Windows, but really only as a portal to Linux. To setup WSL 2 please refer to my previous blog post located here.

Once you have WSL 2 set up installing Go is actually pretty easy via the command line. The first step is to head over to Golang’s download page and check the newest version of Go.

At the time of writing this blog post, the most recent version of Go is 1.14.2.

Now its time to download Go to your computer, open up your terminal and enter the following commands, but replace 1.14.2 with the current version of Go at the time of downloading.

wget https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz
sudo tar -xvf go1.14.2.linux-amd64.tar.gz
sudo mv go /usr/local

Next up open up your .bashrc file. You can find it by navigating to the home path, opening up the current directory in Windows Explorer and opening up .bashrc with a text editor of your choice.

cd ~
explorer.exe .

Add the following three lines to the botom of your .bashrc file and save it.

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

Refresh your terminal or execute the command source ~/.bashrc

Go should be set up you can check your version with the following command!

go version

Make sure it returns the same version you were hoping to.

If you use VS Code with WSL 2 you probably already have the Remote WSL extension. Another extension that you should download is the Go extension for VS Code.

Good Luck trying out Golang. I hope you love it!

--

--