How To

Install Go on Ubuntu 18.04: Tarball Setup and Hello World Guide

Go (also called Golang) is a modern, statically typed programming language created at Google. It is designed for simplicity, fast compilation, and built-in concurrency support. Many widely used tools in the cloud and DevOps space are written in Go, including KubernetesDocker, and Grafana.

Go is not available in Ubuntu’s default repositories in a current version. The official installation method is to download the binary tarball directly from the Go downloads page, extract it to /usr/local, and update your shell’s PATH variable.

This guide walks you through installing Go 1.13 on Ubuntu 18.04, setting up your environment, and writing your first program to confirm everything works.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

Download and Install Go on Ubuntu

Before downloading, check the official Go downloads page to confirm the latest stable release. At the time of writing, that version is Go 1.13. Update the version number in the commands below if a newer release is available.

Download the Go binary tarball:

bashwget https://dl.google.com/go/go1.13.linux-amd64.tar.gz

Verify the download by checking its SHA-256 checksum:

bashsha256sum go1.13.linux-amd64.tar.gz

Output:

68a2297eb099d1a76097905a2ce334e3155004ec08cdea85f24527be3c48e856  go1.13.linux-amd64.tar.gz

Compare this hash against the value listed on the official downloads page. If they do not match, delete the file and re-download it before proceeding.

Extract the tarball to /usr/local:

bashsudo tar -C /usr/local -xzf go1.13.linux-amd64.tar.gz

Add Go to the PATH

The Go binaries are installed at /usr/local/go/bin, but your shell does not know about this location yet. Add it to your PATH by appending the following line to your ~/.profile file:

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

Apply the change to your current terminal session:

bashsource ~/.profile

Verify that Go is installed and accessible:

bashgo version

Output:

go version go1.13 linux/amd64

Write and Run Your First Go Program

Go uses a workspace directory to organize your code. By default, the workspace is at ~/go. Create it along with a directory for a sample program:

bashmkdir ~/gomkdir -p ~/go/src/hello

The Go workspace contains three key subdirectories: src for source code, pkg for compiled package objects, and bin for compiled executables. This structure keeps your work organized as your projects grow.

Create a file named hello.go inside ~/go/src/hello with the following content:

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

Navigate to the directory and compile the program:

bashcd ~/go/src/hellogo build

The go build command produces an executable binary named hello in the current directory. Run it:

bash./hello

Output:

Hello, World

Your Go environment is installed and working correctly.

Go is now set up on your Ubuntu 18.04 machine. The workflow is straightforward: write your code, run go build to compile, and execute the binary directly. Leave a comment below if you run into any issues during installation or the build process.

Cyber Defence

Recent Posts

jobs Command in Linux: List and Manage Background Processes

The jobs command is a Bash builtin that lists all background and suspended processes belonging to the…

13 hours ago

dmesg Command in Linux: Read and Filter Kernel Messages

The Linux kernel writes messages to the kernel ring buffer throughout the boot process and…

13 hours ago

How to Transfer Files with rsync over SSH: A Practical Guide

rsync over SSH combines secure encrypted transport with fast incremental transfers. Instead of copying every file…

13 hours ago

pstree Command in Linux: Visualize Running Process Hierarchies

The pstree command in Linux displays running processes in a tree structure rather than a flat list.…

13 hours ago

type Command in Linux: Show How the Shell Resolves a Name

The type command in Linux shows how the current shell would interpret a name typed on the…

1 day ago

How to Check Memory Usage in Linux: free, top, and /proc/meminfo

When a Linux system is slow or behaving unexpectedly, memory is one of the first…

1 day ago