Skip to main content

Install Golang

First of all install some required packages

sudo apt-get install -y curl git jq make unzip

You now have two methods for installing Golang.

Ubuntu

To install Go on Ubuntu, you can easily follow the steps provided.

Add the repository

sudo add-apt-repository ppa:longsleep/golang-backports

Install Golang

sudo apt-get install -y golang-go

Manually

This method should work on all Linux systems, although it is intended for more experienced users.

Get a copy of the last Golang version and unpack it

cd ~ && \
curl -OL https://golang.org/dl/goX.X.X.linux-amd64.tar.gz && \
tar -C ${HOME} -xvf goX.X.X.linux-amd64.tar.gz

Copy the extracted go into /usr/local/lib/go

sudo cp -r ${HOME}/go /usr/local/lib/go

Export Golang environment variables

Now that you've successfully installed Golang, it's essential to configure its environmental variables.

Open the .bashrc file

nano ${HOME}/.bashrc

Based on your installation type, set the GOROOT environment variable by choosing one of the following lines

# For Ubuntu installation
export GOROOT=/usr/lib/go

# For Manual installation
export GOROOT=/usr/local/lib/go

After setting the appropriate GOROOT environment variable, you can add the following lines

${HOME}/.bashrc
export GOPATH=${HOME}/go
export GOBIN=${GOPATH}/bin
export PATH=${PATH}:${GOROOT}/bin:${GOBIN}

Source the file to reflect changes in the current Terminal session.

source ${HOME}/.bashrc