Install Golang Ubuntu1804
Published: 2019-08-22
Intro
This will just be a quick post on how to install Golang on Ubuntu 1804. There will be no earth shattering knowledge bombs, it's more of a documentation post for myself. Others may or may not find it useful.
Code versions used for this lab
- Ubuntu - 1804
- Golang - 1.15.1
Download
Download the Golang package for your system and architecture.
curl -OL https://dl.google.com/go/go1.15.1.linux-amd64.tar.gzInstall
Once downloaded, extract the tar file to install Golang.
sudo tar -C /usr/local -xzf go1.15.1.linux-amd64.tar.gzThis will create a binary here: /usr/local/go/bin/go
Configure
In order to effectively utilise go you will need to setup a couple of environment variables. The first one adds the the go binary to your $PATH
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.zshrcThe next one defines the root directory for your $GOPATH. Your $GOPATH is the location of your Go workspace. All of your go projects will get built under this workspace.
echo 'export GOPATH=$HOME/go' >> ~/.zshrcFinally source your rc file to load the variables into your environment and create the $GOPATH workspace directory.
source ~/.zshrc
mkdir $GOPATHVerify Install
Confirm that Go is installed correctly.
go version
# output
go version go1.12.9 linux/amd64First Go Program
Create a project directory under your $GOPATH workspace.
cd $GOPATH
mkdir hello && cd helloCreate a hello.go file.
// hello.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}Compile the project into a binary.
go buildA binary hello will be created, execute the binary and bask in your glory.
./hello
# output
hello, worldOutro
That's it. Go installed, configured and ready to be stretched to its limits.