Different Linux distributions are available in the market like Ubuntu, CentOS, and RHEL. we will try to learn a single installation process that helps you install Golang on a Linux Operating System.

Find the Architecture of the Linux machine

Every machine has a different CPU so you need to know the architecture used on your machine. Use the below command to find the machine architecture

arch

The architecture of my machine is x86_64 which means it is

  • 64-bit machine
  • This is mostly used in home computers for Intel and AMD processors.

Download Golang Binary for Linux

Visit the official website of Golang to find their releases

https://go.dev/dl/

You will see the different stable versions here. You may see a different stable version number when you open the official website of Golang. In our case, we will be installing the go1.22.4 version

You need to select the appropriate File Name by choosing the correct OS and Arch that was found in the previous step. In our case

  • OS is Linux
  • Arch is x86_64

Right-click on the File Name copy the link and download it to the folder where you want to install the Golang. we are installing it in our home folder

cd ~
mkdir golang
cd golang

Download the Golang tar using the wget command

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

Untar the file

tar -xvzf go1.22.4.linux-amd64.tar.gz
untar the golang downloaded package

Go to the bin folder and you will find the go binary

cd go/bin
go binary in the bin folder

Define GOROOT for Golang

The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but installing the Go tools in a different location is possible. In this case, you must set the GOROOT environment variable to point to the directory in which it was installed. we have downloaded Goland to a different folder so we need to update the GOROOT PATH

Go to the folder where we downloaded Golang. You can get the absolute path using the pwd (present working directory) command

pwd
set the GOROOT path for Golang

The path in your machine will be different and you need to use the path of your machine. The absolute path for my machine installation is /home/selftuts/golang/go

You need to update .bashrc or .zshrc based on the bash terminal that you are using and put the below commands

export GOROOT=/home/selftuts/golang/go
export PATH=$PATH:$GOROOT/bin

Open a new terminal and then access the go command from anywhere and you will see that go binary is working.

go
run go command on linux

Define the GOPATH

The GOPATH environment variable lists places to look for Go code. On Unix, the value is a colon-separated string and on Windows, the value is a semicolon-separated string. On Plan 9, the value is a list. GOPATH must be set to get, build, and install packages outside the standard Go tree.

In our case, we are using the directory /home/selftuts/workspace/go

Find the absolute path of the directory where you want to create the GOPATH and update either .bashrc or .zshrc for the GOPATH variable.

export GOPATH=/home/selftuts/workspace/go

In this way, you can install Golang on your Linux machine

References

Leave a Reply

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