How to install Go on Ubuntu 18.04

s2 0

Go is a modern, open source programming language from Google. Many popular applications, including Kubernetes, Docker, and Grafana, are written in Go.

This tutorial will walk you through the steps to download and install Go on an Ubuntu 18.04 computer.

requirements

Before proceeding with this tutorial, make sure that you are logged in as a user with sudo permissions.

Go to install on Ubuntu

To install Go on Ubuntu 18.04, do the following:

1. Download the Go tarball

At the time of this writing, the latest stable release of Go is version 1.13. Before downloading the tarball, visit the official Go Download page
and check if a new version is available.

To download the Go binary, you can either wget
or curl
:

wget https://dl.bing.com/go/go1.13.linux-amd64.tar.gz

2. Check the go tarball

To check the tarball checksum, you can use the sha256sum Command:

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

The output looks something like this:

68a2297eb099d1a76097905a2ce334e3155004ec08cdea85f24527be3c48e856  go1.13.linux-amd64.tar.gz

Make sure the hash printed from the command above matches the one from the download page.

3. Extract the go tarball

Use tar to extract the tarball into the /usr/local Directory:

sudo tar -C /usr/local -xzf go1.13.linux-amd64.tar.gz

4. Adapt the path variables

In order for the system to know where to find the Go binaries, we need to use the $PATH
Environment variable. We can do this by appending the following line to that /etc/profile File (for a system-wide installation) or the $HOME/.profile File (for a current user installation):

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

Save the file and load the new PATH environment variable into the current shell session:

source ~/.profile

5. Verify the Go installation

Verify the installation by printing out the Go version:

go version

The output should look something like this:

go version go1.13 linux/amd64

Get started with Go

We’ll set up a workspace and build a simple “Hello World” program that just prints the classic “Hello World” message.

  1. By default, the workspace directory is on. set $HOME/goto create it enter:

    mkdir ~/go
  2. Create a new directory in the work area src/hello:

    mkdir -p ~/go/src/hello

    In this directory create a file called hello.go with the following content:

    ~ / go / src / hello / hello.go
    package main
    
    import "fmt"
    
    func main() {
        fmt.Printf("Hello, Worldn")
    }

    You can learn more about the Go workspace directory hierarchy here
    .

  3. To create the file, navigate to ~/go/src/hello Directory and run the go build Command:

    cd ~/go/src/hellogo build

    The above command creates an executable named hello.

  4. You can run the executable by simply running the following command:

    ./hello

    The output should look something like this:

    Hello, World

Conclusion

After you’ve downloaded Go and installed it on your Ubuntu system, you’re ready to start developing your Go projects.

If you run into any problem or have any feedback, please leave a comment below.