How To Install Go On Ubuntu 18.04

Introduction

Go is a modern programming language developed by Google. It is an open source programming language that makes it easy to build simple, reliable, and efficient software, and offers a robust set of libraries. Here we’ll look at how to download and install the latest version of Go (Go 1.10.3). Also, we'll demonstrate how to build a simple Hello World application.

Prerequisites

  • Ubuntu 18.04 system and it have to be configured with a non-root user with sudo privileges.

Step 1 — Installing Go

Step one is about installing Go on your server,First, connect to your Ubuntu server via ssh using the line below .

$  ssh pseudo@server_ip

Then,download the latest version of Go from the official Go downloads page,On the web site you can see the URL for the current binary release tarball with its SHA256 Checksum.

to install Go be sure you're in your home directory, and use curl to get the tarball.

$ cd ~                                                                                                                                         .
$ curl -O https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz

Use the sha256 Checksum to verify the tarball by using the command line below .

$  sha256sum go1.10.3.linux-amd64.tar.gz

Sample Output

go1.10.3.linux-amd64.tar.gz                                                                                                                       .
fa1b0e45d3b647c252f51f5e1204aba049cde4af177ef9f2181f43004f901035 go1.10.3.linux-amd64.tar.gz

Then,make sure you got a hash like the one highlighted in the above output  and also make sure it matches the one from the official downloads page.

Next, use tar to extract the tar.

  • x flag inform tar to extract
  • v flag inform tar that we want a list of the extracted files
  • f flag inform tar that we'll specify a filename:
$  tar xvf go1.10.3.linux-amd64.tar.gz

Now you have a directory called go in your home directory. change go's owner and group to root, and move it to /usr/local.

$  sudo chown -R root:root ./go                                                                                                 .
$  sudo mv go /usr/local

Note: Although /usr/local/go is the officially recommended location, but you may prefer  to use or require different paths.

Step 2 — Setting Go Paths

Here  we'll need to  set some paths in your environment.

First, set Go's root value to tells Go where to look for its files.

$  sudo nano ~/.profile

At the end of the file, add this line

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

In case you choose an alternate installation location for Go, you have to add these lines instead to the same file. In this example demonstrate how to use  the commands if Go is installed in your home directory .

export GOROOT=$HOME/go                                                                                                   .
export GOPATH=$HOME/work                                                                                                 .
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

After you add appropriate line into your profile, save and close the file. Then, refresh your profile by running this command .

$ source ~/.profile

Step 3 — Testing Your Install

So after installing Go and the paths are set for your server, you have to test to ensure that Go is working exactly as expected.

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

$  mkdir $HOME/work

Then,in order for you to create your test file create a directory hierarchy in this folder through this command.

if you plan to use Git to commit and store your Go code on GitHub,You can replace the value user with your GitHub pseudo, If you do not want to use GitHub to store and manage your code then , your folder structure could be something different, like ~/my_project.

$ mkdir -p work/src/github.com/user/hello

Now , you can easily create a simple "Hello World" program using Go .

$ nano ~/work/src/github.com/user/hello/hello.go

Go to your editor, and paste the below code, which uses the main Go packages, imports the formatted IO content component, then sets a new function to print "Hello, World" when we run it.

package main                                                                                                                            .
import "fmt"                                                                                                                                .
func main() {                                                                                                                              .
    fmt.Printf("hello, world\n")                                                                                                      .
}

The above program will print "hello, world" after we successfully run it, which will show that Go programs are compiling correctly. Save and close the file, then compile it by typing the Go command install as it shown in the command line below.

$ go install github.com/user/hello

After the file is compiled, you can run it by executing the command below .

$ hello

If the above  command shows "hello, world", then Go is successfully installed and functional on your system. if you want to see where the compiled hello binary is installed you can use the which command as shown below.

$ which hello

Output

/home/user/work/bin/hello

Conclusion

By downloading, installing and testing the latest Go package and setting its paths on your system, you can now start coding different type of application using this powerful and modern programming language from Google. You can find more about this language and also about the latest version of it (Go 1.10.3) by checking their official website.

 

Check out these top 3 Linux hosting services

Was this article helpful?