Skip to main content

Installing Go on Ubuntu

Go, also known as Golang, is a powerful open-source programming language developed by Google. It is known for its simplicity, efficiency, and ability to build scalable applications. In this tutorial, we will guide you through the process of installing Go on Ubuntu.

Prerequisites

Before we begin, please ensure that you have access to an Ubuntu machine, either on a physical or virtual environment. Additionally, make sure you have administrative privileges or sudo access to install packages.

Step 1: Update the System

To start, let's update the package list and upgrade any existing packages on your Ubuntu machine. Open the terminal and run the following commands:

sudo apt update
sudo apt upgrade

This will ensure that your system is up to date with the latest security patches and software updates.

Step 2: Downloading Go

In this step, we will download the latest version of the Go programming language. Visit the official Go website (https://golang.org/dl/) to find the download page and locate the appropriate Linux distribution for your system.

At the time of writing this tutorial, the latest stable version is Go 1.17. Locate the download link for the Linux distribution and copy it.

Next, open the terminal and use the wget command to download the Go installation package. Replace [GO_DOWNLOAD_URL] with the actual download link.

wget [GO_DOWNLOAD_URL]

For example, if the download link is https://golang.org/dl/go1.17.linux-amd64.tar.gz, the command would be:

wget https://golang.org/dl/go1.17.linux-amd64.tar.gz

Step 3: Extracting and Installing Go

Once the download is complete, we need to extract the downloaded package and install Go on our system. Use the following command to extract the tarball:

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

This command will extract the Go files and place them in the /usr/local/go directory. This directory is the recommended location for installing Go, but you can choose a different location if desired.

Step 4: Setting Go Environment Variables

To use Go effectively, we need to set up the necessary environment variables. Open the terminal and run the following command to open the .profile file for editing:

nano ~/.profile

Add the following lines at the end of the file:

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

Save the file and exit the editor by pressing Ctrl+X, followed by Y, and then Enter.

To apply the changes, either restart your terminal or run the following command:

source ~/.profile

Step 5: Verifying the Installation

After completing the installation, let's verify if Go is installed correctly on your Ubuntu machine. Open a new terminal and run the following command:

go version

If Go is installed correctly, you should see the output showing the installed Go version, for example:

go version go1.17 linux/amd64

Step 6: Writing and Running a Simple Go Program

Now that Go is installed, let's write a simple "Hello, World!" program and run it to ensure everything is working as expected.

Create a new file named hello.go using your favorite text editor and add the following code:

package main

import "fmt"

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

Save the file and go back to the terminal. Navigate to the directory where you saved the hello.go file. Then, run the following command to build and execute the program:

go run hello.go

You should see the output:

Hello, World!

Congratulations! You have successfully installed Go on Ubuntu and executed a simple Go program.

Conclusion

In this tutorial, we covered the step-by-step process of installing Go on Ubuntu. We began by updating the system, downloading the Go installation package, extracting and installing Go, setting up the necessary environment variables, verifying the installation, and finally running a simple Go program.

With Go installed, you are now ready to explore the language and build powerful applications!