How To Install Go Language In Linux

Install Go Language In Linux

Go, also known as GoLang, is an open source programming language developed at Google by Robert Griesemer, Rob Pike, and Ken Thompson in 2007. Go became a public open source project on November 10, 2009. Go language is being actively used in some Google’s production servers, as well as by other companies such as Dropbox, CoreOS, CloudFlare, Docker, Soundcloud, Uber and many.

In this tutorial, we will see how to install and configure Go programming language in Linux.

Install Go Language In Linux

You can install Golang either using package manager or compile and install from source. We will see both.

Install Go language using package managers

Go language is available in the default repositories of most modern Linux distributions.

On RPM based systems like RHEL, CentOS, Fedora, run:

sudo yum install golang

Or,

sudo dnf install golang

On DEB based systems such as Debian, Ubuntu, Linux Mint, you can install it using command:

sudo apt-get install golang

You can also search and install additional Go tools. On RPM based systems, run the following command to search for additional go tools:

sudo yum search golang

On DEB based systems, run:

sudo apt-cache search golang

Install Go language from source

The version of Go language in the default repositories might be outdated. If you want to install the most recent version, you can download the Go lang tarball from the official website and install it manually as described below.

The following were tested in Ubuntu 16.04 LTS server edition. However, these steps are same for other Linux distributions.

Download the latest tarball from here.

wget https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz

Next, check the integrity of the downloaded file as shown below.

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

Sample output would be:

53ab94104ee3923e228a2cb2116e5e462ad3ebaeea06ff04463479d7f12d27ca go1.8.linux-amd64.tar.gz

Please note that the SHA256 Checksum value displayed above should match with the one provided with the download link. If it doesn’t match, download new tarball and proceed.

Extract the downloaded tarball using command:

sudo tar -C /usr/local -xvzf go1.8.linux-amd64.tar.gz

This command extracts the tarball to /usr/local directory. Here, -c flag indicates the target directory.

Configure Go

Now, we need to set the executable path of Go language in user profile. To do so, edit your user profile:

sudo vi ~/.profile

Add the following line:

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

Save and close the file.

Next, setup workspace for Go language where we are going to save the Go language builds.

A workspace is a directory hierarchy with three directories at its root:

  • src contains Go source files,
  • pkg contains package objects, and
  • bin contains executable commands.

Create the above directory hierarchy for Go language workspace using command:

mkdir -p $HOME/go_projects/{src,pkg,bin}

Here, $HOME/go_projects is the directory which is where Go will build its files.

Next, we need to point Go to the new workspace.

To do so, Edit ~/.profile file:

sudo vi ~/.profile

Append the following lines to point Go to the new workspace directory.

export GOPATH="$HOME/go_projects"
export GOBIN="$GOPATH/bin"

If Go language is installed on another location other than the default location (/usr/local/), you need to specify the installation path(GOROOT) in the ~/.profile file. Say for example if you have installed go lang in your HOME directory, then you will have to add the following lines in user profile file.

export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin

Please note that if you have installed Golang using package managers, the installation path will be either /usr/lib/go or /usr/lib/golang. You need to update the correct path value in GOROOT.

Once you have specified the appropriate values, run the following command to update Go lang environment values.

source ~/.profile

Now, run the following commands to verify if you have properly installed and configured Go language.

Let us check the installed version:

go version

Sample output:

go version go1.8 linux/amd64

To view the Go lang environment information, run:

go env

Sample output:

GOARCH="amd64"
GOBIN="/home/sk/go_projects/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/sk/go_projects"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

If you see output something like above, congratulations! Go language is ready to use. You can start coding your programs.

Create A Simple ‘hello world’ program using Go

We know now how to install and configure Go language. Let us go ahead and create a simple ‘hello world’ program.

Create a separate directory to store the source code of your program. The following command creates a directory to store the ‘hello world’ program source code.

mkdir $HOME/go_projects/src
mkdir $HOME/go_projects/src/hello

Create a file called hello.go:

vi go_projects/src/hello/hello.go

Append the following lines:

package main

import "fmt"

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

Save and close the file. Then, run the following command to compile the ‘hello world’ program:

go install $GOPATH/src/hello/hello.go

Finally, run the ‘hello world’ program using command:

$GOBIN/hello

Sample output would be:

Hello, World

Congrats! You have just created a sample program using Go language. For more details, refer the help section by running the following command:

go help

Also, check the Go language documentation link rpovided at the end of this guide for more details. In case you don’t want Go language anymore, you can uninstall by simply removing the go directory i.e /usr/local/go. Also, delete the workspace directories as well.

That’s all for now. In this guide, you have learned how to install Go language in Linux and how to configure and use them to code programs. We will meet with another article shortly. If you find this guide useful, please share it on your social networks and support OSTechNix.

Cheers!

Resource:

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...