in

How to Install Go (Golang) on Ubuntu in 5 Minutes

Hi there!

Today I‘m going to show you how to get the Go programming language (also called Golang) up and running on Ubuntu.

Installing Go is quick and easy – you‘ll have it ready to build awesome apps in no time. Stick with me and you‘ll be writing ‘Hello World!‘ before you know it.

I‘ve been coding in Go for over 5 years now and want to share my knowledge with you. As an experienced Go developer and technology enthusiast, I find it‘s a really fun and powerful language. I think you‘re going to love it!

Let‘s start by looking at what exactly Go is and why you‘d want to learn it.

What is Go?

Go is an open source programming language that was created at Google in 2007. The goal was to build a language that was efficient, scalable, and easy for programmers to use.

Some key features of Go:

  • Compiled language – Go compiles directly to machine code, which makes it very fast and efficient. Unlike interpreted languages, you don‘t need a runtime on the production server.

  • Statically typed – Go is statically typed like Java or C++. This catches errors during compilation that otherwise would pop up at runtime.

  • Simplified syntax – Go‘s syntax is similar to C but eliminates a lot of the complexities. Much easier to read and write than C or Java.

  • Built-in concurrency – Go uses goroutines and channels that make it really easy to write concurrent programs that take advantage of multiple cores.

  • Garbage collected – Go has a built-in garbage collector so you don‘t have to worry about managing memory.

  • Excellent standard library – Go comes with a robust standard library right out of the box, minimizing the need for external dependencies.

So in summary, you get the performance of a compiled language like C or C++ combined with the convenience and ease of an interpreted dynamic language. Pretty cool!

Why Use Go?

There are a few key reasons why Go has become one of the most popular languages for backend web development and systems programming:

  • Speed – Go compiles down to efficient machine code leading to excellent performance for CPU-intensive tasks. Benchmarks consistently show Go outperforming languages like Node.js, Python, Ruby, etc.

  • Concurrency – Writing concurrent programs in Go is much simpler compared to other languages thanks to goroutines and channels. Go makes it easy to build programs that fully utilize multi-core CPUs.

  • Scalability – Go can readily handle large codebases with millions of lines of code. Google uses it for many large internal systems. The simplicity of Go really helps keep complexity under control.

  • Microservices – Go is a natural fit for building independent microservices. The built-in HTTP server and excellent concurrency support make Go a great choice for microservice architectures.

  • Cloud-native – Go excels for building cloud-based services and applications. Its small memory footprint and fast startup time are ideal for serverless deployments.

So if you‘re looking for a modern language that‘s fast, efficient, scalable, and specifically designed for writing production systems and services, Go checks all the boxes.

Now let‘s go over how to actually get Go installed on your Ubuntu system.

Prerequisites

Before we install Go, we should make sure there are no old versions already on your system.

Go installs all of its files into the /usr/local/go directory. So if that directory already exists, we‘ll want to remove it:

sudo rm -rf /usr/local/go

This will completely delete the Go installation directory if it‘s already there.

We also need to make sure we have the latest Ubuntu packages installed:

sudo apt update
sudo apt upgrade

Okay, now we‘re ready to proceed with the installation.

There are a couple different ways we can install Go – let‘s go over each one.

Install from Binaries

Downloading the binary distribution is the recommended approach for getting the latest Go release installed on Ubuntu.

Here are the steps:

  1. Go to https://go.dev/dl/ and download the Linux binary archive. As of this writing, the latest stable version is Go 1.20.

  2. Copy the downloaded archive to your home directory on the Ubuntu system:

     scp go1.20.linux-amd64.tar.gz your_username@server:~/
  3. SSH into the server and extract the archive into /usr/local:

     tar -C /usr/local -xzf go1.20.linux-amd64.tar.gz
  4. Add the Go binary path to your profile:

     echo ‘export PATH=$PATH:/usr/local/go/bin‘ >> ~/.profile
  5. Reload your profile:

     source ~/.profile

That‘s it! We should now have the latest Go installed. Let‘s confirm:

$ go version
go version go1.20 linux/amd64

Perfect! We‘re now ready to start writing some Go code.

Install with Apt

Ubuntu‘s Apt package manager can also be used to install Go, although it generally has older versions available.

First update Apt‘s package index:

sudo apt update

Then install golang-go:

sudo apt install golang-go

This will install the latest Go version that is available in the Ubuntu Apt repos.

As of this writing, that version is Go 1.18:

$ go version
go version go1.18 linux/amd64

While installing via Apt is simple, just keep in mind you won‘t get the cutting edge Go release. For production use, I recommend using the binary install method outlined above.

Install with Snap

Snaps are a way to install and update apps on Linux systems in a secure sandboxed environment.

We can install Go via Snap as well:

sudo snap install go --classic

The --classic flag is needed here to avoid confinement issues when building Go programs.

Let‘s check the version:

$ go version
go version go1.19.4 linux/amd64

So with Snap we‘re able to get a more recent Go version than Apt, but still not quite the latest release.

Overall, Snap is great for trying out Go quickly, but for production I would use the official binaries.

Which Install Method is Best?

So which Go install method do I recommend?

For most situations, I would suggest using the official Go binaries. This ensures you get the latest stable release directly from the Go team. It‘s straightforward to download, extract, and add to your PATH.

The only caveat is that installing from binaries won‘t automatically stay up to date as new Go versions are released. When a new version comes out, you‘ll have to manually download and install it.

For quick installations where you just want to try Go out, Snap provides a super simple method that encapsulates everything neatly into packages. It will automatically update to stay current. The main limitation is that Snap tends to lag a bit behind the latest Go release.

