Any Go Installation needs two basic things: the binaries of the language somewhere on your disk and a GOPATH path in your system where your projects and the projects that you download from other people will be stored.
In the following lines, we will explore how to install Go binaries in Linux, Windows and OS X. For a detailed explanation of how to install the latest version of Go, you can refer to the official documentation at https://golang.org/doc/install.
To install Go in Linux you have two options:
Easy option: Use your distribution package manager:
RHEL/Fedora/Centos users with YUM/DNF:
sudo yum install -y golang
Ubuntu/Debian users using APT with:
sudo apt-get install -y golang
Advanced: Downloading the latest distribution from https://golang.org.
I recommend using the second and downloading a distribution. Go's updates maintains backward compatibility and you usually should not be worried about updating your Go binaries frequently.
The advanced installation of Go in Linux requires you to download the binaries from golang webpage. After entering https://golang.org , click the Download Go button (usually at the right) some Featured Downloads option is available for each distribution. Select Linux distribution to download the latest stable version.
At https://golang.org you can also download beta versions of the language.
Let's say we have saved the
tar.gz file in Downloads folder so let's extract it and move it to a different path. By convention, Go binaries are usually placed in
tar -zxvf go*.*.*.linux-amd64.tar.gz sudo mv go /usr/local/go
On extraction remember to replace asterisks (
*) with the version you have downloaded.
Now we have our Go installation in
/usr/local/go path so now we have to add the
bin subfolder to our
PATH and the
bin folder within our GOPATH.
mkdir -p $HOME/go/bin
With -p we are telling bash to create all directories that are necessary. Now we need to append bin folder paths to our PATH, append the following lines at the end of your
Check that our
go/bin directory is available:
$ go version Go version go1.6.2 linux/amd64
To install Go in Windows, you will need administrator privileges. Open your favorite browser and navigate to
. Once there click the Download Go button and select Microsoft Windows distribution. A
*.msi file will start downloading.
Execute the MSI installer by double clicking it. An installer will appear asking you to accept the End User License Agreement (EULA) and select a target folder for your installation. We will continue with the default path that in my case was
Once the installation is finished you will have to add the binary Go folder, located in
C:\Go\bin to your Path. For this, you must go to Control Panel and select System option. Once in System, select the Advanced tab and click the Environment variables button. Here you'll find a window with variables for your current user and system variables. In system variables, you'll find the Path variable. Click it and click the Edit
button to open a text box. You can add your path by adding
;C:\Go/bin at the end of the current line (note the semicolon at the beginning of the path). In recent Windows versions (Windows 10) you will have a manager to add variables easily.
In Mac OS X the installation process is very similar to Linux. Open your favorite browser and navigate to
and click the Download Go. From the list of possible distributions that appear, select Apple OS X. This will download a
*.pkg file to your download folder.
A window will guide you through the installation process where you have to type your administrator password so that it can put Go binary files in
/usr/local/go/bin folder with the proper permissions. Now, open Terminal to test the installation by typing this on it:
$ go version Go version go1.6.2 darwin/amd64
If you see the installed version, everything was fine. If it doesn't work check that you have followed correctly every step or refer to the documentation at https://golang.org.
Go will always work under the same workspace. This helps the compiler to find packages and libraries that you could be using. This workspace is commonly called GOPATH.
GOPATH has a very important role in your working environment while developing Go software. When you import a library in your code it will search for this library in your
$GOPATH/src. The same when you install some Go apps, binaries will be stored in
At the same, all your source code must be stored in a valid route within
$GOPATH/src folder. For example, I store my projects in GitHub and my username is Sayden so, for a project called minimal-mesos-go-framework I will have this folder structure like
$GOPATH/src/github.com/sayden/minimal-mesos-go-framework which reflects the URI where this repo is stored at GitHub:
mkdir -p $HOME/go
$HOME/go path is going to be the destination of our
$GOPATH. We have to set an environment variable with our
$GOPATH pointing to this folder. To set the environment variable, open again the file
$HOME/.bashrc with your favorite text editor and add the following line at the end of it:
Save the file and open a new terminal. To check that everything is working, just write an echo to the
$GOPATH variable like this:
echo $GOPATH /home/mcastro/go
If the output of the preceding command points to your chosen Go path, everything is correct and you can continue to write your first program.