Linux and Golang logos on a white background

How to Install Golang on Linux: A Comprehensive Guide

Before proceeding with Golang installation, it’s essential to ensure that you have GCC (GNU Compiler Collection) installed on your Linux system. GCC is a fundamental prerequisite for compiling and building Golang programs.  If GCC is installed, your Linux system is fully equipped to facilitate the installation and operation of Golang.

Are you looking to start developing with the popular programming language, Golang, on your Linux system? Look no further! This step-by-step guide will walk you through the process of installing Golang on your Linux machine, setting up your environment, and creating and running your first Golang program. Whether you’re a seasoned programmer or just starting out, this guide has everything you need to get started with Golang on Linux.

What are the prerequisites for installing Golang on Linux?

Before diving into the installation process, there are a few prerequisites that need to be met in order to successfully install Golang on your Linux system.

A Linux Distribution

First and foremost, you will need a Linux distribution installed on your machine. While Golang is compatible with most Linux distributions, some may require additional steps during the installation process. It’s always best to check the official Golang documentation for any specific instructions for your particular distribution.

Sufficient Storage Space

Golang requires a minimum of 1GB of storage space for its installation. Make sure your Linux system has enough free space before proceeding with the installation.

Basic Terminal Knowledge

Some of the installation steps and setting up of environment variables will require the use of the terminal. While this guide will provide detailed instructions, having basic knowledge of using the terminal on your Linux system will be helpful.

How do I Install Golang on Linux with a Step-by-Step Guide?

Now that you have met all the prerequisites, let’s dive into the installation process for Golang on Linux.

Step 1: Download Golang

The first step is to download Golang onto your Linux system. Go to the official Golang website and navigate to the downloads page. Here, you will find the latest version of Golang available for download.

Alternatively, if you prefer to use the terminal, you can use the wget command to download the Golang binary directly onto your Linux system.

Step 2: Unpack the Binary

Once the binary is downloaded, you will need to unpack it using the terminal or a file manager. If you used the wget command, the binary will be located in the directory where you ran the command.

Step 3: Move the Binary to /usr/local

Next, you will need to move the unpacked binary to the /usr/local directory. This is the recommended location for Golang installations on Linux systems.

Step 4: Set up Environment Variables

In order to use Golang, you will need to set up some environment variables. Open your preferred text editor and add the following lines to your .bashrc file:

export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

Save the file and exit the editor. Then, run the following command to apply the changes:

source ~/.bashrc

This will set the $GOPATH variable to your home directory and add Golang’s bin folder to your system’s $PATH.

Step 5: Verify Golang Installation

To ensure that Golang was installed correctly on your Linux system, open the terminal and run the command go version. You should see the Golang version you just installed displayed in the output.

How do you set up Golang Environment Variables?

As mentioned earlier, setting up environment variables is an important step in the Golang installation process. These variables tell your system where to find Golang binaries and packages so that they can be used with ease. Let’s take a closer look at the two main environment variables needed for Golang development on Linux.

GOPATH

The $GOPATH variable is used to specify the location of your workspace, which contains all your Golang projects and packages. By default, the value of this variable is set to $HOME/go, but you can change it to any preferred location.

PATH

The $PATH variable tells your system where to look for executable files. Adding Golang’s bin folder to this variable ensures that you can run Golang commands from anywhere in your system without having to specify the full path to the binary.

A workplace with a computer on the screen of which there is a program code

How do you create and run a simple Golang program?

Now that you have Golang installed on your Linux system and an IDE or text editor set up, let’s create and run a simple “Hello World” program to test everything out.

Step 1: Create a Directory for Your Golang Workspace

First, create a directory in the location specified by your $GOPATH environment variable. This will be your workspace where all your Golang projects and packages will be stored.

Step 2: Set Up a New Golang Project

Next, you will need to create a new project inside your workspace directory. You can do this using your preferred IDE or text editor, or through the terminal using the go mod initcommand.

Step 3: Write Your Golang Program

In your project directory, create a new file called main.go and add the following code:

package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}

This is a basic “Hello World” program that will print the message to the terminal when executed.

Step 4: Build and Run Your Program

To build your program, open the terminal in your project directory and run the command go build. This will create an executable file in your project folder with the same name as your project.

To run your program, simply use the command ./<project-name>. In this case, it would be ./hello-world.

Congratulations! You have just created and run your first Golang program on Linux.

How do you manage Golang packages and dependencies?

Golang has a built-in package manager called go get which allows you to easily manage external dependencies for your projects. Here’s how you can use this tool to manage packages and dependencies in your Golang projects on Linux.

  • Installing Packages: To install a new package, simply use the command go get <package-name> in your project directory. This will download the package and its dependencies into your project’s go.modfile;
  • Updating Packages: To update a specific package to its latest version, use the command go get -u <package-name>. This will also update any dependencies associated with that package;
  • Removing Packages: If you no longer need a package in your project, you can remove it by using the command go clean -i <package-name>. This will remove the package and its dependencies from your project’s go.mod file;
  • Managing Dependencies: To view all the dependencies used in your project, you can use the command go list -m all. This will display a list of all the packages installed in your project along with their versions.

Common Issues and Troubleshooting Tips

While Golang installation on Linux is typically straightforward, there may be some common issues that you may encounter during the process. Here are a few troubleshooting tips to help you resolve these issues.

Permission Errors

If you encounter permission errors while trying to install Golang on your Linux system, try running the installation commands with sudo.

Package Installation/Update Failures

In some cases, package installations or updates may fail due to network issues or outdated package repositories. To fix this, try updating your package manager or using a different repository.

Missing Dependencies

If you encounter missing dependencies while running your Golang program, use the go getcommand to install them.