Linux

How to Install and Use Go on Ubuntu

How to Install and Use Go on Ubuntu

Go, often referred to as Golang, is a popular open-source programming language created by Google. It is known for its simplicity, efficiency, and speed, making it a great choice for building reliable and efficient software. If you’re a developer looking to install Go on your Ubuntu system, this guide will walk you through the process step by step. We will also cover some basics on how to set up and use Go for your projects.

Prerequisites
Before installing Go on your Ubuntu system, ensure you have the following:

  • A system running Ubuntu 18.04 or later.
  • A user account with sudo privileges.
  • Internet connection to download the Go package.

Step 1: Update Your System
Before installing any new software, it’s always a good idea to make sure your system is up to date. Open your terminal and run the following command:

sudo apt update && sudo apt upgrade -y

This will update the list of available packages and their versions and install the newest versions of all packages currently installed on your system.

Step 2: Download the Latest Go Version
Visit the official Go downloads page to check the latest version of Go. At the time of writing, the latest stable version is 1.21.1. You can download the .tar.gz file using the wget command:

wget https://go.dev/dl/go1.21.1.linux-amd64.tar.gz

Make sure to replace go1.21.1.linux-amd64.tar.gz with the actual version you want to download.

Step 3: Extract the Go Tarball
After downloading, you need to extract the tarball to the /usr/local directory. Use the following command:

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

This command will install Go in the /usr/local/go directory, which is the standard location for Go installations.

Step 4: Set Up Environment Variables
To make Go accessible system-wide, you need to set up the environment variables. Open your .profile, .bashrc, or .zshrc file using a text editor:

nano ~/.profile

Add the following lines to the file:

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

This command adds Go’s binary directory to your system’s PATH. Save the changes and close the editor. To apply the changes, run:

source ~/.profile

Step 5: Verify the Installation
After setting up the environment variables, you should verify whether Go was installed correctly. Run:

go version

If installed correctly, you will see an output showing the version of Go you have installed, similar to:

go version go1.21.1 linux/amd64

Step 6: Create Your First Go Program
Now that you have installed Go, let’s create a simple program to test if everything is working properly. Follow these steps:

Create a directory for your project:

mkdir ~/go-projects/hello

Move into the project directory:

cd ~/go-projects/hello

Create a file named hello.go:

nano hello.go

Add the following code:

package main

import “fmt”

func main() {
fmt.Println(“Hello, World!”)
}
Save and close the file.

Step 7: Run Your Go Program
To compile and run your program, use the go run command:

go run hello.go

You should see the output:

Hello, World!

Congratulations! You’ve successfully written and executed your first Go program.

Step 8: Setting Up a Go Workspace
Go organizes code in a specific way. It’s helpful to set up a workspace to keep your projects organized. The default workspace is located at ~/go. To create a new workspace, run:

mkdir -p ~/go/{bin,pkg,src}

  • bin: Stores compiled binaries.
  • pkg: Stores package objects.
  • src: Stores source files.
    Make sure to set the GOPATH environment variable by adding this line to your .profile:

export GOPATH=$HOME/go

Don’t forget to update your PATH:

export PATH=$PATH:$GOPATH/bin

Step 9: Update and Uninstall Go

  • Update Go
    When a new version of Go is released, you might want to update it. You can simply remove the old Go version and install the new one:

sudo rm -rf /usr/local/go

Then, repeat the download and extraction process using the latest version from the Go website.

Uninstall Go
If you need to remove Go from your system entirely, run:

sudo rm -rf /usr/local/go

Remove any lines referring to Go from your .profile or .bashrc, and update your environment:

source ~/.profile

Conclusion
Go is a versatile language that can be used for web development, system programming, and even machine learning applications. In this guide, we covered how to install Go on Ubuntu, set up environment variables, and create a simple Go program. Following these steps will have you ready to start coding in Go and building your projects.

By understanding the setup process and using Go’s workspace effectively, you can ensure your projects are well-organized and easy to manage.

Related Articles

Leave a Reply

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

Back to top button