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.

Related Post:  How to install Ansible on your favorite Linux distribution

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.

Related Post:  Triple-boot Windows 7 and 8 and Ubuntu 14.04 on a PC with UEFI firmware

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:

Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest
Share on linkedin
LinkedIn

Hola! Did you notice that LinuxBSDos.com no longer run network ads?  Yep, no more ads from the usual suspects that track and annoy you across the Internet. But since I still need to pay to keep the site running, feel free to make a small donation by PayPal or your favorite cryptocurrency.

  • Bitcoin
  • Ethereum
  • Xrp
  • Bitcoin cash
  • Bitcoin sv
  • Litecoin
  • Binance coin
  • Cardano
  • Ethereum classic
Scan to Donate Bitcoin to bc1qzvlte2m224zkayhdc7fdfjkp2rsgt0l5a496ua

Donate Bitcoin to this address

Scan the QR code or copy the address below into your wallet to send some Bitcoin

Scan to Donate Ethereum to 0x0F4362DFF77F3Ba0Dc637F5f3Eba35D09a2fA60C

Donate Ethereum to this address

Scan the QR code or copy the address below into your wallet to send some Ethereum

Scan to Donate Xrp to r4ggjvL36njsMCYTkJ3S7cTHscPsMsSGQv

Donate Xrp to this address

Scan the QR code or copy the address below into your wallet to send some Xrp

Scan to Donate Bitcoin cash to qrs0dedzp9t55af3nfwypydghp29r0xguy9s20fz2k

Donate Bitcoin cash to this address

Scan the QR code or copy the address below into your wallet to send some Bitcoin cash

Scan to Donate Bitcoin sv to 15K9TLyVDBtLuG9cYvXCX9SSkq9C9oUKHK

Donate Bitcoin sv to this address

Scan the QR code or copy the address below into your wallet to send some Bitcoin sv

Scan to Donate Litecoin to LetJ9QQMb7u2LMZ9Tu6rtHwcBcQFW98fbG

Donate Litecoin to this address

Scan the QR code or copy the address below into your wallet to send some Litecoin

Scan to Donate Binance coin to bnb1ga8trq08ssqepd90v6225nzfgy448pu5pw8gxp

Donate Binance coin to this address

Scan the QR code or copy the address below into your wallet to send some Binance coin

Scan to Donate Cardano to addr1qx2354yw49etstfljpdhwja3ajjlt487lg95vu9ngy2q6vu4rf2ga2tjhqknlyzmwa9mrm997h20a7stgectxsg5p5esq5l7d9

Donate Cardano to this address

Scan the QR code or copy the address below into your wallet to send some Cardano

Scan to Donate Ethereum classic to 0xcD6CC972a2297FcafACDcfE042C55C69516a9264

Donate Ethereum classic to this address

Scan the QR code or copy the address below into your wallet to send some Ethereum classic

Subscribe for updates. Trust me, no spam!

Sponsored links

1. Attend Algorithm Conference, a top AI and ML event.
2. Reasons to use control panel for your server.
3. DHgate Computers Electronics, Cell Phones & more.

Upcoming events

One Response

  1. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest

On social media
Via my newsletter

Partner links

1. Attend Algorithm Conference, a top AI and ML event for 2021.
2. Reasons to use control panel for your server.
3. DHgate Computers Electronics, Cell Phones & more.
Hacking, pentesting distributions

Linux Distributions for Hacking

Experts use these Linux distributions for hacking, digital forensics, and pentesting.

Categories
Archives

The authors of these books are confirmed to speak during

Algorithm Conference

T-minus AI

Author was the first chairperson of AI for the U.S. Air Force.

The case for killer robots

Author is the Director of the Center for Natural and Artificial Intelligence.

Why greatness cannot be planned

Author works on AI safety as a Senior Research Scientist at Uber AI Labs.