How to Install Go on Ubuntu 16.04
Categories:
Overview
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][1] page.
You can also visit [golang website][2] to learn more about Golang.
Objective
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.
Install Go From Ubuntu Repository
Ubuntu 16.04 shipped with [Golang 1.6][3]. 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
Then install golang
$ sudo apt-get install golang
[][4]
Now let’s check go version installed
$ go version
go version go1.6.2 linux/amd64
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
$ 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
Check downloaded file
Let’s check downloaded file using sha256sum
command. Compare the hash output with hash on [Golang download page][5].
$ 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 Go Archive
Extract downloaded file above using tar
command.
$ tar xzf go1.9.linux-amd64.tar.gz
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.
$ sudo mv go /usr/local/
Change ownership to root
user and group.
$ sudo chown -R root:root /usr/local/go
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.
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
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
package main
import "fmt"
func main() {
fmt.Printf("Hello from HowtoDojo\n")
}
```
Compile the source code above using `build` options
```
$ go build hello.go
```
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.
```
$ 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
```
If you don't want to compile the go code you can use `run` options
```
$ go run hello.go
```
This will compile our source code and directly run the application. No binary will be created from this command.
## Summary
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!
[1]: https://github.com/golang/go/wiki/GoUsers
[2]: https://golang.org/
[3]: https://packages.ubuntu.com/xenial/golang
[4]: https://www.howtodojo.com/wp-content/uploads/2017/09/install-golang-ubuntu-16.04.png
[5]: https://golang.org/dl/