mdawar.dev

A blog about programming, Web development, Open Source, Linux and DevOps.

Install Go on Linux

(Updated: )

1. Download the tar archive

bash
$# Set the Go version to download
$export GO_VERSION=X.Y.Z # Replace X.Y.Z with the version number
$# Set the processor architecture (amd64, arm64 or 386)
$export ARCH=amd64
$# Download the tar archive
$wget https://go.dev/dl/go${GO_VERSION}.linux-${ARCH}.tar.gz

Or go to the Go Downloads page and download the Linux tar archive .tar.gz.

2. Extract the archive

bash
$# First remove any previous installation if it exists
$sudo rm -rf /usr/local/go
$# Then extract the archive
$sudo tar -C /usr/local -xzf go${GO_VERSION}.linux-${ARCH}.tar.gz
$# You can check installation directory
$ls /usr/local/go

3. Make the Go binaries available globally

To make the Go binaries available globally on your system, you need to add their directories to your PATH environment variable.

Their are 2 directories that you might want to add to your PATH:

  1. /usr/local/go/bin: Go installation binaries
  2. ~/go/bin: Binaries of third party tools that you install via go install

Use any of your ~/.profile, ~/.bash_profil e or ~/.bashrc files to add these directories to the PATH variable:

bash
# Go installation binaries
export PATH="${PATH}:/usr/local/go/bin"
# Third party tools installed via `go install`
export PATH="${PATH}:${HOME}/go/bin"

You can also add both directories on the same line:

bash
export PATH="${PATH}:/usr/local/go/bin:${HOME}/go/bin"

This change will be applied when you open a new terminal window or on you next login.

To apply this change immediately, execute the file that you have changed, for example:

bash
$source ~/.profile

4. Verify the installation

You should be able to run go from any location:

bash
$go version
go version goX.Y.Z linux/amd64

Update the Go Version

If you want to update the Go version, follow the same installation steps:

bash
$# Set the Go version to download
$export GO_VERSION=X.Y.Z # Replace X.Y.Z with the version number
$# Set the processor architecture (amd64, arm64 or 386)
$export ARCH=amd64
$# Remove the previous Go installation directory
$sudo rm -rf /usr/local/go
$# Download the new tar archive
$wget https://go.dev/dl/go${GO_VERSION}.linux-${ARCH}.tar.gz
$# Then extract the archive to /usr/local
$sudo tar -C /usr/local -xzf go${GO_VERSION}.linux-${ARCH}.tar.gz
$# Verify the installation
$go version

Uninstall Go

To uninstall Go we need to undo what we did previously:

bash
$# Remove the Go directory
$sudo rm -rf /usr/local/go
$# Remove the thrid party tools installed via `go install` if any
$sudo rm -rf ~/go

If you have installed any third party tools using go install and you want to remove their source files and compiled binaries:

bash
$# Remove the installed thrid party tools
$sudo rm -rf ~/go

Then remove the Go bin directories from your PATH environment variable.