Install the Latest Version of Golang on Ubuntu

Updated on December 3, 2020
Install the Latest Version of Golang on Ubuntu header image

Introduction

Go, or Golang is an open-source language developed by Google to create simple, reliable, and efficient software. This tutorial explains how to install Golang on a Vultr Ubuntu cloud server instance.

Prerequisites

This guide is tested on a new Vultr Ubuntu 20.04 LTS cloud server instance, and works on any version of Ubuntu. Follow our best practice guides before installing Golang.

1. Download Golang

Because the Golang package is not always up to date in the Ubuntu repository, it's preferred to download the official website's latest version.

Change to a temporary directory for the installation.

$ cd /tmp

Select the latest package for your architecture from https://golang.org/dl/ and download it.

$ wget https://golang.org/dl/go1.<VERSION_NUMBER>.linux-amd64.tar.gz

2. Extract to the Installation Location

Extract the Golang executable to /usr/local.

$ sudo tar -C /usr/local -xzf go1.<VERSION_NUMBER>.linux-amd64.tar.gz

3. Set Environment

To use Golang, set the required environment variables in your .profile.

$ echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile
$ echo "export GOPATH=~/.go" >> ~/.profile

Reload your profile to begin using Golang.

$ source ~/.profile

4. Test the Installation

Verify you installed the expected version of Golang.

$ go version
# go version go1.<VERSION_NUMBER> linux/amd64

5. Hello World

  1. Create a Hello World program to test your environment.

     $ nano helloworld.go
  2. Paste the following in helloworld.go.

     package main
    
     import (
         "fmt"
     )
    
     func main() {
         fmt.Println("Hello world!")
     }
  3. Save the file and exit the Nano editor.

  4. Run the program.

     $ go run helloworld.go
     # Hello world!

Summary

You have installed Golang. See more information at golang.org.