Golang is an open-source programming language developed the Google. It provides easy-to-build simple, reliable, and efficient software. This language is designed for writing servers, that’s why it is used widely these days. Go has released the latest version 1.20.
In this tutorial, you will get instructions to install Go 1.20 on your Ubuntu 22.04 LTS and Ubuntu 20.04 LTS Linux systems. Choose one the below methods for the installation of Go on your system.
Method 1: Installing Go from the Default System Repository
The official Ubuntu repositories may contain older versions of the Go programming language. At the time of writing this tutorial, the default repositories for Ubuntu have Go 1.13 available.
To install this version of Go on your Ubuntu system, follow these steps:
- Open a terminal window.
- Update the package list to ensure you have the latest information about available packages:
sudo apt update
- Install Go by running the following command:
sudo apt install golang
- Once the installation is complete, you can check the installed Go version by using the following command:
go version
You should see output similar to the following:
Outputgo version go1.13.8 linux/amd64
Please note that this method installs the version available in the default repository, which may not be the latest. If your application requires a more recent version of Go, you can explore alternative installation methods.
Method 2: Installing Latest Go with Source code
Here are the steps to download the latest Golang source code and set it up on your Ubuntu system:
Step 1: Update and Upgrade Ubuntu
Before installing new software, it’s always a good practice to update and upgrade your system. This ensures you have the latest security patches and dependencies. Open a terminal and run:
sudo apt-get update
sudo apt-get -y upgrade
Step 2: Download the Golang Installer
You can download the latest version of Golang from its official download site. However, for Ubuntu, the easiest method is to use the terminal. To download the latest stable version of Go, use:
wget https://go.dev/dl/go1.21.6.linux-amd64.tar.gz
Replace go1.21.6.linux-amd64.tar.gz
with the latest version available.
Step 3: Extract the Archive
Once the download is complete, extract the tarball. This will create a directory named go in the current directory.
sudo tar -xvf go1.21.6.linux-amd64.tar.gz -C /usr/local
Step 4: Set Go Paths
To use the Go command, you need to add /usr/local/go/bin
to your PATH variable. You can do this by editing your profile file.
echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile
source ~/.profile
Step 5: Verify the Installation
To ensure that Go is installed correctly, check its version:
go version
You should see the version of Go that you’ve installed.
Step 6: Setting Up Your Go Workspace
Go uses a specific directory structure for its projects. It’s a good idea to set up your Go workspace:
- Create a workspace directory:
mkdir ~/go
- Set the GOPATH environment variable:
echo "export GOPATH=$HOME/go" >> ~/.profile
source ~/.profile
Testing Your Setup (Optional)
- Create a simple “Hello, World!” program to test your setup.
- Create a new directory for your test project:
mkdir -p ${GOPATH}/src/hello
- Create a new file named hello.go with the following content:
package main import "fmt" func main() { fmt.Printf("Hello, World!\n") }
- Run the program:
go run ${GOPATH}/src/hello/hello.go
You should see “Hello, World!” printed in the terminal.
Conclusion
In conclusion, installing Go 1.21 on Ubuntu 22.04 and 20.04 is a straightforward process that involves downloading the latest version of Go, adding the Go binary to the PATH environment variable, and setting up your Go workspace. By following the step-by-step instructions provided in this guide, you should be able to easily install and start working with Go on your Ubuntu system. With Go’s speed and efficiency, you can develop powerful and scalable applications that meet your needs. We hope this guide has been helpful in getting you started with Go on Ubuntu.