Menu

Install Go on Ubuntu 18.04

In this post, I will show you how to install go on Ubuntu 18.04 and we will compile and run a sample application.

What is Go?

In case you are not sure what the Go language is and why it was created here is an excerpt from the golang.org website about what Go is:

Go is a general-purpose language designed with systems programming in mind. It is strongly typed and garbage-collected and has explicit support for concurrent programming. Programs are constructed from packages, whose properties allow efficient management of dependencies.

Install Go

There are a couple of ways to install Go on your Ubuntu system.  The easiest way is to use SNAP:

# snap install go --classic

You can also use the regular Canonical version:

# apt install golang-go

Or you can install the GCC version with

# apt install gccgo-go

Those are the easy ways.  If you want the latest version of go you will need to install it by downloading the binaries or compiling from source.  In this post, I will cover how to install the binaries.  If you installed using one of these methods then skip ahead to the Sample Application section.

Installing the Go Binaries

First you will need to go to the Go download page:

https://golang.org/dl/

Click the link for Linux which in the example below is go1.10.3.linux-amd64.tar.gz.

install go ubuntu 18.04

or you can use wget:

# wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz

Of course by the time you read this the version may have changed so adjust appropriately.  Next we need to untar the file:

# tar -xzvf go1.10.3.linux-amd64.tar.gz

From here you can move it to a better home:

# mv go/ /usr/local/bin

Then add it to your path.

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

And link it to permanently be able to run it.

# cd /usr/bin
# ln -s /usr/local/bin/go/bin/go go

No you should be able to run go:

# go version
go version go1.10.3 linux/amd64

Now you have Go installed and you are ready to start programming.

Sample Go Application

Since Go is a systems programming language, we will make our sample application something systems related.  This sample application will be a simple app to show you the system time.

Open your favorite editor and create a new file called gettime.go.  Put in the following contents.

package main

import (
        "fmt"
        "time"
)

func main() {
        fmt.Println(time.Now())
}

Save the file then exit.

Go is a compiled language so we need to compile our code to a native executable:

# go build gettime.go

You will now have an executable in your current directory that you can run:

# ./gettime
2018-06-11 20:02:08.774145422 +0000 UTC m=+0.000131689

Click here for more great articles from AdminTome Blog.

Conclusion

I hope you have enjoyed this article, if so please leave a comment below.  For more articles, please signup for the AdminTome Blog below.  Also please feel free to share the article to your friends using the buttons to the left.  Thanks again for reading this post.

Signup for the AdminTome Blog Newsletter

Get weekly updates from AdminTome Blog including the latest articles and special content only for subscribers.

privacy We value your privacy and would never spam you

 
Tags: ,
%d bloggers like this: