Golang on Linux Mint 18

How to install Go 1.8 on Ubuntu 17.04

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:

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:

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:

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:

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.

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

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:

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:

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

Please share:

One Response

Add a Comment

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