How To Install Go and Set Up a Local Programming Environment on Ubuntu 18.04

Written by Gopher Guides

Go is a programming language that was born out of frustration at Google. Developers continually had to pick a language that executed efficiently but took a long time to compile, or to pick a language that was easy to program but ran inefficiently in production. Go was designed to have all three available at the same time: fast compilation, ease of programming, and efficient execution in production.

While Go is a versatile programming language that can be used for many different programming projects, it’s particularly well suited for networking/distributed systems programs, and has earned a reputation as “the language of the cloud”. It focuses on helping the modern programmer do more with a strong set of tooling, removing debates over formatting by making the format part of the language specification, as well as making deployment easy by compiling to a single binary. Go is easy to learn, with a very small set of keywords, which makes it a great choice for beginners and experienced developers alike.

This tutorial will guide you through installing and configuring a programming workspace with Go via the command line. This tutorial will explicitly cover the installation procedure for Ubuntu 18.04, but the general principles can apply to other Debian Linux distributions.

Prerequisites

You will need a computer or virtual machine with Ubuntu 18.04 installed, as well as have administrative access to that machine and an internet connection. You can download this operating system via the Ubuntu 18.04 releases page.

Step 1 — Setting Up Go

In this step, you’ll install Go by downloading the current release from the official Go downloads page.

To do this, you’ll want to find the URL for the current binary release tarball. You will also want to note the SHA256 hash listed next to it, as you’ll use this hash to verify the downloaded file.

You’ll be completing the installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.

The command line, also known as a shell or terminal, can help you modify and automate many of the tasks you do on a computer every day, and is an essential tool for software developers. There are many terminal commands to learn that can enable you to do more powerful things. For more information about the command line, check out the Introduction to the Linux Terminal tutorial.

On Ubuntu 18.04, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left hand corner of your screen and typing terminal into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.

4. How To Install Go and Set Up a Local Programming Environment on Ubuntu 18.04 - 图1

Ubuntu Terminal

Once the terminal is open, you will manually install the Go binaries. While you could use a package manager, such as apt-get, walking through the manual installation steps will help you understand any configuration changes to your system that are needed to have a valid Go workspace.

Before downloading Go, make sure that you are in the home (~) directory:

  1. cd ~

Use curl to retrieve the tarball URL that you copied from the official Go downloads page:

  1. curl -O https://dl.google.com/go/go1.12.1.linux-amd64.tar.gz

Next, use sha256sum to verify the tarball:

  1. sha256sum go1.12.1.linux-amd64.tar.gz

The hash that is displayed from running the above command should match the hash that was on the downloads page. If it does not, then this is not a valid file and you should download the file again.

Output

  1. 2a3fdabf665496a0db5f41ec6af7a9b15a49fbe71a85a50ca38b1f13a103aeec go1.12.1.linux-amd64.tar.gz

Next, extract the downloaded archive and install it to the desired location on the system. It’s considered best practice to keep it under /usr/local:

  1. sudo tar -xvf go1.12.1.linux-amd64.tar.gz -C /usr/local

You will now have a directory called go in the /usr/local directory. Next, recursively change this directory’s owner and group to root:

  1. sudo chown -R root:root /usr/local/go

This will secure all the files and ensure that only the root user can run the Go binaries.

Note: Although /usr/local/go is the officially-recommended location, some users may prefer or require different paths.

In this step, you downloaded and installed Go on your Ubuntu 18.04 machine. In the next step you will configure your Go workspace.

Step 2 — Creating Your Go Workspace

You can create your programming workspace now that Go is installed. The Go workspace will contain two directories at its root:

  • src: The directory that contains Go source files. A source file is a file that you write using the Go programming language. Source files are used by the Go compiler to create an executable binary file.
  • bin: The directory that contains executables built and installed by the Go tools. Executables are binary files that run on your system and execute tasks. These are typically the programs compiled by your source code or other downloaded Go source code.

The src subdirectory may contain multiple version control repositories (such as Git, Mercurial, and Bazaar). This allows for a canonical import of code in your project. Canonical imports are imports that reference a fully qualified package, such as github.com/digitalocean/godo.

You will see directories like github.com, golang.org, or others when your program imports third party libraries. If you are using a code repository like github.com, you will also put your projects and source files under that directory. We will explore this concept later in this step.

Here is what a typical workspace may look like:

  1. .
  2. ├── bin
  3. ├── buffalo # command executable
  4. ├── dlv # command executable
  5. └── packr # command executable
  6. └── src
  7. └── github.com
  8. └── digitalocean
  9. └── godo
  10. ├── .git # Git repository metadata
  11. ├── account.go # package source
  12. ├── account_test.go # test source
  13. ├── ...
  14. ├── timestamp.go
  15. ├── timestamp_test.go
  16. └── util
  17. ├── droplet.go
  18. └── droplet_test.go

The default directory for the Go workspace as of 1.8 is your user’s home directory with a go subdirectory, or $HOME/go. If you are using an earlier version of Go than 1.8, it is still considered best practice to use the $HOME/go location for your workspace.

