Home Golang How To Install Go Language In Linux

How To Install Go Language In Linux

By sk
Published: Last Updated on 6.9k views

Go, also known as GoLang, is an open source programming language developed at Google by Robert Griesemer, Rob Pike, and Ken Thompson in 2007. The Go language 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 Arch Linux and derivatives:

$ sudo pacman -S go

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

$ sudo yum install golang


$ 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


$ sudo zypper install golang

Install Go language from source

Go is available in the default repositories of some Linux distributions. However, the Go language version in the default repositories might be bit outdated. So, it is always recommended to use the most recent version by downloading the tarball from the official website and manually install it 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://dl.google.com/go/go1.10.2.linux-amd64.tar.gz

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

$ sha256sum go1.10.2.linux-amd64.tar.gz

Sample output would be:

4b677d698c65370afa33757b6954ade60347aaca310ea92a63ed717d7cb0c2ff go1.10.2.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.10.2.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

Type :wq to 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.10.2 linux/amd64

To view the Go lang environment information, run:

$ go env

Sample output:

GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build286572886=/tmp/go-build -gno-record-gcc-switches"

If you see output something like above, congratulations! Go language is ready to use. You can start coding your go 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
Hello World Program In Golang

Hello World Program In Golang

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 provided 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 code a simple program.


Related read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More