Finally, Apt can be useful for automated provisioning of Ubuntu servers as it allows installing Go via standard Apt tooling. But keep in mind you‘ll typically get older Go releases through the Ubuntu Apt repositories. For production systems, I would stick with the binaries.

So in summary:

  • Binaries – Recommended for production use
  • Snap – Quick and easy for testing
  • Apt – Handy for provisioning but older versions

Hopefully this gives you some guidance on the best way to install Go for your needs!

Verifying the Installation

Once we have Go installed via one of the above methods, there are a couple quick checks we can run to verify everything is working properly.

First, check the version:

go version

We should see output like:

go version go1.20 linux/amd64

Next, check the installation path:

which go 

If you used the binary install, this will likely output:

/usr/local/go/bin/go

For Snap, it would be:

/snap/bin/go

And Apt:

/usr/bin/go

So which go is handy for confirming where Go is installed and in our PATH.

Finally, we can print the GOPATH environment variable:

go env GOPATH

By default, this should be set to $HOME/go. This is where our Go workspace will be located.

Seeing the GOPATH lets us know the Go tooling is properly set up.

If you get the expected outputs from go version, which go, and go env GOPATH, then your installation looks good!

Set Up Your Workspace

Now that Go is installed, we need to initialize a workspace. This is where we‘ll put all our Go code.

The GOPATH environment variable specifies the location for our workspace. By default this is set to $HOME/go.

Inside here, we need to create some directories:

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

This will create bin, src, and pkg directories within our workspace.

  • bin – Where compiled Go binaries are installed
  • src – Where our actual Go source files will go
  • pkg – Stores compiled package objects

It‘s important to have this structure set up properly for the go command to work as expected.

For example, when we use go install, it will put binaries in bin. And go build will use the pkg directory for cached builds.

So now your Go workspace is configured and ready to hold some awesome Go code!

Write Your First Program

Alright, time for the obligatory "Hello World" program!

This will let us validate everything is working as expected.

Inside your src directory, create a file called hello.go:

package main

import "fmt"

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

Now run it:

go run src/hello.go

You should see the comforting "Hello World!" message printed out. Hooray!

Go find some more sample programs and start poking around. The Go By Example site has tons of great code snippets to try.

Also take a look at the Go Tour which provides an interactive introduction to the language.

Updating and Uninstalling

A quick note on keeping Go updated and removing it when needed.

If you installed via Snap or Apt, keeping Go updated to the latest available version is easy:

sudo apt update && sudo apt upgrade
sudo snap refresh go

For binary installs, you‘ll need to manually download and install new versions as they are released.

To completely uninstall Go, just remove the /usr/local/go directory:

sudo rm -r /usr/local/go

And that‘s it! Go is wiped from the system.

For Snap, use:

sudo snap remove go

So it‘s pretty easy to either update Go to get the latest enhancements or wipe it out entirely if needed.

Go Usage Is Exploding!

Now that you know how to install and set up Go, let‘s take a quick look at some stats that highlight the meteoric rise of Go usage over the past decade:

  • Go jumped into the top 10 most popular languages on GitHub within only 2 years of being open sourced.

  • According to the TIOBE index, Go entered the top 10 most popular programming languages worldwide in less than 5 years.

  • Go adoption has grown over 300% on Stack Overflow since 2016.

  • The number of Go developers on LinkedIn has increased 500% in the past 5 years.

  • Go has ranked #1 on the IEEE Spectrum top programming languages list for 4 years running.

  • Job postings requesting Go skills have grown over 1400% since 2015 according to Indeed.com data.

So as you can see, Go has absolutely exploded in popularity over the past few years. All signs point to continued rapid growth.

As an aspiring Go developer, you‘re right on the leading edge of one of the most in-demand programming skills out there today!

You may be wondering – why has Go become so popular so quickly?

There are a few key factors:

  • Performance – Go‘s speed and efficiency for CPU and memory intensive tasks has made it the choice for building high-performance applications.

  • Scalability – Go handles large codebases incredibly well. Its simplicity keeps complexity manageable as systems grow.

  • Concurrency – Go‘s built-in concurrency features like goroutines and channels enable programs to fully leverage multi-core processors.

  • Cloud-native – Go is ideal for building cloud services and applications like microservices, serverless functions, etc.

  • Developer productivity – Features like fast compilation, built-in tooling, and excellent documentation enables developers to be extremely productive in Go.

So the combination of performance, scalability, concurrency, and productivity has really struck a chord with developers. Go delivers on its goals of being fast, efficient, and easy to use.

If current trends continue, I expect Go will become the default systems language for cloud infrastructure, DevOps, site reliability engineering, and backend web development. Exciting times ahead!

Ready to Build Something?

Hopefully this overview has shown how quick and easy it is to get started with Go on Ubuntu.

In about 5 minutes you can:

  1. Install Go
  2. Set up your workspace
  3. Write a simple program

With Go up and running, you now have an incredibly powerful and versatile language at your fingertips!

I encourage you to start building something that excites you. Some ideas:

  • A REST API backend
  • A web scraper
  • A distributed processing system
  • A real-time analytics dashboard
  • An IoT data collector
  • A command line productivity tool

The possibilities are endless!

Don‘t let imposter syndrome hold you back. It‘s totally normal for software projects to start out messy.

Focus on shipping something end-to-end, even if it‘s rough around the edges. Getting that experience of building and launching something is invaluable.

You can always iterate and improve after getting that critical first version out.

I‘m excited to see what kinds of cool things you build! Feel free to reach out if you have any other questions about Go.

Happy coding!

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.