Sling Academy
Home/Golang/How to set up and run Go in Ubuntu

How to set up and run Go in Ubuntu

Last updated: November 20, 2024

The Go programming language, also known as Golang, has gained immense popularity due to its simplicity, efficiency, and strong concurrency mechanisms. If you are developing applications on Ubuntu, you'll find Go to be a powerful ally. In this article, we'll walk through the essentials of setting up and running Go in Ubuntu.

Why Go?

Before diving into the setup process, let’s briefly cover why you might want to learn and use Go:

  • Go is simple, yet powerful, designed with the modern developer in mind.
  • Offers great performance, comparable to C or C++, and includes garbage collection.
  • It compiles quickly and includes built-in support for concurrent programming.

Prerequisites

To follow this guide, you'll need:

  • A system running Ubuntu (20.04 or later versions are preferred).
  • Admin or superuser privileges.
  • Basic knowledge of terminal commands in Linux.

Step 1: Installing Go

Let’s start with installing Go. Follow these simple steps:

  1. Update Your System: Start by ensuring that your package list is up to date.
sudo apt update && sudo apt upgrade
  1. Download the Go Tarball: Head to the official Go download page and copy the link to the latest Linux archive. Use wget to download it:
wget https://golang.org/dl/go1.XX.X.linux-amd64.tar.gz
  1. Extract the Archive: Unpack the tarball in /usr/local directory.
sudo tar -C /usr/local -xzf go1.XX.X.linux-amd64.tar.gz

Step 2: Configure Go Environment

Configuring the Go environment is crucial for smooth execution:

  • Edit your profile:
nano ~/.profile
  • Add the following lines to include Go in your PATH and set GOROOT and GOPATH:
export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
  • Save changes and exit the editor (Ctrl + O, Enter, Ctrl + X).
  • Apply these changes to your current session:
source ~/.profile

Step 3: Verify the Installation

To verify that Go is installed successfully, run:

go version

It should display the version of Go installed.

Step 4: First Go Program

Now that you have Go installed, let's verify its functionality by creating a simple program:

mkdir -p $GOPATH/src/hello
cd $GOPATH/src/hello
// hello.go
package main

import "fmt"

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

In your terminal, run:

go run hello.go

If everything is set up correctly, you should see Hello, Go! printed on the console.

Troubleshooting Tips

If you encounter any issues, consider the following:

  • Double-check the PATH settings in your profile.
  • Ensure you've downloaded the correct version of Go for your system architecture.
  • Refer to the official Go installation guide for deeper troubleshooting steps.

Conclusion

Congratulations! You've successfully set up Go on your Ubuntu system and executed your first program. With Go up and running, you’re now ready to dive deeper into building efficient and high-performance applications.

Next Article: Go vs Node.js: Which is better?

Previous Article: How to set up and run Go in MacOS

Series: Getting Started with Golang

Golang

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant