...
Golang

Install Golang on Ubuntu: A Comprehensive Guide

Introduction

Go, also known as Golang, is a popular programming language developed by Google. It’s known for its simplicity, efficiency, and concurrency features, making it a preferred choice for building web applications, network services, and cloud-native tools. If you’re looking to delve into the world of Go programming, installing it on your Ubuntu system is the first step. This guide will walk you through the process of installing Go on Ubuntu in a straightforward and easy-to-understand manner.

Prerequisites

Before embarking on the installation journey, ensure you have the following prerequisites:

  • Ubuntu System: Ensure you have a running Ubuntu system, preferably the latest LTS (Long Term Support) version for stability.
  • Internet Connection: An active internet connection is necessary to download the Go installation package.
  • Sudo Privileges: You’ll need sudo privileges to execute certain commands during the installation process.

Step 1: Download the Go Installation Package

  1. Navigate to the Go Download Page: Open a web browser and visit the official Go download page: https://go.dev/doc/install
  2. Choose the Appropriate Package: Select the appropriate Go package for your Ubuntu system architecture. For 64-bit systems, the file name typically ends with linux-amd64.tar.gz.
  3. Download the Package: Click on the download link to download the Go installation package. Save the file to a convenient location on your Ubuntu system, such as the Downloads directory.

Step 2: Install Go

  1. Open a Terminal: Launch a terminal window to execute the installation commands.
  2. Change to Download Directory: Navigate to the directory where you saved the downloaded Go package. Use the cd command to change directories. For example, if the package is in the Downloads directory, use:

Bash

cd ~/Downloads
  1. Extract the Package: Extract the downloaded Go package using the tar command. Replace go1.18.linux-amd64.tar.gz with the actual filename of your downloaded package:

Bash

tar -xf go1.18.linux-amd64.tar.gz
  1. Move the Go Directory: Move the extracted Go directory to the appropriate location. The recommended location is /usr/local. Use the following command, replacing /path/to/extracted/go with the actual path to the extracted directory:

Bash

sudo mv /path/to/extracted/go /usr/local
  1. Set Environment Variables: Set the necessary environment variables for Go to function correctly. Open or create the /etc/profile file using a text editor:

Bash

sudo nano /etc/profile

Add the following lines to the end of the file, replacing /usr/local/go with the actual path to the Go directory if you installed it in a different location:

Bash

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

Save the file and exit the editor.

  1. Source the Updated Profile: Reload the updated profile to apply the environment variable changes:

Bash

source /etc/profile

Step 3: Verify Go Installation

  1. Check Go Version: Open a new terminal window and verify the Go installation by running the following command:

Bash

go version

This should output the installed Go version, confirming successful installation.

  1. Write a Sample Go Program: To further confirm the installation, create a simple Go program to test the functionality. Create a file named hello.go and add the following code:

Go

package main

import "fmt"

func main() {
  fmt.Println("Hello, World!")
}
  1. Run the Go Program: Save the hello.go file and compile it using the go build command:

Bash

go build hello.go

This will create an executable file named hello in the current directory.

  1. Execute the Program: Run the compiled program using the ./ command:

Bash

./hello

This should print “Hello, World!” to the console, confirming that Go is working correctly.

Conclusion

Congratulations! You have successfully installed Go on your Ubuntu system. With Go installed, you’re ready to embark on your Go programming journey. Explore the vast array of Go libraries, frameworks, and tools to build powerful and efficient applications. Remember to keep your Go installation updated to the latest version for security and performance enhancements.

Leave a Reply

Your email address will not be published. Required fields are marked *