How to install Go programming language in Linux

Go also known as Golang is an open source programming language developed by a team (Robert Griesemer, Rob Pike, and Ken Thompson) at Google and many contributors from the open source community.

Go is expressive, simple, reliable, concise, clean, and efficient which make programmers to write/develop application easily and more productive, that too on multi-core and networked machines. Go language was designed to resolve common criticisms of other languages while maintaining their positive characteristics.

Some notable open-source applications using Go.

  • Docker, a set of tools for deploying Linux containers.
  • Snappy, a package manager for Ubuntu.
  • Dropbox, migrated some of their critical components from Python to Go
  • Google, for many projects, notably including download server dl.google.com
  • MongoDB, tools for administering MongoDB instances
  • Netflix, for two portions of their server architecture
  • SoundCloud, for “dozens of systems
  • Uber, for handling high volumes of geofence-based queries

Installing Go on Linux via package manager

Most of the Linux distribution included Go language on official repository, so we can easily install but will get bit older version. If you preferring latest release, try to build manually.

[Install go on Debian/Ubuntu/Mint]
$ sudo apt-get install golang

[Install lnav on RHEL/CentOS]
$ sudo yum install golang

[Install lnav on Fedora]
$ sudo dnf install golang

[Install lnav on openSUSE]
$ sudo zypper install golang

[Install lnav on Mageia]
$ sudo urpmi golang

[Install lnav on Arch Linux based system]
$ sudo pacman -S golang

In order to work with Go language, create a work space directory under your home directory. Go language will keep all the files Here.

$ mkdir ~/go_proj

Make sure you have to setup Go related environment variables to make it work. Add /usr/local/go/bin line to your /etc/profile for a system-wide installation or add $HOME/.profile for user specific installation.

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

Setup GOPATH and GOBIN environment variables. GoPATH is project workspace. Add following lines to $HOME/.profile file.

export GOPATH=$HOME/go_proj
export GOBIN=$GOPATH/bin

Manual method to install Go on Linux

If you preferring for latest release, visit the golang website and download the latest archive file and follow the steps to install it.

$ wget https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz
$ sudo tar -xzf go1.8.linux-amd64.tar.gz -C /usr/local 

In order to work with Go language, create a workspace directory under your home directory. Go language will keep all the files Here.

$ mkdir ~/go_proj

Make sure you have to setup Go related environment variables to make it work. Add /usr/local/go/bin line to your /etc/profile for a system-wide installation or add $HOME/.profile for user specific installation.

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

When you install Go in different location instead of default /usr/local/go, you must set the GOROOT environment variable to point to the directory.

For example, if you installed Go to your home directory you should add commands like the following to $HOME/.profile

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

Setup GOPATH and GOBIN environment variables. GoPATH is project workspace. Add following lines to $HOME/.profile file.

export GOPATH=$HOME/go_proj
export GOBIN=$GOPATH/bin

Run the following commands to take effect.

$ source ~/.profile

Verify GoLang installation

Run the below command to see the version of Go language.

$ go version
go version go1.8 linux/amd64

Check the Go environment variables.

$ go env
GOARCH="amd64"
GOBIN="/home/magi/go_proj/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/magi/go_proj"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build742208715=/tmp/go-build -gno-record-gcc-switches"
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"

Test your installation

Check that Go is installed correctly by building a simple program. We have already created workspace directory called go_proj.

Next, make the directory src/hello inside your workspace.

$ mkdir -p ~/go_proj/src/hello

Create a file name hello.go and add the following simple program.

$ nano ~/go_proj/src/hello/hello.go

package main

import "fmt"

func main() {
    fmt.Printf("Welcome To 2daygeek\n")
}

compile the hello.go source file with the go install command

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

Run the program.

$ $GOBIN/hello

See the output.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

One Comment on “How to install Go programming language in Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *