Installing and Using Golang on Ubuntu 20.04 and VSCode

Kaigo
2 min readMay 3, 2020

If you think you may have previous version of golang install, or you’re not sure, run the following commands to completely remove of any trace of golang on your system.

sudo apt remove golangsudo apt autoremove

Next we’re ready to install golang. We will install it in the location recommended by the official golang docs, which is /usr/local/go

So first head to the website and download the latest version-> https://golang.org/dl/

Note: I also don’t recommend installing through apt on Ubuntu, as it installs the package in weird location and on top of that, it won’t be the latest version.

Next, cdinto your download location, and extract the tarball (tar.gz) file.

cd /Downloadssudo tar -xvf go1.14.2.linux-amd64.tar.gz

And simply move the new go directory to the recommended location.

sudo mv go /usr/local

Now we have the go language and compiler in the correct location we just need to be able to access it through our terminal. As at this point the system does not know about golang, we know this because if we type go versionin a terminal we get. .

‘go’ not found

So we tell the system about golang by adding it to the global PATH.

sudo echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc

or if you’re a zshell user.

sudo echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.zshrc

If this line doesn’t make sense to you, it is basically adding a new line to the hidden file .bashrc and this new line adds the golang location to the global PATH. It’s a very common thing to do when installing any software manually.

Restart the terminal and verify the manual install with go version

You should get. .

go version go1.14.2 linux/amd64

Now I’ll create a new folder for my go projects and open it in vscode.

mkdir ~/gocd go/mkdir awesome-projectcode awesome-project

create a new file for example, main.goand paste in some hello world code.

package main

import "fmt"

func main() {
fmt.Printf("hello, world\n")
}

Extra: download the following extension to make running go programs easier.

Press the run button on the top right side of the screen to see your program output!

--

--