Installing GO or rather Golang can be straightforward following the GO Installation Documentation, however, after installation one realises they encounter a compilation error when running Golang in Ubuntu or they need to change the default GO directory.
So this article will highlight:
Installing GO in Windows, Mac and in Ubuntu
Changing the default GO home directory to your own custom directory
Resolving compilation error when you run you Golang in Ubuntu
The current version of Golang by when this article is being written is Go 1.17
Note that:
The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named go inside your home directory ($HOME/go).
It is important to change the default GOPATH directory from $HOME/go when making the Golang installation to avoid losing you files incase of a new installation since the default directory gets rewritten in a new installation.
Install in Windows 10
To install Golang in windows, follow the official instructions on how to install Go whereby:
b) run the installer
c) follow the prompts on the installer:
d) To change the installation directory, click the change button
e) Navigate to your preferred directory
f) After following all the prompts, the installation will be done then at last close the installer by pressing the finish button.
g) Confirm the installation in the Command Prompt:
open Command Prompt by typing
cmd
in the searchthen run this command:
> go version
go version go1.17.6 windows/amd64
Install in Mac
You can follow the official instructions on how to install Go, or you can use Homebrew instead:
brew install go
When installed, run go version to see the installed version of Go.
$ go version
go version go1.17.3 darwin/amd64
Setup your workspace
Incase your custom directory is ~/go-workspace
mkdir ~/go-workspace
mkdir ~/go-workspace/bin ~/go-workspace/bin/src ~/go-workspace/pkg
To change your GOPATH to something else add GOPATH to your ~/.zprofile.
Open the ~/.zprofile file
nano ~/.zprofile
Copy this to your ~/.zprofile
export GOPATH=$HOME/go-workspace
export GOROOT=/usr/local/go
export PATH=$PATH:$GOPATH/bin
Add GOPATH/bin directory to your PATH environment variable so you can run Go programs anywhere.
To effect the changes, source ~/.zprofile
source ~/.zprofile
Install in Linux
These are the steps to install Golang in Linux:
a). Dowload Golang:
Run:
$ wget https://go.dev/dl/go1.17.6.linux-amd64.tar.gz
b). Extract the archive you downloaded into /usr/local.
In the directory you've downloaded the Golang archive run:
sudo rm -rf /usr/local/go && tar -C /usr/local -xzf go1.17.6.linux-amd64.tar.gz
d). Setup your workspace
To change your GOPATH to something else add GOPATH to your .bashrc.
mkdir ~/go-workspace
mkdir ~/go-workspace/bin ~/go-workspace/bin/src ~/go-workspace/pkg
echo "GOPATH=$HOME/go-workspace" >> ~/.bashrc
echo "export GOPATH" >> ~/.bashrc
echo "PATH=$PATH:\$GOPATH/bin # Add GOPATH/bin to PATH for scripting" >> ~/.bashrc
To effect the changes, source ~/.bashrc
source ~/.bashrc
Verify that you've installed Go:
$ go version
Confirm that the command prints the installed version of Go.
go version go1.17.6 linux/amd64
Resolving compilation error when you run you Golang in Ubuntu
When you run a Golang program and encounter this error:
exec: "gcc": executable file not found in %PATH%
Run this to install the gcc/g++ compilers and libraries:
apt-get install build-essential