How to Install Go on Ubuntu 16.04
Posted on September 16, 2017 • 3 min read • 611 wordsIn this tutorial we'll learn how-to install Go programming language on Ubuntu 16.04 Xenial Xerus.
Go or Golang is a relatively new programming language originally developed by Google that gaining a lot of popularity for the last few years.
Kubernetes and Docker is sample of application written in Go. You can see list of application and companies using Go in GoUsers page.
You can also visit golang website to learn more about Golang.
In this tutorial we’ll learn how-to install Go programming language on Ubuntu 16.04 Xenial Xerus.
We’ll also learn creating Hello World Golang application, compile and run the application.
Ubuntu 16.04 shipped with Golang 1.6. At the time of this writing the latest version of Go is 1.9 so this is a little bit behind, but if you’re ok with this we can install this using apt-get command
Let’s update our apt database first.
sudo apt-get update
```bash
Then install golang
```bash
sudo apt-get install golang
Now let’s check go version installed
$ go version
go version go1.6.2 linux/amd64
```bash
## Install Go 1.9
Since Go from Ubuntu repository is not up to date we can install latest stable version of Go by downloading binary from [Golang Download page][5].
### Download Go Binary
Download Go 1.9 package using wget
```bash
wget -c https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz
If you want to use curl
you can use command below.
curl -C - https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz -o go1.9.linux-amd64.tar.gz
```bash
### Check downloaded file
Let's check downloaded file using `sha256sum` command. Compare the hash output with hash on [Golang download page][5].
```bash
$ sha256sum go1.9.linux-amd64.tar.gz
d70eadefce8e160638a9a6db97f7192d8463069ab33138893ad3bf31b0650a79 go1.9.linux-amd64.tar.gz
If the hash is not the same you need to redownload the package since the package might get corrupted on the way from server to your computer.
Extract downloaded file above using tar
command.
tar xzf go1.9.linux-amd64.tar.gz
```bash
This process will create new directory named `go`.
### Install Go in default location
The go binary distributions assume they will be installed in `/usr/local/go` directory. Let's move the `go` directory to `/usr/local` and change the ownership of the directory to `root` user.
```bash
sudo mv go /usr/local/
Change ownership to root
user and group.
sudo chown -R root:root /usr/local/go
```bash
### Add go in PATH environment variable
There are multiple ways to add golang to our path, in this tutorial we'll create new file `/etc/profile.d/go.sh` with contents below.
```bash
PATH=$PATH:/usr/local/go/bin
You can use command below to create and add contents to go.sh file.
echo "PATH=\$PATH:/usr/local/go/bin" | sudo tee /etc/profile.d/go.sh
```bash
Why we are using this approach? because all users on the system will have `go` on their path and cleaner and easier to track compared to changing path on `/etc/profile` file.
## Hello Go from HowtoDojo
Let's create new file called `hello.go`
```go
package main
import "fmt"
func main() {
fmt.Printf("Hello from HowtoDojo\n")
}
Compile the source code above using build
options
$ go build hello.go
```bash
The build process above will create new file named `hello`. When we check the file type of this file we will get the file type is Linux ELF binary file.
```bash
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
To run the compiled file we can use command below
$ ./hello
```bash
If you don't want to compile the go code you can use `run` options
```bash
$ go run hello.go
This will compile our source code and directly run the application. No binary will be created from this command.
In this tutorial we learned how-to install Go or Golang from Ubuntu repository.
We also learn to install latest version of Go by downloading binary from Go website.
We create our first Go application, compile and run the application. Have fun with Go!