Issue the following command to create the directory structure for your Go workspace:

  1. mkdir -p $HOME/go/{bin,src}

The -p option tells mkdir to create all parents in the directory, even if they don’t currently exist. Using {bin,src} creates a set of arguments to mkdir and tells it to create both the bin directory and the src directory.

This will ensure the following directory structure is now in place:

  1. └── $HOME
  2. └── go
  3. ├── bin
  4. └── src

Prior to Go 1.8, it was required to set a local environment variable called $GOPATH. $GOPATH told the compiler where to find imported third party source code, as well as any local source code you had written. While it is no longer explicitly required, it is still considered a good practice as many third party tools still depend on this variable being set.

You can set your $GOPATH by adding the global variables to your ~/.profile. You may want to add this into .zshrc or .bashrc file as per your shell configuration.

First, open ~/.profile with nano or your preferred text editor:

  1. nano ~/.profile

Set your $GOPATH by adding the following to the file:

~/.profile

  1. export GOPATH=$HOME/go

When Go compiles and installs tools, it will put them in the $GOPATH/bin directory. For convenience, it’s common to add the workspace’s /bin subdirectory to your PATH in your ~/.profile:

~/.profile

  1. export PATH=$PATH:$GOPATH/bin

This will allow you to run any programs you compile or download via the Go tools anywhere on your system.

Finally, you need to add the go binary to your PATH. You can do this by adding /usr/local/go/bin to the end of the line:

~/.profile

  1. export PATH=$PATH:$GOPATH/bin:/usr/local/go/bin

Adding /usr/local/go/bin to your $PATH makes all of the Go tools available anywhere on your system.

To update your shell, issue the following command to load the global variables:

  1. . ~/.profile

You can verify your $PATH is updated by using the echo command and inspecting the output:

  1. echo $PATH

You will see your $GOPATH/bin which will show up in your home directory. If you are logged in as root, you would see /root/go/bin in the path.

Output

  1. /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/root/go/bin:/usr/local/go/bin

You will also see the path to the Go tools for /usr/local/go/bin:

Output

  1. /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/root/go/bin:/usr/local/go/bin

Verify the installation by checking the current version of Go:

  1. go version

And we should receive output like this:

Output

  1. go version go1.12.1 linux/amd64

Now that you have the root of the workspace created and your $GOPATH environment variable set, you can create your future projects with the following directory structure. This example assumes you are using github.com as your repository:

  1. $GOPATH/src/github.com/username/project

So as an example, if you were working on the https://github.com/digitalocean/godo project, it would be stored in the following directory:

  1. $GOPATH/src/github.com/digitalocean/godo

This project structure will make projects available with the go get tool. It will also help readability later. You can verify this by using the go get command and fetch the godo library:

  1. go get github.com/digitalocean/godo

This will download the contents of the godo library and create the $GOPATH/src/github.com/digitalocean/godo directory on your machine.

You can check to see if it successfully downloaded the godo package by listing the directory:

  1. ll $GOPATH/src/github.com/digitalocean/godo

You should see output similar to this:

Output

  1. drwxr-xr-x 4 root root 4096 Apr 5 00:43 ./
  2. drwxr-xr-x 3 root root 4096 Apr 5 00:43 ../
  3. drwxr-xr-x 8 root root 4096 Apr 5 00:43 .git/
  4. -rwxr-xr-x 1 root root 8 Apr 5 00:43 .gitignore*
  5. -rw-r--r-- 1 root root 61 Apr 5 00:43 .travis.yml
  6. -rw-r--r-- 1 root root 2808 Apr 5 00:43 CHANGELOG.md
  7. -rw-r--r-- 1 root root 1851 Apr 5 00:43 CONTRIBUTING.md
  8. .
  9. .
  10. .
  11. -rw-r--r-- 1 root root 4893 Apr 5 00:43 vpcs.go
  12. -rw-r--r-- 1 root root 4091 Apr 5 00:43 vpcs_test.go

In this step, you created a Go workspace and configured the necessary environment variables. In the next step you will test the workspace with some code.

Step 3 — Creating a Simple Program

Now that you have the Go workspace set up, create a “Hello, World!” program. This will make sure that the workspace is configured properly, and also gives you the opportunity to become more familiar with Go. Because we are creating a single Go source file, and not an actual project, we don’t need to be in our workspace to do this.

From your home directory, open up a command-line text editor, such as nano, and create a new file:

  1. nano hello.go

Write your program in the new file:

  1. package main
  2. import "fmt"
  3. func main() {
  4. fmt.Println("Hello, World!")
  5. }

This code will use the fmt package and call the Println function with Hello, World! as the argument. This will cause the phrase Hello, World! to print out to the terminal when the program is run.

Exit nano by pressing the CTRL and X keys. When prompted to save the file, press Y and then ENTER.

Once you exit out of nano and return to your shell, run the program:

  1. go run hello.go

The hello.go program will cause the terminal to produce the following output:

Output

  1. Hello, World!

In this step, you used a basic program to verify that your Go workspace is properly configured.

Conclusion

Congratulations! At this point you have a Go programming workspace set up on your Ubuntu machine and can begin a coding project!