How to Install Golang on Ubuntu with Source|Snap|Wget
Posted January 10, 2024
Go, also known as Golang, is an open-source programming language developed by Google. It is designed to be simple, efficient, and suitable for building modern systems and applications. This article will guide you through the process of installing Go on Ubuntu, one of the most popular Linux distributions.
There are several methods to install Go on Ubuntu. We will cover the following methods, how to verify the installation and how to uninstall it:
- Install Golang From Binary File
- Install Go using the wget command
- Install Go using the Ubuntu package manager
- Install Go Using Snap
- Verify the Golang installation
- Test Go installation
- Uninstall Go
Install Golang From Binary File
- Visit the official Go downloads page: https://golang.org/dl/
-
Download the latest version of the Go binary file for Linux by clicking on the appropriate link. For example, if you’re running Ubuntu on an AMD64 architecture, you would download the file named
go1.20.1.linux-amd64.tar.gz
. -
Open the terminal and navigate to the directory where you downloaded the Go binary file using the cd command:
cd ~/Downloads
Replace ~/Downloads with the actual path to the directory where you downloaded the file.
-
Extract the downloaded archive using the following command:
sudo tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz
Replace
go1.20.1.linux-amd64.tar.gz
with the actual filename of the downloaded archive.This command extracts the Go binary files to the
/usr/local/go
directory. -
After the extraction is complete, you’ll need to update your system’s PATH environment variable to include the Go binary directory. Open your shell’s profile file (e.g.,
.bashrc
,.zshrc
, etc.) using a text editor:nano ~/.bashrc
-
Add the following line to the end of the file:
export PATH=$PATH:/usr/local/go/bin
This line appends the Go binary directory (
/usr/local/go/bin
) to your system’s PATH. -
Save the file and exit the text editor.
-
Source the updated profile file to apply the changes:
source ~/.bashrc
Now, the Go binaries should be available in your system’s PATH.
Install Go using the wget command
-
Open the terminal.
-
Use the wget command to download the Go binary file directly from the official Go downloads page:
wget https://golang.org/dl/go1.20.1.linux-amd64.tar.gz
Replace
go1.20.1.linux-amd64.tar.gz
with the appropriate filename for the latest version and your system architecture. -
Extract the downloaded archive using the following command:
sudo tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz
This command extracts the Go binary files to the
/usr/local/go
directory. -
Follow steps 5-8 from the “Install Golang From Binary File” section to update your system’s PATH and make the Go binaries available.
Install Go using the Ubuntu package manager
-
Open the terminal.
-
Update the package index to ensure you have the latest package information:
sudo apt update
-
Install the Go package from the official Ubuntu repositories:
sudo apt install golang-go
This command will install the latest version of Go available in the Ubuntu repositories.
Note: The version of Go available in the Ubuntu repositories may not always be the latest release. If you need the latest version, it’s recommended to use one of the other installation methods.
-
After the installation is complete, the Go binaries should be available in your system’s PATH.
Install Go Using Snap
Snap is a package management system for Linux distributions, including Ubuntu. You can install the latest version of Go using the Snap package manager.
-
Open the terminal.
-
Install the snapd package if it’s not already installed:
sudo apt update sudo apt install snapd
-
Install the Go package using Snap:
sudo snap install go --classic
The
--classic
flag is required to allow the Go snap package to have the necessary permissions to function correctly. -
After the installation is complete, the Go binaries should be available in your system’s PATH.
Verify the Golang installation
After installing Go using any of the above methods, you can verify the installation by checking the Go version:
go version
This command should output the installed version of Go.
Test Go installation
To test the Go installation, create a simple “Hello, World!” program.
-
Open a text editor and create a new file named
hello.go
. -
Add the following code to the file:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
-
Save the file.
-
Open the terminal and navigate to the directory containing the hello.go file using the cd command:
cd /path/to/directory
Replace
/path/to/directory
with the actual path to the directory containing thehello.go
file. -
Compile and run the Go program:
go run hello.go
You should see the output Hello, World! printed in the terminal.
Uninstall Go
If you need to uninstall Go from your Ubuntu system, follow these steps:
-
Open the terminal.
-
Remove the Go package using the appropriate method based on how you installed it:
-
For binary installation:
sudo rm -rf /usr/local/go
This command removes the Go installation directory
/usr/local/go
.Additionally, you should remove the PATH entry you added earlier by editing your shell’s profile file (e.g.,
.bashrc
,.zshrc
, etc.) and removing the line that exports the Go binary directory to PATH. -
For package manager installation:
sudo apt remove golang-go
This command removes the Go package installed via the Ubuntu package manager.
-
For Snap installation:
sudo snap remove go
This command removes the Go package installed via Snap.
-
Conclusion
In this article, we covered multiple methods to install Go on Ubuntu, including using the binary file, wget command, Ubuntu package manager, and Snap. We also demonstrated how to verify the installation, test the Go installation, and uninstall Go if needed. With Go installed on your Ubuntu system, you can start exploring this powerful programming language and build your applications.