How to install Go 1.17 on Ubuntu 20.04|22.04

In this tutorial, we are going to explore how to install Go 1.17 on Ubuntu 20.04|22.04.

Golang is an open-source programming language that is easy to learn and use. It is built-in concurrency and has a robust standard library. It is reliable, builds fast, and efficient software that scales fast.

Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel-type systems enable flexible and modular program constructions.

Go compiles quickly to machine code and has the convenience of garbage collection and the power of run-time reflection.

Installing Go 1.17 on Ubuntu 20.04

1. Run system updates

To begin with, we need to run system updates in order to make our current repositories up to date. To do so we need to run the following command on our terminal.

$ sudo apt update && apt upgrade -y

When both upgrades and updates are complete, go ahead to download go from its download page.

2. Download Go 1.17

To download go, move into the go download page and click download. When the download is complete go to the download page and extract.

When the download is complete, now move ahead and install go onto your system.

3. Install Go 1.17 on Ubuntu 20.04

For us to have a working go installation, we need to extract the archive downloaded into usr/local, creating a Go tree on /usr/local/go. Remove the previous go modules before proceeding. Use the following command to perform this operation.

$ tar -C /usr/local -xzf go1.17.5.linux-amd64.tar.gz

Now that we have extracted go, we now need to create PATH so that we can tell where we will find our Go modules whenever we want to access them.

Add /usr/local/go/bin to the PATH environment variable.

On your $HOME/.profile add the following line:

$ export PATH=$PATH:/usr/local/go/bin

For the above command to be applied system-wide make sure to add it to /etc/profile.

To apply the changes immediately, run source $HOME/.profile

$ source $HOME/.profile

To verify the go version run the following command

$ go version
go version go1.17.5 linux/amd64
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build452429440=/tmp/go-build -gno-record-gcc-switches"

4. Write hello world sample program.

To write our first program do the following:

Move to your home directory and create a hello directory

$ cd $HOME
$ mkdir hello
$ cd hello

Enable dependency tracking for your code. To enable dependency tracking, create a go.mod module. This module stays with your code. To enable it run the go mod init. Run the following to enable dependency tracking.

$ go mod init nextgentips/hello
go: creating new go.mod: module nextgentips/hello

Open your preferred text editor and write the following code

Package main
import 'fmt'

func main() {
         fmt.Println("Hello, world")
}

Run your code with the following command.

$ go run
Hello, world

Conclusion.

In this guide, we have learned how to install and set up the go path. Happy coding!

Best Golang Books

If you want to learn more about Go programming read this book.

About Kipkoech Sang

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Kipkoech Sang →

Leave a Reply

Your email address will not be published.