Go, often referred to as Golang, is an open-source programming language created by Google. It was designed by Robert Griesemer, Rob Pike, and Ken Thompson and first introduced in 2009.
Go (Golang) is a flexible programming language that can be used in a wide range of applications and software, like:
- Web Applications
- Command-Line Tools
- Network Applications
- Microservices
- Data Processing and Pipelines
- Distributed Systems
- System Tools and Utilities
- Blockchain and cryptocurrency applications
- DevOps tools, and many more.
Isn’t it fascinating? Once you explore the features section, you’ll understand the true power of this tool. Additionally, this article provides practical examples for installing Go (Golang) on your preferred Linux distribution.
Tutorial Details
Description | Installation of Go (Golang) with Practical Examples |
Difficulty Level | Moderate |
Root or Sudo Privileges | Yes |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | – |
Internet Required | Yes |
Key Features
Until now, you know what you can do with Go (Golang), but the whole fame of this application depends on the following features:
- Simplicity
- Concurrent and Efficient (utilizes goroutines (lightweight threads) and channels to make programs efficiently utilize modern multicore processors)
- Fast Compilation
- Static Typing (variable types are checked at compilation, enhancing type safety and catching errors early in the development process)
- Garbage Collection (automatically manages memory allocation and deallocation)
- Built-in Tools
- Cross-platform
Let’s roll to the next section to learn how to install this amazing tool on your preferred Linux distribution.
How to Install Go (Golang) on Linux
Go (Golang) can be easily installed from the distributions default package manager, but the default repositories might be outdated. So, it is always recommended to install the most recent version of Go from the source code.
Although, I will show you the installation steps for both ways: package manager and source code, starting with:
Installing Go using Package Managers
Go (Golang) is available in the default repositories (but might be outdated) of most modern Linux distributions and can be easily installed using the distributions default package manager.
The following are examples of Go installations on some well-known Linux distributions:
$ sudo apt install golang -y #Debian, Ubuntu, or Pop!_OS
$ sudo dnf install golang -y #Fedora
$ sudo yum install golang -y #Red Hat, CentOS
$ sudo pacman -Sy go #Arch, Manjaro, or EndeavourOS
$ sudo zypper install golang -y #openSUSE
Once the installation has successfully completed, check the Go version by using the following command:
$ go version
Output:
As you can see from the above picture, the “Go 1.18.1 version” is installed in the system, but while I am writing this article, the “Go 1.21.0 version” is out and can be easily installed from source.
Installing Go from Source
To install Go from source on your Linux system, follow these steps:
1. First, visit the official Go website (https://go.dev/dl/), find and download the latest stable version of Go. For example, while I was writing this article, “Go 1.21.0” was the latest version. For that, I’ll download the following package:
Ensure the package is suitable for your architecture and operating system. However, if you want to follow this guide, simply execute the following command to download “Go 1.21.0” using the wget command:
$ wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz
2. Now, navigate to the directory where you downloaded the Go tarball. If you are already there, proceed to extract it using the below command, directing the files to the “/usr/local/
” path.
/usr/local/go
” directory (if it exists).$ sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go*.linux-amd64.tar.gz
3. To make Go accessible from anywhere, you need to add the following line to your “/etc/profile
” (for system-wide installation) or “~/.profile
” (for user-specific installation).
export PATH=$PATH:/usr/local/go/bin
Then save the file and reload the shell configuration file by restarting the terminal session, or execute the following source command if you added the above line in the user-specific configuration file:
$ source ~/.profile
4. Now, you need to create the workspace directory and set the values for the “GOROOT
“, “GOPATH
” and “GOBIN
” Go environment variables in your user profile.
So, first create the Go workspace by creating a “~/go_projects
” directory, which is the root of your workspace. Inside the workspace, there will be three more directories, namely:
bin
: Contains Go executable binaries.src
: Contains Go source files.pkg
: Contains package objects.
Execute the following command to create the workspace, including the aforementioned directory tree:
$ mkdir -p ~/go_projects/{bin,src,pkg}
$ cd ~/go_projects
$ ls
Output:
5. Then, set values for the “GOROOT
“, “GOPATH
” and “GOBIN
” Go environment variables in your user profile (ref: “~/.profile
“) pointing towards your workspace directory.
export GOROOT=/usr/local/go
export GOPATH="$HOME/go_projects"
export GOBIN="$GOPATH/bin"
6. Lastly, save the file and reload the changes made in the shell configuration file by restarting the terminal session or executing the following command:
$ source ~/.profile
7. Once you have perfectly completed all the aforementioned steps, execute the following command to view your Go version and environments:
$ go version
$ go env
Output:
Testing Go (Golang) with a Sample Program
To test if the Go installation is working correctly, let’s create a basic “Hello, World!
” Go program. So, follow the following steps one-by-one:
1. First, create a hello project directory under the “~/go_project/src/
” path.
$ mkdir -p ~/go_projects/src/hello
2. Then, using your preferred text editor, create the “hello.go
” file:
$ vim ~/go_projects/src/hello/hello.go
3. Now copy and paste the following content into the aforementioned file (ref: “hello.go
“):
package main
import "fmt"
func main() {
fmt.Println("Hello, LinuxTLDR!")
}
4. Now, compile the above program using “go install
” and run it:
$ go install ~/go_projects/src/hello/hello.go
$ $GOBIN/hello
Output:
Congratulations!!! You have successfully installed and run your first Go program on Linux. Note that Go also provides you with some useful built-in tools that you can use, like:
go build
: Compile Go programs.go test
: Run tests for your Go code.go fmt
: Format your Go code.go get
: Fetch and install Go packages from the internet.go doc
: Access documentation for Go packages.
And if you want to run Go binary executables like other Linux commands, then add “$GOBIN
” to your $PATH environment variable.
But when you do so, ensure the program name does not conflict with the Linux built-in commands, like “ls
“, as Linux already uses the ls command to list files and directories.
Updating Go (Golang) Installed from Source
If you have installed Go using the system’s default package manager, you can simply update your system. However, if you have installed Go from source, then you only have to follow the 1st and 2nd steps from the Installing Go from Source section.
Whereas, the first step involved visiting the Go website (https://go.dev/dl/) and downloading the latest Go version suitable for your architecture and operating system.
Then, execute the following command to remove the existing Go installation by deleting the “/usr/local/go
” directory (if it exists) and extracting the latest downloaded tarball, directing the files to the “/usr/local/
” path.
$ sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go<VERSION>.linux-<ARCH>.tar.gz
How to Remove Go (Golang) from Linux
To remove Go from your Linux system, you can either use the distributions default package manager (if Go was installed using the package manager) or manually remove the Go installation directory (if Go was installed from source).
Removing Go installed using the distribution default package manager:
$ sudo apt remove golang -y #Debian, Ubuntu, or Pop!_OS
$ sudo dnf remove golang -y #Fedora
$ sudo yum remove golang -y #Red Hat, CentOS
$ sudo pacman -Rs go #Arch, Manjaro, or EndeavourOS
$ sudo zypper remove golang -y #openSUSE
Removing Go installed from source:
$ sudo rm -rf /usr/local/go && rm -rf ~/go_projects
Then remove the following environment variables from the shell configuration file where you added them during installation:
export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH="$HOME/go_projects"
export GOBIN="$GOPATH/bin"
Final Word
I hope you find this article useful, and if you find something missing or have a suggestion to add something to this article, then do let us know via the comment section.
Till then, peace!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.