How to install Go and set up a local development environment on Ubuntu 18.04

0 Shares
0
0
0
0

Introduction

Go is a programming language born out of frustration at Google. Developers were constantly forced to choose between a language that ran efficiently but took a long time to compile, or a language that was easy to program in but inefficient in production. Go was designed to have all three at once: fast compilation, ease of programming, and efficient production execution.

While Go is a versatile programming language that can be used for many different programming projects, it is particularly well-suited for network/distributed systems applications and is known as the “language of the cloud.” It focuses on helping the modern programmer do more with a robust set of tools, eliminates formatting discussions by making formatting part of the language specification, and also simplifies deployment by compiling into a single binary. Go is easy to learn, with a very small set of keywords, making it a great choice for beginners and experienced developers alike.

This tutorial will guide you through installing and configuring a Go programming workspace via the command line. This tutorial explicitly covers the installation steps for Ubuntu 18.04, but the general principles can be applied to other Debian-based Linux distributions.

Prerequisites
  • You will need a computer or virtual machine with Ubuntu 18.04 installed, as well as administrative access to that device and an internet connection.

Step 1 – Setting Up Go

At this point, you will install Go by downloading the current version from the official Go downloads page.

First, connect to the Ubuntu server via ssh:

ssh sammy@your_server_ip

Next, go to the official Go downloads page in your web browser. From there, copy the URL for the current binary tarball.

As of this writing, the latest version is go1.16.7. To install Go on an Ubuntu server (or any Linux server), copy the URL of the file ending in linux-amd64.tar.gz.

Now that you have your link ready, first verify that you are in the main directory:

cd 

Then use curl to retrieve the tarball, making sure to replace the highlighted URL with the one you just copied. The -O flag ensures that this output goes to a file, and the L flag instructs HTTPS redirection, as the link is taken from the Go website and is redirected here before downloading the file:

curl -OL https://golang.org/dl/go1.16.7.linux-amd64.tar.gz

To verify the authenticity of the file you downloaded, run the sha256sum command, passing it the file name as an argument:

sha256sum go1.16.7.linux-amd64.tar.gz

This will return the SHA256 checksum of the tarball:

Output
go1.16.7.linux-amd64.tar.gz
7fe7a73f55ba3e2285da36f8b085e5c0159e9564ef5f63ee0ed6b818ade8ef04 go1.16.7.linux-amd64.tar.gz

If the checksum matches what is listed on the Downloads page, you have completed this step correctly.

Next, use tar to extract the tarball. This command includes the -C flag, which tells tar to change to the given directory before performing any other operations. This means that the extracted files will be written to the /usr/local/ directory, the recommended location for installing Go… The x flag tells tar to extract, v tells it that we want the full output (a list of the files being extracted), and f tells it to specify a filename:

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

Although /usr/local/go is the recommended location to install Go, some users may prefer or need different paths.

Step 2 – Setting Go Paths

In this step, you determine paths in your environment.

First, set the Go root value, which tells Go where to look for its files. You can do this by editing the .profile file, which contains a list of commands that the system runs every time you log in.

Use your favorite editor to open the .profile file, which is stored in the user's home directory. We'll use nano here:

sudo nano ~/.profile

Then add the following information to the end of your file:

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

Once you have added this information to your profile, save and close the file. If you are using nano, do this by pressing CTRL+X, then Y, then ENTER.

Then update your profile by running the following command:

source ~/.profile

After that, check if you can run go commands by running the go version:

go version

This command will output the release number of each version of Go installed on your system:

Output
go version go1.16.7 linux/amd64

This output confirms that you are now running Go on your server.

Step 3 – Test the installation

Now that Go is installed and the paths for your server are set up, you can create your Hello, World! application to make sure Go is working.

First, create a new directory for your Go workspace, where Go will build its files:

mkdir hello

Then go to the directory you created:

cd hello

When importing packages, you need to manage dependencies through the module itself. You can do this by creating a go.mod file with the go mod init command:

go mod init your_domain/hello

Next, create a Hello, World! Go file in your favorite text editor:

nano hello.go

Add the following text to your hello.go file:

package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}

Then save and close the file by pressing CTRL+X then Y then ENTER.

Test your code to verify that it prints Hello, World! Greeting:

go run .
Output
Hello, World!

The go run command compiles and runs the Go package from a list of .go source files in the new hello directory you created and the path you entered. However, you can also use go build to create an executable file, which can save you time.

Step 4 – Convert your Go code into a binary executable file

The go run command is commonly used as a shortcut to compile and run a program that requires frequent changes. In cases where you have finished your code and want to run it without compiling it every time, you can use go build to convert your code into an executable binary. Building your code into an executable binary combines your program into a single file with all the supporting code necessary to run the binary. Once you have built the binary executable, you can run the go install program to place your program in the executable path so that you can run it from anywhere on your system. Then, your program will successfully say Hello, World! when prompted and you will no longer need to compile the program.

Try it out and run go build. Make sure to run this from the same directory where your hello.go file is stored:

go build

Then, run ./hello to verify that the code is working correctly:

./hello
Output
Hello, World!

This confirms that you have successfully converted your hello.go code into an executable binary. However, you can only call this binary from within this directory. If you want to run this program from another location on your server, you will need to specify the full path to the binary file to run it.

Typing out the full path to a binary file can quickly become tedious. Alternatively, you can run the go install command. This is similar to go build but instead of putting the executable in the current directory, go install and place it in the $GOPATH/bin folder, which allows you to run it from anywhere on your server.

To successfully run go install, you need to pass it the binary installation path that you created with go build. To find the binary installation path, run the following go list command:

go list -f ‘{{.Target}}’

go list produces a list of every Go package by name stored in the current working directory. The f flag causes go list to return the output in a different format based on the package pattern you pass it. This tells it to use the Target pattern, which causes go list to return the installation path of every package stored in this directory:

Output
‘/home/sammy/go/bin/hello

This is the installation path of the binary file that you created with go build. That is, the directory where this binary is installed is /home/sammy/go/bin/.

Add this installation directory to your system shell path. Make sure to change the highlighted part of this command to reflect the binary installation directory on your system, if it is different:

export PATH=$PATH:/home/sammy/go/bin/

Finally, run go install to compile and install the package:

go install

Run this executable binary by just typing hello

hello
Output
Hello, World!

If you get Hello, World! output, you have successfully made your Go application executable from a specified and unspecified path on your server.

Result

By downloading and installing the latest Go package and specifying its paths, you now have a system for Go development. You can find additional articles on installing and using Go in our Go tag and subscribe

Leave a Reply

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

You May Also Like