Go, also known as Golang, is a programming language created by Google. It simplifies coding and provides great performance, which makes it popular among developers. Its user-friendly syntax and built-in concurrency support allow developers to efficiently create scalable applications. Moreover, Go is a cross-platform programming language and is available for all major operating systems, including Linux, Windows, etc.
In this article, we’ll discuss what Golang is and how to install and use it on Ubuntu.
Golang is a simple yet powerful programming language that enables developers to build basic to advanced applications. Go was developed to manage large-scale projects, with a focus on concurrency and clean code. Its scalability and cross-platform compatibility have made it a popular choice among developers.
Go is widely used in systems programming, networking, and microservices. Key benefits of Golang include fast compilation, simple syntax, automatic memory management, and tools like go fmt
and go test
.
To install the Go language on Ubuntu, the below-listed requirements must be fulfilled:
If a user fulfills the prerequisites, he can proceed with the installation of Go on Ubuntu.
Apt is the default package manager of Ubuntu. Installing Golang through the default package is the most convenient way. However, it’s important to note that the version installed via Ubuntu's default package manager may not always be the latest.
Let’s go through the below-listed steps to install Go on Ubuntu through apt
:
Before installing Golang on Ubuntu, it is recommended to update and upgrade the system repositories to ensure we have the latest package information:
sudo apt update && sudo apt upgrade -y
Now run the command below to install Go on Ubuntu using the default package manager:
sudo apt install golang-go -y
The installation process for Golang on Ubuntu may take a while to complete.
Finally, confirm the Golang installation on Ubuntu by checking its version:
go version
The output indicates that the go version go1.22.2
has been installed successfully:
If Golang is no longer needed, uninstall it from Ubuntu by executing the following command:
sudo apt remove golang-go -y
It's recommended to remove unnecessary dependent packages, as they can take up extra space. To do this, simply execute the below-given command:
sudo apt autoremove
To install the Go programming language on Ubuntu from the official source, users can use the wget
command. This command downloads the latest Golang binary package from the official website.
Let’s go through the step-by-step process below to download and install Golang on Ubuntu using the wget command.
First, open a browser and navigate to the Go language’s official All Releases page. Scroll down to select an appropriate binary package under the stable version. Copy the link address of the selected binary package:
Now specify the copied link address with the wget
command, as shown below:
wget https://go.dev/dl/go1.23.2.linux-amd64.tar.gz -O golang.tar.gz
Here, the -O
option is used to specify an output file name. For instance, the above command downloads the Golang binary package and saves it as golang.tar.gz
:
After this, use the tar command to extract the downloaded package:
sudo tar -xzvf golang.tar.gz -C /usr/local
Configuring the PATH
variable for Golang allows access to Go commands and tools from any directory on the system. For this purpose, the Go binary paths need to be added to the PATH
environment variable:
echo export PATH=$HOME/golang/bin:/usr/local/go/bin:$PATH >> ~/.profile
Next, execute the following command to apply the changes made to the profile file:
source ~/.profile
Once the Go environment is configured, execute the command below to check if Go has been successfully installed:
go version
In Golang, the default workspace directory is ~/go
, and the environment variable $GOPATH
refers to this location. However, users can customize this workspace by setting the $GOPATH
environment variable in their ~/.profile
, ~/.bashrc
, or ~/.zshrc
files:
export GOPATH=$HOME/golangExamples
This command will set the workspace directory to ~/golangExamples
. Moreover, users can also set $GOROOT
to override the default Go installation path. However, this is usually not necessary:
export GOROOT=/usr/local/go
Finally, execute the command below to apply the changes:
source ~/.profile
Users can customize the
$GOPATH
and$GOROOT
environment variables, however, it is not recommended to change them unless you have a specific reason.
If Golang is downloaded using the wget
command and now needs to be removed, simply run the command below:
sudo rm -rf /usr/local/go
This command will permanently remove the Go language from Ubuntu.
Once the Go programming language is installed on Ubuntu, we can use it to fulfill our programming needs, such as creating applications, and web services, handling concurrent tasks, etc.
You can use any text editor, like nano
, to create a new file and save it with a .go
extension:
nano exampleCode.go
After creating a go file, paste the following code into it to print a "Welcome to hostman.com" message on the terminal:
package main
import "fmt"
func main() {
fmt.Println("Welcome to hostman.com")
}
Save the code, close the nano
editor, and then run the command below in the terminal to execute the Go program:
go run exampleCode.go
The output shows that the "Welcome to hostman.com" message is successfully displayed in the Ubuntu terminal, which confirms that the Golang program has been executed correctly:
That’s all about installing, configuring, and using the Go programming language on Ubuntu.
Golang is a cross-platform programming language best known for its easy-to-use syntax, concurrency support, and scalability. Key benefits of Golang include fast compilation, simple syntax, and built-in tools like go fmt
and go test
, etc. These features make Golang a perfect choice for building efficient applications. In this article, we explored two methods to install Golang on Ubuntu. The first method uses the default package manager (apt
). The second method involves downloading via wget
. Choose any of the discussed methods to install the Go language and start using it to develop scalable applications on Ubuntu.