How to Install Go Programming Language on Linux (Ubuntu/Debian/CentOS)

Go or Golang, is an open-source programming language designed to be fast, easy, efficient and scalable. It is a modern programming language developed by Google that helps you develop simple, reliable and efficient software. There are some popular DevOps tools built on Go, including Docker, Kubernetes, Terraform, Grafana, Hoshicorp Vault, etc.

In this tutorial, we will show you how to install Go on a Linux System. With this guide, you can install Go on various Linux systems, including Debian 10, Ubuntu 18.04 and CentOS 7.

What we will do:

  • Download Go Latest Stable Version
  • Configure System Environment for Go
  • Testing - Hello World

Step 1 - Download Go Latest Stable Version

Download the latest Go version '1.13' using the wget command below.

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

Once it's complete, verify the downloaded tarball version using the command below.

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

And you will get the 'sha256' checksum hashes of the file as below.

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

Next, extract the Go tarball file 'go1.13.linux-amd64.tar.gz' to the '/usr/local' directory.

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

Now the Go binary package has been installed to the Linux system under the '/usr/local/go' directory.

Unpack Golang archive

Step 2 - Configure System Environment for Go

After downloading and install Go binary package, we need to set up the system environment on the Linux system. We can edit the 'profile' for your shell. For bash user, you can edit the '.bash_profile' and for zsh shell users is the '.zprofile'.

For Debian and Ubuntu User

For the debian user, you will need to edit/create the '~/.bash_profile' configuration on the home directory.

Go to the user home directory and edit/create the configuration '~/.bash_profile' using vim editor.

vim ~/.bash_profile

Now paste the environment configuration for Go lang below.

export GOPATH=$HOME/work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

Save and close.

After that, we need to load the '~/.bash_profile' on every login. You need to load it through the '~/.bashrc' configuration.

Edit the '~/.bashrc' file using vim editor.

vim ~/.bashrc

Add the configuration below to the bottom of the line.

source ~/.bash_profile

Save and close.

Configure system environment

Now log out from your shell and log in again, and the environment variable for Go has been configured.

For CentOS 7 Users

For the CentOS 7 user, you just need to edit the '~/.bash_profile' file and load it manually.

vim ~/.bash_profile

Paste the following variables.

export GOPATH=$HOME/work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

Save and close.

Now load it manually using the 'source' command as below.

source ~/.bash_profile

Now the environment variables for Go has been configured.

Edit user profile

For ZSH Users

For zsh users, you will need to add the system environment configuration to the '~/.zprofile' configuration.

Go to the user home directory and create/edit the configuration file '~/.zprofile' using vim editor.

vim ~/.zprofile

Paste the following configuration

export GOPATH=$HOME/work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

Save and close.

And the '~/.zprofile' will be loaded automatically at login. To make an effect, log out from your shell and log in again.

Create the Working Directory

Next, after configuring the environment variable for the Golang, we need to create the Go working directory under the home user directory.

mkdir -p $HOME/work

After that, check the environment that we've created using the following commands.

echo $GOPATH
echo $PATH

Now you will get the '$GOPATH' as the Go working directory and the Go binary path has been added to the '$PATH' environment.

And you will be able to run the go commands below.

go version
go --help

As a result, Golang environment configuration on the Linux system has been completed successfully.

Check go version

Additionally: Set Go Environment Variable System-wide

To setup Go environment variable globally for all users that using the Bash shell on the system, you will need to define the environment variable under the '/etc/profile.d' directory. And it's the same for all major Linux systems that used bash as default shell such as Debian, Ubuntu CentOS.

Now Go to the '/etc/profile.d' directory and create a new configuration file 'go.sh' using vim editor.

cd /etc/profile.d/
vim go.sh

Paste the Go environment variable as below.

export GOPATH=$HOME/work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

Save and close, then make the file executable.

chmod +x /etc/profile.d/go.sh

And all users inside the system that used Bash as its shell will be able to run go by default.

Set Go Environment Variable System-wide

Step 3 - Testing - Hello Go

In this step, we're to test our Go installation by creating the simple 'Hello World' program. And the '$GOPATH' will be the primary working directory.

Go to the '$GOPATH' working directory, create new 'src' and 'bin' directory on it.

cd $GOPATH
mkdir -p src/ bin/

Now create a new 'hello' project directory under the 'src' and go into it.

mkdir -p src/hello/
cd src/hello/

Create the Golang file 'hello.go' using vim editor.

vim hello.go

Paste the simple Go script below.

package main

import (
    "fmt"
)

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

Save and close.

Next, compile the 'hello.go' program to the '$GOPATH/bin' directory using the following command.

go build -o $GOPATH/bin/hello hello.go

Now the compiled program called 'hello' will be available on the '$GOPATH/bin' directory, and you can just run the 'hello' command as it has been defined on the binary '$PATH' directory.

hello

And you will get the result as below.

Hello World Go program

As a result, the Go installation on your Linux system has been completed successfully.

Reference

Share this page:

1 Comment(s)