Install Go Programming Language on Ubuntu Linux

Golang, also known as Go is an Open source cross platform programming language developed by google. Very powerful but simple, Go support all the major operating systems. Go Programming language run on Windows, Linux, macOS, FreeBSD and many other Unix operating systems.

Setting up your development environment correctly is very important. In this tutorial I will explain how to install Go on Ubuntu Linux and set up your Development Environment for the Golang.

To install the Go compiler on Ubuntu, Do the following steps:

  1. Go to following URL golang.org/dl and download the Linux tar file for Ubuntu.

  2. After the download is finished, extract Linux tar file to the /usr/local directory.

    sudo tar -zxvf go1.9.3.linux-amd64.tar.gz -C /usr/local
  3. Next, we need to add the bin directory of the /usr/local/go to the PATH Variable. First, Open the Ubuntu /etc/bash.bashrc file:

    sudo gedit /etc/bash.bashrc
  4. Then, Add the following line at the end of the file.

    export PATH=$PATH:/usr/local/go/bin
  5. Save and source the /etc/bash.bashrc file to apply the new changes:

    source /etc/bash.bashrc

From the command line run the go version command to check the installation.

go version
go version on ubuntu

You should get the version information as shown in the above screenshot. And Congratulations, you have successfully installed Golang on Ubuntu.

Install Golang to a custom directory

By default Go binary distributions expect you will install Golang in /usr/local directory as we did above. But if you installed on different directory, then you need to configure GOROOT environment variable pointing to the custom directory.

For example, if you installed Golang to /opt directory, you should add the GOROOT variable to the /etc/bash.bashrc file.

export GOROOT=/opt/go
export PATH=$PATH:$GOROOT/bin

Again, Run the version command from the Ubuntu terminal and make sure everything is OK.

Writing Your First Go Program: Hello World

After installing the Golang successfully on your Ubuntu machine, you are now ready to write and execute the famous "Hello World" program on Ubuntu.

Create a file called helloworld.go and add the following code:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

Then, compile and run the program using go run command from the Ubuntu terminal.

go run helloworld.go

If everything is right, you should see the message Hello, World! output on your screen.

Configure GO Workspace Environment (GOPATH)

Most programming languages use a separate folder for each project. But, Golang use one location for all projects. Ensure that you have properly set up your GOPATH. This is a local directory that contains Go source files, package objects and command binaries that compiler produced from the source files.

To set up our workspace, we need to set the GOPATH environment variable which specifies the location of our workspace. You can use any folder you like.

Say you want to use a folder called "go" inside your home directory. First, create the folder, then open the .bashrc file:

gedit ~/.bashrc

Add the following line at the end of the bashrc file:

export GOPATH=$HOME/go

Reload the bashrc file:

source ~/.bashrc

Inside the workspace folder, you need to create three subdirectories src, pkg and bin.

  • The src folder contains Go source files, which are organized in packages, with one subdirectory in the src directory representing one package.
  • pkg contains package objects.
  • bin contains executable binary files.
Configure GO Workspace Environment

For convenience you should also add your workspace's bin subdirectory to your PATH variable. Doing so will allow you to execute the binaries of your compiled Go code from anywhere in the Ubuntu terminal.

In order to add the bin subdirectory to Ubuntu PATH variable, append following line to the .bashrc file.

export PATH=$PATH:$GOPATH/bin

Then, reload the .bashrc file:

source ~/.bashrc

Integrated Development Environment - IDE

There are multiple IDE and text editors you can use for Go programming, But LiteIDE and Intellij Gogland are the two editors specialized for the Go programming. Both LiteIDE and Gogland can install on Ubuntu.

The most popular editors and IDE's for Ubuntu include the following:

  • Sublime Text 3.
  • Visual Studio Code.
  • Atom Text editor.
  • Eclipse.
  • GNU Emacs.
  • VIM.

Summary

In this tutorial, we installed Google Go programming language on Ubuntu Linux. Then, We Configure Go Workspace Environment with GOPATH environment variable.