How to Install Go on Ubuntu 20.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install Go on Ubuntu 20.04, let's first understand – What is Go?

Go, also known as Golang, is an open-source programming language developed by Google. It's designed for simplicity, efficiency, and scalability, making it a popular choice for building web applications, system tools, and distributed systems. With its robust standard library and built-in concurrency support, Go offers developers a productive environment for creating high-performance software.

Whether you're a beginner or an experienced programmer, Golang's clean syntax and strong typing make it easy to learn and use. Explore the power of Go to streamline your development process and unlock new possibilities for your projects.

In this tutorial, you will install Go. We will also address a few FAQs on how to install Go on Ubuntu 20.04.

Advantages of Go

  1. Simplicity: Golang's clean syntax and minimalistic design make it easy to learn and read, promoting faster development.
  2. Concurrency: Built-in goroutines and channels enable efficient handling of multiple tasks, improving scalability and performance.
  3. Efficiency: Golang's compilation to machine code results in highly optimized executables, reducing memory footprint and improving speed.
  4. Scalability: Go's efficient garbage collection and lightweight processes allow for the development of high-performance, scalable applications.
  5. Standard Library: Golang's extensive standard library provides a wide range of packages, saving development time and effort.

Step 1 - Install Go on Ubuntu 20.04

1) You will first download the Go tarball. So, before downloading it, visit the official Go-downloads page. Then, check for any new version available.

2) Next, run the below command as a user with the sudo privileges. It will download and extract the Go-binary archive in /usr/local directory:

wget -c https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local

Step 2 - Adjust the Path Variables

1) After that, adjust the Path Variable. Add the location of the Go directory to the $PATH environment variable. This will enable the system to know where to search for Go-executable binaries.

2) Now, append the below line either to the /etc/profile file (for system-wide installation) or HOME/.profile file (for current-user installation):

~/.profile
export PATH=$PATH:/usr/local/go/bin

3) After that, save the file and load the new PATH environment variable into the existing shell session.

source ~/.profile

Step 3 - Verify Go Installation

1) Now, you need to verify the Go Installation.

go version

2) You will get a similar output as shown below.

Output

go version go1.14.2 linux/amd64

Step 4 - Getting Started with Go

In order to test the Go installation. First, you will create a workspace. Then, build a simple program that prints the “hello world” message.

1) Usually, by default, the GOPATH variable is set to $HOME/go. It specifies the location of the workspace. Now, to create the workspace directory run:

mkdir ~/go

2) After that, inside the workspace create a new directory src/hello.

mkdir -p ~/go/src/hello

3) In that directory create a file namely hello.go.

package main

import "fmt"

func main() {
    fmt.Printf("Hello, World\n")
}

4) After that, navigate to the ~/go/src/hello directory. Moreover, run go build to build the program.

cd ~/go/src/hello
go build

The above command will build an executable file namely hello.

5) You will be able to run the executable. Do it by running the command below:

./hello

The output will be as:

Output

Hello, World

FAQs to Install Go on Ubuntu 20.04

Can I install Go using a package manager like apt-get?

While Ubuntu's package manager can install Go, it's often an older version. It's recommended to download and install the official binary distribution to get the latest version.

How do I set the GOPATH environment variable?

Go doesn't require a GOPATH variable by default. However, if you prefer using one, you can set it in your shell profile file like .bashrc or .profile using the export GOPATH=/path/to/gopath command.

How do I update Go to the latest version on Ubuntu?

To update Go, you need to download the latest binary distribution from the official website and repeat the installation steps, replacing the existing Go installation.

How can I uninstall Go from Ubuntu 20.04?

To uninstall Go, simply remove the Go directory from where it was installed, typically /usr/local/go. Additionally, you can remove any references to Go from your shell profile file.

Can I have multiple versions of Go installed on Ubuntu 20.04?

Yes, you can have multiple versions of Go installed by extracting each version to a separate directory and updating the PATH variable accordingly. This allows you to switch between versions as needed.

What text editor or IDE should I use with Go on Ubuntu?

There are several popular text editors and IDEs for Go development on Ubuntu, including Visual Studio Code with the Go extension, GoLand, Sublime Text with GoSublime, and Vim with plugins like vim-go.

How do I compile and run a Go program on Ubuntu?

After installing Go, you can compile and run a Go program by navigating to the program's directory and using the go build command to compile it into an executable. Then, execute the compiled binary with ./program_name.

Conclusion

We hope this detailed tutorial helped you install Go on Ubuntu 20.04. To learn more about Go installation, check out the official Golang Documentation.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.