This post may contain affiliate links/ads and I may earn a small commission when you click on the links/ads at no additional cost to you. As an Amazon Affiliate, I earn from qualifying purchases. Techsphinx also participates in the StationX Affiliate program. You can read my full disclaimer here.
Go, also known as Golang, is an open-source programming language developed by Google. It has become a popular choice among developers as it is commonly used for web development, building Cloud & Network Services and command line interface (CLI) programs.
Golang can be easily installed on Windows, MacOS and Linux. In this tutorial, I will show you how to install Go on Ubuntu. Moreover, I will also show you how to write your first Golang program and compile your code into an executable binary.
Pre-Requisites for Installing Golang on Ubuntu
- The Ubuntu system should be up and running. (I am using Ubuntu 22.04 here.)
- An account with sudo privileges is required.
- Internet connection to download Golang.
Install Go on Ubuntu
Installing Go using the APT package manager
One of the easiest ways to install Golang on Ubuntu is to use the APT package manager. This method will get you the stable version of Golang along with all its dependencies that is available in the Ubuntu repository.
1. Open up your terminal and run the following command to update your system’s package index:
sudo apt update
3. Now, run the following command to install Golang:
sudo apt install golang-go
Once the above command installs Go on Ubuntu, you can proceed to the “Verifying Golang installation on Ubuntu” section of this post.
Installing Golang from the official Go website
Another way to install Golang on Ubuntu is to download and install it from the official Golang website. This method is useful if you want to install the latest version of Golang or if you want to install a version that is not available in the Ubuntu repositories. Here’s how to do it:
1. Open up your web browser and go to the official Golang website.
2. Download the Golang binary for your architecture and operating system. For Ubuntu, you should download the Linux version.
You can also use the wget command to download the Golang.
wget https://go.dev/dl/go1.20.3.linux-amd64.tar.gz
The above command will download the “1.20.3” version of Golang which might not be the latest version of Golang when you’re reading this post. You can download the latest version of Go from the Golang website.
3. Once the download is complete, open up your terminal and navigate to the directory where you downloaded the Golang binary.
Note: Replace “go1.20.3.linux-amd64.tar.gz” with the filename you downloaded from here onwards.
4. Optionally, you can verify the checksum of the downloaded file using the following command:
sha256sum go1.20.3.linux-amd64.tar.gz
This should match the one you see on the Golang website.
5. Run the following command to extract the Golang archive. (I am extracting it in the “/usr/local” directory.)
tar -C /usr/local -xzf go1.20.3.linux-amd64.tar.gz
6. Finally, add the Golang binary directory to your system’s PATH by running the following command:
export PATH=$PATH:/usr/local/go/bin
You can make this change permanent by adding the same line to any of the following files:
~/.bashrc ~/.profile ~/.bash_profile
I am adding it to the “~/.profile” file.
Next, reload the .profile file using the source command:
source ~/.profile
Verifying Golang installation on Ubuntu
Once you have installed Golang on your Ubuntu system, you can verify that it is working correctly by running the following command:
go version
This should output the version of Golang that you have installed on your system.
Writing and running your first Golang program on Ubuntu
Now that you have successfully installed Go on Ubuntu, you can start writing Golang code. Here’s how to write and run your first Golang program on Ubuntu:
1. Create a new directory for your Go workspace and change to that directory.
mkdir hello
cd hello
2. The next step is to create a “go.mod” file. This file defines the module’s path and keeps track of the dependencies.
go mod init example/hello
Note: You can replace “example” in the above command with your own domain name.
3. Now, open up your favorite text editor and create a new file called “hello.go”. (I am using nano command-line text editor here.)
nano hello.go
4. Add the following code to the “hello.go” file:
package main
import "fmt"
func main() {
fmt.Println("Hello World!!")
}
5. Save the hello.go file and exit your text editor. (In Nano – use Ctrl + x, then type “y” and hit enter.)
6. Open up your terminal and navigate to the directory where you saved the hello.go file.
7. Use the following command to run your Golang program:
go run hello.go
This should output “Hello World!!” to your terminal.
Compile Go code into an Executable Binary
The “go run” command is useful when your project is work-in-progress. However, once your project is complete, then you can compile your code into a single executable binary file that contains all the necessary packages and dependencies that you have defined/used in your project.
To build the “hello.go” code we created previously, run the following command from the same “hello” directory.
go build
Now, if you use the “ls” command you’ll see that there is a new executable file called “hello”.
You can run the new hello file using the following command:
./hello
If you are seeing the “Hello World!!” output, it means the executable binary is working properly.
To make things even more interesting, let’s use the “go install” command. This command works in the same way as the “go build” command, however, it will also install the package, so that we can run the executable file from anywhere on the system.
To do that, first, we have to export the PATH where the “go install” command will install the executable file. Usually, it’s the “/go/bin” directory of the user.
export PATH=~/go/bin/
If you want to check the executable binary’s install path, then you can use the following command:
go list -f ‘{{.Target}}’
Now, you can run the following command to build and install your Go code on Ubuntu.
go install
Try running the “hello” command from anywhere on the system just like you run any other Linux command.
hello
If you’ve done everything correctly, you’ll see the “Hello World!!” output.
Uninstall Go from Ubuntu
Whether you want to remove an old version of Golang to install the new version or you just want to remove Golang from your Ubuntu system, this section will help you with the uninstallation process.
If you have installed golang using the apt package manager, then use the following commands to uninstall Go from Ubuntu:
sudo apt remove golang-go
sudo apt autoremove
If you have downloaded and installed Golang from the official website, then use the following command to remove golang from your system:
sudo rm -rf /usr/local/go
Note: The above command assumes that you have used the second method to install Go on Ubuntu and extracted golang in “/usr/local”. If you have used a different location to extract Go, then replace “/usr/local/go” with that location.
Conclusion
As you can see installing Golang on Ubuntu is a relatively straightforward process and I have shown you two different methods to do that. I have also shown you how to verify your Golang installation and how to write, run and build your first Golang program on Ubuntu.
If you are just starting out with Golang, then the official go language documentation can be of great help.
If you like this post, then follow Techsphinx on Facebook and Twitter for more reviews, tricks, tips and tutorials.
This article needs update or correction? Report the issue here so I can update it.