Go, or Golang is an open-source programming language that Google created. It’s statically typed and produces compiled machine code binaries, and go language is a compiled language. This is popular amongst developers as it means you do not need to compile the source code to create an executable file. Developers that use Google’s Go language say it is the C for the twenty-first century when it comes to syntax.
In the following tutorial, you will learn how to install and configure Go (Golang) on your Ubuntu 20.04 LTS Focal Fossa using the command line terminal with the default repository version of Ubuntu or manually downloading and installing the latest version available from the official Golang source.
Table of Contents
Update Ubuntu
Before proceeding, ensure your system is up-to-date
sudo apt update && sudo apt upgrade -y
Install Go (Golang) – APT Method with Ubuntu
The first and easiest method is to install Golang using the APT repository from Ubuntu 20.04; this can be achieved using the following command.
sudo apt install golang -y
The above command installs Golang v1.13, and most users would prefer a much newer version of Golang that is constantly updated when new releases hit. The best way to achieve this is to install the PPA.
First, import the PPA.
sudo add-apt-repository ppa:longsleep/golang-backports -y
Next, run a quick APT update to reflect the newly added PPA.
sudo apt update
Install Golang; run this command even if you installed an older version.
sudo apt install golang -y
Next, to finish off, check the software version to see what you are on and if it is recent enough for your requirements:
go version
Example output:
Install Go (Golang) – Manual Method
The second method which may suit you better if you are after specific or more recent versions of Golang is to use the manual method as detailed in the following steps.
First, download the latest version of Go from their official website using the following command.
Example command only:
wget https://golang.org/dl/go1.8.1.linux-amd64.tar.gz
Note, at the time of this guide, version 1.8.1 is the latest, but this will change in time. Ensure you visit the download page to check version numbers and replace the URL with an updated one.
Once the Go is downloaded, extract the downloaded file to the /usr/local/directory.
sudo tar -xzf go1.*.*.linux-amd64.tar.gz -C /usr/local/
Now, you will need to add the path of the Go directory to the $PATH variable in the /etc/profile.
sudo nano /etc/profile
Once the file is open, add the following line.
export PATH=$PATH:/usr/local/go/bin
Once complete, (CTRL+O) to save and (CTRL+X) exit.
Next, activate the PATH environment variable that you just added.
source /etc/profile
Verify the installation by checking the version with the following command.
go version
Example output:
Create a Golang Test Program – Verify Installation
Now that you have installed Go, we will create a small program that will print (hello world).
First, create a directory using the following command.
mkdir go-hello
Now, you will create a (.go) file. The quickest way to do this is using the nano editor as follows.
nano go-hello/hello.go
Next, add the following text below to create the (Hello World) Go program.
package main
import "fmt"
func main() {
fmt.Printf("Hello, World from LinuxCapable.com\n")
}
Save the file (CTRL+O), then exit (CTRL+X).
Now, you need to build (go.mod) file so you can execute the Go file you just created!
sudo nano go-hello/go.mod
Add the following line.
module example.com/mod
Save the file (CTRL+O), then exit (CTRL+X).
Next, cd to the directory, then build the program by entering the following command.
cd go-hello && go build
Finally, execute the (Hello World) program by entering the following command.
./mod
Example output:
Comments and Conclusion
In the tutorial, you have learned how to successfully install Go (Golang) using Ubuntu’s default repositories or direct from the source (Recommended) and successfully create your first Go program.
For more information on using and developing with Go, visit the official documentation.