Home Golang How To Install Go Language In Linux

How To Install Go Language In Linux

Install Golang In Linux

By sk
12.7K views

This guide explains what is Go Programming language and how to install Go language in Linux and finally how to create a test program in Golang with a simple example.

What is Go Language?

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.

Now let us go ahead and see two ways to install Golang and configure Go programming language in Linux.

Install Go Language in Linux

As stated above, we can install Golang either using your distribution's default package manager or compile and install Golang from source. We will see both.

1. Install Golang using Package Managers

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

To install Golang in Alpine Linux, run:

$ sudo apk add go

On Arch Linux and variants like EndeavourOS and Manjaro Linux:

$ sudo pacman -S go

On RPM based systems like Fedora, RHEL, and its clones like CentOS, AlmaLinux and Rocky Linux, run:

$ sudo dnf install golang

On older RHEL systems, use yum instead of dnf:

$ sudo yum install golang

On DEB based systems such as Debian, Ubuntu, Linux Mint and Pop OS, you can install Go language using command:

$ sudo apt install golang

On SUSE/openSUSE:

$ sudo zypper install golang

Once installed, Check the Golang version using command:

$ go version

Sample output:

go version go1.18.1 linux/amd64

1.1. Test Installation

Create a file named hello.go :

$ vi hello.go

Add the following code in it:

package main

import "fmt"

func main() {
        fmt.Printf("hello, world\n")
}

Save the file and close it.

Now, run the code using command:

$ go run hello.go 

Sample output:

hello, world

2. Install Golang 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 and Ubuntu 22. 04 versions. However, these steps are same for other Linux distributions.

Download the latest Golang version from here.

$ wget https://go.dev/dl/go1.19.5.linux-amd64.tar.gz

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

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

Sample output:

36519702ae2fd573c9869461990ae550c8c0d955cd28d2827a6b159fda81ff95  go1.19.5.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.19.5.linux-amd64.tar.gz

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

2.1. Configure Go Language

Now, we need to set the executable path of Go language in your User's profile file.

To do so, edit your user profile file:

$ vi ~/.profile

Add the following line:

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

For a system-wide installation, add the above line in /etc/profile file.

Type :wq to save and close the file.

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

2.2. Create Workspace Directory

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 (i.e. /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

If you have done system-wide installation, run this command instead:

$ source /etc/profile

2.3. Check Golang Version

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.19.5 linux/amd64

To view the Go lang environment information, run:

$ go env

Sample output:

GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/ostechnix/go_projects/bin"
GOCACHE="/home/ostechnix/.cache/go-build"
GOENV="/home/ostechnix/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ostechnix/go_projects/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ostechnix/go_projects"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build350461959=/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.

2.4. Create a Simple 'hello world' Program using Go

We know now how to install, configure and update 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 inside the hello directory:

$ vi $HOME/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:

Hello, World
Run Hello World Program in Golang
Run Hello World Program in Golang

Congratulations! You have just created a sample program using Go language.

Update Golang

If you have installed Go using the package manager, simply update your system. If the latest version is available in the repositories, Golang will be updated automatically.

If you have installed Golang from source, follow the instructions.

First, remove the existing version.

To uninstall Golang, delete the /usr/local/go directory using command:

$ sudo rm -rf /usr/local/go

Download and install the new version as mentioned in the "Install Golang from Source" section above.

$ wget https://go.dev/dl/go1.19.5.linux-amd64.tar.gz

Go to the location where you downloaded the tar file and extract the archive file to your $PATH:

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

Make sure that your PATH contains /usr/local/go/bin.

$ echo $PATH | grep "/usr/local/go/bin"

Getting Help

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.

Remove Go Language

In case you don't want Go language anymore, you can uninstall Golang by simply removing the go directory i.e /usr/local/go.

$ rm -rf /usr/local/go

Double the path before hitting the ENTER key.

if you have installed Golang using the package managers, you can use the respective uninstall command to remove Golang. For instance, run the following command to uninstall Go programming language on Debian-based systems:

$ sudo apt remove golang

Also, delete the workspace directories as well.

Conclusion

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.

Resource:

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