Partner links

How to install Go 1.8 on Ubuntu 17.04

Golang on Linux Mint 18

Go, or Golang, is an open source programming language from Google. Released in late-2009, it has since become a very popular programming language. Docker and many of its tools are written in Go, as are many new Web applications.

Unlike Python, it doesn’t come pre-installed on most Linux distributions, though its installation packages are likely in the official package repositories of your favorite Linux distribution. However, it is also likely that that package is one or two revisions behind the latest version. And that is the case with Ubuntu 17.04, where the version in the repository is v1.6. The latest version is v1.8.3.

So in this tutorial, we’ll go through the process of installing Go 1.8.3 on Ubuntu 17.04. That process involves downloading the Go binary from the project’s download page, copying it to the appropriate system directory, and setting up a Go workspace on the target system. The same method may also be used to install it on a related distribution, like Linux Mint 18.2, or even Debian 9 or Debian 8.

Download and Install the Go Binary

The Go binary is available for download from the project’s download page. Use the following commands to download and install it on your system:

# Download Go 1.8.3

wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz

# Install Go 1.8.3 to the default system directory - /usr/local

sudo tar -C /usr/local -xf go1.8.3.linux-amd64.tar.gz

# Verify that it's installed
# > indicates output

/usr/local/bin/go version
> go version go1.8.3 linux/amd64

# Execute the "go" command to see the new commands available on the system 

/usr/local/bin/go

#

With that, Go 1.8.3 is now installed on your system. But I’m sure you noticed that the last two commands above were executed by specifying their absolute paths. That’s not how it should be, so the next task is to add the Go installation bin directory to your PATH.

Add the Go Installation bin Directory to PATH

To make the newly-installed Go tools available in your PATH, so that you can execute the go command without needing to specify the absolute PATH, you’ll need to add the Go bin directory (/usr/local/go/bin) to the PATH environment variable, either system wide in the /etc/profile directory, or in your ~/.profile file. It can also go in your ~/.bashrc file or wherever you have PATH defined. Use the following commands to modify the PATH definition in your ~/.profile file:

# Use the nano editor to open the .profile file, or the file where PATH is defined on your system

nano ~/.profile

# Modify PATH as that it reads as follows 

export PATH=$PATH:/usr/local/go/bin

# Save and close the file, then activate the change using the following command

source ~/.profile

#

With that last command, you can now execute go without needing to specify the absolute PATH. Just type go ….

Make GOPATH Available to Bash

Installation of Go adds more than twenty new environmental variables to the system (to view them, type go env), one of which is GOPATH, which points to the workspace (projects directory structure) for your Go projects. By default, it points to a directory named go in your home directory, that is, ~/go or $HOME/go. You may continuing using the default, or change it. In this tutorial, we’ll stick with the default.

To make GOPATH available to Bash, add it to your Bash profile file or wherever you defined PATH. For this tutorial, we’ll add in the ~/.profile. You may do so using the following commands:

# Use the nano editor to open the ~/.profile file

nano ~/.profile

# Set GOPATH environment variable by adding the next line 

export GOPATH=$HOME/go

# Alternatively, use the following

export GOPATH=$(go env GOPATH)

# Save and close the file, then activate it

source ~/.profile

# 

Test Your Go Installation

Here we’ll set up an example Go program named Hello World! to test whether our installation of Go works. If Go can run it successfully, then Go is truly set to go. We’ll create the source directory for the program under GOPATH. Use the following commands to set up the program’s structure using github.com/your-username, gitlab.com/your-username or any other application that you use as the base path:

# Create the Go project's directory structure
# Use the next command if you're using GitHub to manage your project
# Replace "user" with your username on GitHub

mkdir -p ~/go/src/github.com/user/hello

# Use this if you're using GitLab. Replace "user" with your username on GitLab

mkdir -p ~/go/src/gitlab.com/user/hello

# 

Inside the hello directory you created using the last command, create a file named hello.go and copy and paste the following code into it. All the code does is emit the famous Hello, world! greeting.

# Create a file called hello.go

nano ~/go/src/github.com/user/hello/hello.go

# Or

nano ~/go/src/gitlab.com/user/hello/hello.go

# Copy and paste the following code into hello.go

package main

import "fmt"

func main() {
    fmt.Printf("Hello, world!\n")
}

# 

Save and close the file. Use the next command to compile the program:

# Compile the hello Go code
# If you went the path of GitHub, use this command
# Remember to replace user with your username on GitHub

go install github.com/user/hello

# Use this if you went the path of GitLab
# Remember to replace user with your username on Gitlab

go install gitlab.com/user/hello

# 

If the code compiled successfully, there should now be an executable named hello in the bin directory of your Go workspace. And if you run the command, you’ll get the expected output, as shown here:

# Run Go command
# > indicates output

$GOPATH/bin/hello
> Hello, world!

# 

If you got the correct output as in the above code, then you’ve successfully installed Go on your Ubuntu 17.04 installation. To make running your new Go executables easier, let’s add the bin directory of your GOPATH to PATH, so you won’t have to specify the absolute paths as in the above command.

Add GOPATH bin Directory to PATH

In this final and last section, we’ll add the bin directory of your GOPATH directory to your PATH. Since we already modified PATH earlier, we’ll just append the new entry to it using the following commands:

# Use nano to open the .profile file

nano ~/.profile

# The line for PATH in that file should read as follows

export PATH=$PATH:/usr/local/go/bin

# Append the new entry to it so that it reads as follows

export PATH=$PATH:/usr/local/go/bin:$(go env GOPATH)/bin

# Save and close the file, then activate the change using

source ~/.profile

#

That’s it! You just installed Go 1.8.3 on Ubuntu 17.04 and you can execute new Go commands by specifying just the relative paths. To learn more about Go and how to start developing Go applications, click here.

Golang logo

Share:

Facebook
Twitter
Pinterest
LinkedIn

Partner links

Newsletter: Subscribe for updates

Subscribe
Notify of
guest
1 Comment
Inline Feedbacks
View all comments
Rodislav
6 years ago

Testing of the version
In my case it was: /usr/local/go/bin/go version

The code highlighter is a bit strange, it removes the selection by using a magic algorithm, could you improve it ?
thanks

Get the latest

On social media

Security distros

Hacker
Linux distros for hacking and pentesting

Crypto mining OS

Bitcoin
Distros for mining bitcoin and other cryptocurrencies

Crypto hardware

MSI GeForce GTX 1070
Installing Nvidia GTX 1070 GPU drivers on Ubuntu

Disk guide

LVM
Beginner's guide to disks & disk partitions in Linux

Bash guide

Bash shell terminal
How to set the PATH variable in Bash
Categories
Archives
1
0
Hya, what do you think? Please comment.x
()
x