Install GoLang in Linux : Setting up Dev Environment

Go Lang development environment can be brought up on Linux, Windows and Macs. In this article, I will be using rocky linux setup on virtual box. We can setup the the environment in /usr/local/go  or in a custom path. This is required if we want to setup different go versions in the same dev environment

1. Installation on path /usr/local/go

1.1 Download & Extract the source archive

  • At the time, this article is written, 1.18.4 is the latest go version. Visit the official go download page for the latest version. We can use wget to download the archive
[root@Linux-Data-Hub ~]# wget https://go.dev/dl/go1.18.4.linux-amd64.tar.gz
--2022-07-18 09:01:15-- https://go.dev/dl/go1.18.4.linux-amd64.tar.gz
Resolving go.dev (go.dev)... 216.239.34.21, 216.239.36.21, 216.239.38.21, ...
Connecting to go.dev (go.dev)|216.239.34.21|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://dl.google.com/go/go1.18.4.linux-amd64.tar.gz [following]
--2022-07-18 09:01:16-- https://dl.google.com/go/go1.18.4.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 142.250.195.238, 2404:6800:4007:828::200e
Connecting to dl.google.com (dl.google.com)|142.250.195.238|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 141812725 (135M) [application/x-gzip]
Saving to: ‘go1.18.4.linux-amd64.tar.gz’

go1.18.4.linux-amd64.tar.gz 100%[===============================================================================================================>] 135.24M 5.63MB/s in 23s

(5.77 MB/s) - ‘go1.18.4.linux-amd64.tar.gz’ saved [141812725/141812725]
  • Extract the archive to /usr/local, using below command
tar -C /usr/local -xzf go1.18.4.linux-amd64.tar.gz

1.2 Update environment variable

  • Add /usr/local/go/bin to the PATH environment variable
[root@Linux-Data-Hub ~]# echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile
[root@Linux-Data-Hub ~]# source /etc/profile

1.3 Verify go version

Verify installation of go, by checking the version. If the installation and environment variable is set properly, go version will be seen using below command

[root@Linux-Data-Hub ~]# go version
go version go1.18.4 linux/amd64

2. Installation of GoLang on custom path

If you want to keep multiple version of go in your dev environment. You can install go in custom path, following below steps

2.1 Download the source archive

[root@LDH ~]# wget "https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz"

2.2 Extract the archive to custom path

In my case Im creating a custom path (/custom/dir) for installing go

mkdir -p  /custom/dir
#Creating src directory
mkdir /custom/dir/src
#Navigate to src and extract the archive
cd /custom/dir/src; tar -xvf go1.4.2.linux-amd64.tar.gz

2.3 Update environment variable

We need to set the PATH, GOROOT, GOPATH

bin]# echo "export PATH=$PATH:/custom/dir/src/go/bin" >> /etc/profile
bin]# echo "export GOROOT=/custom/dir/src/go"  >> /etc/profile
bin]# mkdir -p /custom/dir/src/GoPackages
bin]# echo "export GOPATH=/custom/dir/src/GoPackages" >> /etc/profile

2.4 Verify go version

Verify installation of go, by checking the version.

[root@LDH ~]# go version
go version go1.4.2 linux/amd64

Search on LinuxDataHub

Leave a Comment