Modern technology has made developing and managing your applications with the help of various frameworks, like Go (Golang), easier than ever. But would you like to fill in that curiosity? Why not install Go on Ubuntu? Worry not; this tutorial got you covered!
Go allows developers like yourself to write reliable code quickly and efficiently. And in this tutorial, you will learn to install Go on Ubuntu and compile a Go program to test your Go framework.
Read on and develop your Go environment today!
Prerequisites
Below are a few things you need in place before taking advantage of Go:
- An Ubuntu system with at least version 18.04 or higher – This tutorial uses Ubuntu 20.04.
- A user account with sudo rights on your Ubuntu system.
Enforcing the APT Package Manager to Install Go on Ubuntu
Being an open-source programming language, Go has quickly become prevalent among developers. Besides downsizing compile time of programs, you can use Go for various applications, whether for cloud or server-side applications or AI and robotics.
But like any other language, you must install Go on your system. In this case, you will first install Go via the APT package manager. This approach facilitates Go’s direct download and installation from Ubuntu’s official repositories.
To install Go on your Ubuntu, follow these steps:
1. Open your terminal, and run the apt update
command below to update the APT package repository to ensure the latest versions of packages are available.
sudo apt update -y
2. Next, execute the following command to download and install
Go (golang
) from Ubuntu’s package repository.
sudo apt install golang -y
3. Lastly, run the below command to verify Go is successfully installed by checking its version.
go --version
You will see the installed Go version below, confirming that the installation was successful.
Installing Go via the Latest Binary Release
Although the apt package manager simplifies Go’s installation process, it may not always provide the most up-to-date version. Another method to install Go is by installing its binary directory from Golang’s website, which ensures access to Go’s latest release.
Go’s binary is a pre-compiled version of the Go compiler and runtime environment. This compiled binary can be executed on your specific system without compiling the Go source code yourself.
To install Go via its binary:
1. Open your preferred web browser, and head over to the Golang website.
2. Next, look for the version compatible with your Ubuntu machine, and copy the download link for your system’s architecture and the hash value.
At this time of writing, the latest version for 64-bit Ubuntu machines is go1.21.0.linux-amd64.tar.gz.
3. Now, run the below wget
command to download the binary to your working directory. Ensure you replace the URL below with the one you copied in step two.
sudo wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz
4. Once downloaded, execute the following sha256sum
command to verify the file’s (go1.21.0.linux-amd64.tar.gz
) integrity.
Checking the hash value of the downloaded package helps ensure that the file has not been maliciously modified or corrupted during transit.
sudo sha256sum go1.21.0.linux-amd64.tar.gz
Compare the hash value in the output below with the one you noted in step two. If both hash value match, your file is intact. Otherwise, you must re-download the binary.
5. Now, run the below command to extract the contents of the downloaded binary (go1.21.0.linux-amd64.tar.gz
) into the /usr/local/
directory.
This command does not provide output to the terminal but unpacks the archived files and directories, making them accessible and usable on your system.
After extraction, the Go executable files, like the compiler and other Go tools, are located in the bin
subdirectory of /usr/local/go/
.
sudo tar -xzf go1.21.0.linux-amd64.tar.gz -C /usr/local/
6. Open the ~/.profile file in your preferred editor. This file is a system-wide configuration file that sets environment variables for your users on the system.
7. Next, add the following line at the bottom of the ~/.profile file, save the changes, and close the editor.
This line modifies the PATH
variable to include the bin
directory of your Go installation. This modification lets you run Go commands from any terminal window without specifying the Go executable’s full path.
export PATH=$PATH:/usr/local/go/bin
8. Execute the below source
command to update your system’s environment variables with the new $PATH
setting.
This command has no terminal output but applies your changes to the ~/.profile
file into the current shell session.
source ~/.profile
9. Finally, run the following go
command to verify that Go is correctly installed.
go version
If all goes well, you will see Go’s version installed, which should match Go’s binary version you downloaded in step three (go1.21).
Creating a Project to Test the Go Framework
Post-installation, you must take a crucial step to ensure the Go framework on your system is in force. How? In this example, you will create and run a basic Hello World program to ensure your GO installation works correctly.
1. Execute the following command to create (mkdir
) and move (cd
) to a new Go project directory named my-go-project
(arbitrary). This directory is where you will create and save all your GO source code and program.
mkdir my-go-project && cd my-go-project
2. Next, run the go
command below to initialize (init
) a module (mod
) for your Go project. A module is a collection of Go packages that are versioned together.
Go introduced a “Go modules” feature to manage dependencies and versioning within projects. This feature allows you to manage your project’s dependencies more effectively and ensures your project clearly understands the dependencies it requires.
The ata.com/hello
argument is the module path, typically a URL-like path uniquely identifying your module. Change this module path with the import path of your project or a custom path that suits your organization or project structure.
go mod init ata.com/hello
3. With a module initialized, create a source code file named hello.go
with your preferred editor, add the following code to the file, and save it.
The code below is a program that demonstrates the basic structure of a Go program and prints a message to your terminal.
// Define the package name, a collection of related functions, variables, and types.
package main
// Import the fmt package for input and output operations.
import "fmt"
// The main function - starting point of the program's execution.
func main() {
// Print a message to the terminal and move the cursor to the next line.
fmt.Printf("Hello world! This is my first GO program\n")
}
💡 Even though modeled after the C programming language, Go was inspired by Python’s productivity and relative simplicity.
4. Now, run the following command to build
(compile) your source file and create a binary file with the same name as your source file (hello).
This command tells Go to read your source code, resolve imports, check for errors, and generate machine code for the specified source file. The result is an executable binary file you can run on your system.
Compiling your source code into an executable binary makes sharing your program with others easier without sharing the original source code. Moreover, the compiled binary file runs independently on compatible systems.
go build
If no errors are found, the command does not provide output to the terminal, as shown below.
5. Finally, execute the command below to run the binary file (hello
), which starts your program and executes the code written inside the main
function.
./hello
Successfully running the program prints the message below to your terminal, which confirms your Go installation is working correctly.
Congratulations! You have successfully written and compiled your first Go program on Ubuntu.
Conclusion
Throughout this tutorial, you learned how to install Go on your Ubuntu machine and compile your first Go program. You can now take advantage of Go’s powerful features and performance and develop your Go applications.
Now, why not enhance collaboration by using version control systems like Git to monitor changes in your Go project? Take your Go knowledge further; explore the official documentation and additional resources available!