How to Install Go 1.9 on Ubuntu 16.04 / Debian 9 / CentOS 7 / Fedora 27
Go Lang is an open source programming language developed by a team of Google engineers in 2007. Go language was designed to resolve the common criticisms of other languages while maintaining their positive characteristics and most widely used for writing servers these days.
Most notable projects using Go language are,
- Docker, a set of tools for deploying containers
- Juju, a service orchestration tool by canonical
- Dropbox moved the critical components to Go lang from python
- snappy, a package manager
System Requirements
Go binary packages are available for below supported operating systems. Please ensure your system meets these requirements before installing Go language.
| Operating system | Architectures | Notes |
|---|---|---|
| FreeBSD 8-STABLE or later | x86_64 | Debian GNU/kFreeBSD not supported |
| Linux 2.6.23 or later with glibc | x86_64, arm | CentOS/RHEL 5 not supported |
| Mac OS X 10.7 or later | x86_64 | use the clang or gcc† that comes with Xcode‡ for cgo support |
| Windows XP or later | x86_64, 386 | use MinGW gcc†. No need for Cygwin or msys. |
This guide will help you to do the installation of Go 1.9 Lang on Ubuntu 16.04, Ubuntu 14.04, CentOS 7, RHEL 7, and Fedora 27.
Download and Install Go Language
Before installing Go language, update your system with the latest security patches to ensure the system is not vulnerable.
Switch to the root user.
$ su -
Update your system using the following command.
### Debian 9 / Ubuntu 16.04 / 14.04 ### # apt-get update ### CentOS / RHEL / Fedora ### # yum -y update
Install wget package.
### Debian 9 / Ubuntu 16.04 / 14.04 ### # apt-get install wget ### CentOS / RHEL / Fedora ### # yum -y install wget
Download the Go language binary package using the following command. Alternatively, you can visit Go Language download page to download the latest version of Go.
# wget https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz
Extract the archive and move it to /usr/local. You can also place the Go lang binary to your custom location instead of /usr/local.
# tar -zxvf go1.9.2.linux-amd64.tar.gz -C /usr/local/
Setup Go Environment variables
Now you would need to setup two important variables for Go, GOROOT (PATH) and GOPATH. Add /usr/local/go/bin to your path variable. You can add this by running below command (temporary) or place the following command in /etc/profile or $HOME/.profile file for persistent across sessions.
export PATH=$PATH:/usr/local/go/bin
If you have installed Go on custom location replace /usr/local/go/bin/ with /path/to/bin/ directory.
GOPATH is a Go environment variable for your project workspace. Let’s create a workspace directory called “work” in your home directory.
mkdir $HOME/work
Set GOPATH variable. You can add this by running below command (temporary) or place the following command in /etc/profile or $HOME/.profile file for persistent across sessions.
export GOPATH=$HOME/work
Verify Go Installation
Run the below command to see the version of Go language.
# go version go version go1.9.2 linux/amd64
Check the Go environment variables we set in previous sections.
# go env GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/root/work" GORACE="" GOROOT="/usr/local/go" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0" CXX="g++" CGO_ENABLED="1" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config"
Create your first project
Assuming that you have workspace called “work” located in the $HOME directory. I am a root user, in my case “/root/work” is my workspace. Go to your workspace.
# cd $GOPATH or # cd $HOME/work
Create a directory “src/hello” under your workspace.
# mkdir -p src/hello
Let’s create a simple program (hello) to test the Go installation.
# vi src/hello/hello.go
Place the following content in the hello.go file.
package main
import "fmt"
func main() {
fmt.Printf("Welcome To ITzGeek\n")
}
Now, compile it with go command.
# go install hello
go command will put an executable command (hello) inside the bin directory of your workspace. Execute the executable using the following command.
# $GOPATH/bin/hello
You should get the following greeting text.
Welcome To ITzGeek
That’s all. You have successfully installed Go.