Install latest Golang on Ubuntu

less than 1 minute read

You can install the latest version of golang and set environment variables on your Ubuntu machine with this snippet.

Test Environment

  • Ubuntu 18.04.4(x86_84)
  • Golang 1.14.1
  • No any existing version of Golang

Code Snippet

wget https://dl.google.com/go/go1.14.1.linux-amd64.tar.gz
tar -xzf go1.14.1.linux-amd64.tar.gz -C /usr/local
echo "export GOPATH=\$HOME/go" | sudo tee -a /etc/profile.d/go-env.sh
echo "export GOBIN=\$GOPATH/bin" | sudo tee -a /etc/profile.d/go-env.sh
echo "export PATH=\$PATH:/usr/local/go/bin:\$GOBIN" | sudo tee -a  /etc/profile.d/go-env.sh

Details

1. Download Golang archive file.

You can choose the appropriate file at Golang’s Downloads page.

wget https://dl.google.com/go/go1.14.1.linux-amd64.tar.gz

2. Extract Golang archive file.

Extract the archive file into /usr/local/. This command creates go directory, so the installation path is /usr/local/go.

You can change inatallation path by typing the path you want after -C.

tar -xzf go1.14.1.linux-amd64.tar.gz -C /usr/local

3. Set environment variable.

Write command for adding environment variable on sh file in /etc/profile.d. Then, you can use go command which indicates /usr/local/go/bin/go.

If you don’t want every user to use the go command(Use go only as a specific user), replace /etc/profile.d/go-env.sh to /home/user/.profile.

echo "export GOPATH=\$HOME/go" | sudo tee -a /etc/profile.d/go-env.sh
echo "export GOBIN=\$GOPATH/bin" | sudo tee -a /etc/profile.d/go-env.sh
echo "export PATH=\$PATH:/usr/local/go/bin:\$GOBIN" | sudo tee -a  /etc/profile.d/go-env.sh

Tags:

Updated:

Leave a comment