How To Install Go (GoLang) In Linux?

Go is a popular programming language which is created by Google. The Go programming language provides simple, reliable, and efficient applications. The Go programming language is similar to the mainstream programming languages like C, C++, Java, etc. The Go programming language also named as GoLang . There are different tools that are developed with Go. Docker, Kubernetes, Lime are mainly developed by using the Go programming language. In this tutorial, we will examine how to install the Go or GoLang and related development tools and environments in Linux.

Install Go From Go Official Website

The Go official website provides the binary files for the Go compilers and related tools. The following link provides the download page.

https://go.dev/dl/

As we can see from the following screenshot as writing this tutorial go1.17.5 is available to download and install.

Go Binary Download Page

We will download the Go binary by using the wget command.

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

As the binaries are compressed with the tar.gz we extract them with the tar command. The best place to put Go binaries is the /usr/local path.

$ sudo tar -C /usr/local/ -xzf go1.17.5.linux-amd64.tar.gz

In order to use the go binaries, they should be added to the +PATH environment variable. The current user .profile configuration file can be used to set the Go path. Just open the ~/.profile and put the following line.

$ nano ~/.profile

The configuration file is like below.

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

As the last step in order to make $PATH change effective for the current terminal run the following command.

$ source ~/.profile

Install Go From Apt Repository

The Go or Golang compilers and binaries are also provided via official repositories of the popular Linux distributions. Ubuntu, Debian, Mint, Kali, etc. provide the Go programming language binaries via the apt package manager and repositories.

See also  Copy Files and Directories Recursively with SCP

The package name for the Go is golang-go and can be installed with the following “apt install” command.

$ apt install golang-go

Check Go Installation

We have already installed the Go and check if it is installed properly and running by using the go command like below. The go command is used to compile Go source code.

$ go
Check Go Installation

Check Install Go Version

Go is a dynamic programming language where a lot of new features and modules are added with new versions and releases. We can check and display the Go version with the version option like below.

$ go version
go version go1.17.5 linux/amd64

Run Sample Go Hello World App

In this part, we will create a sample Hello Worl application with Go or GoLang. then we will compile and run this sample application.

// Hello World Sample App
package main
 
// Import OS and fmt packages
import ( 
	"fmt" 
	"os" 
)
 
// Main method
func main() {

    fmt.Println("Hello, world!")  // Print simple text on screen


}

With the go run command the Go application is compiled and executed with a single command.

$ go run hello_world.go

Alternative if we only need to build the Go application but do not want to run the go build can be used.

$ go build hello_world.go

Leave a Comment