How to install Go (compiler and tools) on Linux

Installing Go from standard system repositories in Debian, Kali Linux, Linux Mint, Ubuntu

To install, run the command:

sudo apt install golang

For Debian it is also recommended to add the export of the following environment variables in ~/.bashrc (or ~/.zshrc if you run ZSH):

export GOPATH=/home/$USER/go
export PATH=${PATH}:$GOROOT/bin:/home/$USER/go/bin

These changes will take effect after a reboot. Instead of restarting your computer, run:

source ~/.bashrc # OR source ~/.zshrc

If you are not sure which shell you have, then run the command:

echo $SHELL

If it displays:

  • /bin/bash – means you have Bash
  • /usr/bin/zsh – means you have ZSH

Installing Go from the standard system repositories on Arch Linux, BlackArch and their derivatives

On Arch Linux and derivatives, Go is installed as follows:

sudo pacman -S go

For Arch Linux it is also recommended to add the export of the following environment variables in ~/.bashrc (or ~/.zshrc):

export GOPATH=/home/$USER/go
export PATH=${PATH}:$GOROOT/bin:/home/$USER/go/bin

These changes will take effect after a reboot. Instead of restarting your computer, run:

source ~/.bashrc # OR source ~/.zshrc

Installing the most recent version of the Go compiler manually

Previously, manual installation was recommended as it involves adding environment variables. Environment variables are used, for example, in installation commands given by software developers:

go get github.com/Ullaakut/cameradar
cd $GOPATH/src/github.com/Ullaakut/cameradar/cmd/cameradar
go install

Without environment variables, this command sequence will fail.

However, these environment variables can be added when installing Go from the repository, as shown above. Therefore, if you did this (added export of environment variables), then manual installation has no advantages unless you need the latest version of the Go language.

If Go is missing from the application sources for your distribution, or if you want to manually install the most recent compiler, then follow this tutorial further.

The following instruction has been successfully tested on Kali Linux, Arch Linux, BlackArch, Linux Mint, Ubuntu. It should also work on almost any Linux distribution.

If you are not sure which shell you have, then run the command:

echo $SHELL

If it displays:

  • /bin/bash – means you have Bash
  • /usr/bin/zsh – means you have ZSH

If you run a Bash shell (most systems), then follow the instructions in this section:

Open the .bashrc file in the user directory with any text editor:

gedit ~/.bashrc

And to create new environment variables add the following lines to this file:

export GOPATH=/home/$USER/go
export GOROOT=/usr/local/src/go
export PATH=${PATH}:$GOROOT/bin:/home/$USER/go/bin

When you're done, save your changes and close the file.

These changes will take effect after a reboot. Instead of restarting your computer, run:

source ~/.bashrc

If you run a ZSH shell (by default, for example, in Kali Linux), then follow the instructions in this section:

Open the ~/.zshrc file in the user directory with any text editor:

gedit ~/.zshrc

And to create new environment variables add the following lines to this file:

export GOPATH=/home/$USER/go
export GOROOT=/usr/local/src/go
export PATH=${PATH}:$GOROOT/bin:/home/$USER/go/bin

When you're done, save your changes and close the file.

These changes will take effect after a reboot. Instead of restarting your computer, run:

source ~/.zshrc

Further it is the same for all shells and systems.

The following command will automatically detect and download the latest version of the Go programming language files:

wget https://golang.org/`curl -s https://golang.org/dl/ | grep -E -o 'dl/go[0-9.]{3,}linux-amd64.tar.gz' | head -n 1`

Extract the downloaded archive:

tar zxf go*.linux-amd64.tar.gz

Move to the directory to $GOROOT that we specified in ~/.bashrc.

sudo mv go $GOROOT

Now type in the terminal:

go

Help should appear:

Recommended for you:

One Comment to How to install Go (compiler and tools) on Linux

  1. Alex says:

    The instruction is corrected – the manual (recommended) installation works again. Added instructions if you are using ZSH shell.

Leave a Reply

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