How to Install Golang on Ubuntu with Source|Snap|Wget

Posted January 10, 2024
How to Install Golang on Ubuntu with Binary File|Snap|Wget

Go, also known as Golang, is an open-source programming language developed by Google. It is designed to be simple, efficient, and suitable for building modern systems and applications. This article will guide you through the process of installing Go on Ubuntu, one of the most popular Linux distributions.

There are several methods to install Go on Ubuntu. We will cover the following methods, how to verify the installation and how to uninstall it:

  1. Install Golang From Binary File
  2. Install Go using the wget command
  3. Install Go using the Ubuntu package manager
  4. Install Go Using Snap
  5. Verify the Golang installation
  6. Test Go installation
  7. Uninstall Go

Install Golang From Binary File

  1. Visit the official Go downloads page: https://golang.org/dl/

How to Install Golang on Ubuntu with Source|Snap|Wget

  1. Download the latest version of the Go binary file for Linux by clicking on the appropriate link. For example, if you’re running Ubuntu on an AMD64 architecture, you would download the file named go1.20.1.linux-amd64.tar.gz.

  2. Open the terminal and navigate to the directory where you downloaded the Go binary file using the cd command:

    cd ~/Downloads
    

    Replace ~/Downloads with the actual path to the directory where you downloaded the file.

  3. Extract the downloaded archive using the following command:

    sudo tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz
    

    Replace go1.20.1.linux-amd64.tar.gz with the actual filename of the downloaded archive.

    This command extracts the Go binary files to the /usr/local/go directory.

  4. After the extraction is complete, you’ll need to update your system’s PATH environment variable to include the Go binary directory. Open your shell’s profile file (e.g., .bashrc, .zshrc, etc.) using a text editor:

    nano ~/.bashrc
    
  5. Add the following line to the end of the file:

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

    This line appends the Go binary directory (/usr/local/go/bin) to your system’s PATH.

  6. Save the file and exit the text editor.

  7. Source the updated profile file to apply the changes:

    source ~/.bashrc
    

    Now, the Go binaries should be available in your system’s PATH.

Install Go using the wget command

  1. Open the terminal.

  2. Use the wget command to download the Go binary file directly from the official Go downloads page:

    wget https://golang.org/dl/go1.20.1.linux-amd64.tar.gz
    

    Replace go1.20.1.linux-amd64.tar.gz with the appropriate filename for the latest version and your system architecture.

  3. Extract the downloaded archive using the following command:

    sudo tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz
    

    This command extracts the Go binary files to the /usr/local/go directory.

  4. Follow steps 5-8 from the “Install Golang From Binary File” section to update your system’s PATH and make the Go binaries available.

Install Go using the Ubuntu package manager

  1. Open the terminal.

  2. Update the package index to ensure you have the latest package information:

    sudo apt update
    
  3. Install the Go package from the official Ubuntu repositories:

    sudo apt install golang-go
    

    This command will install the latest version of Go available in the Ubuntu repositories.

    Note: The version of Go available in the Ubuntu repositories may not always be the latest release. If you need the latest version, it’s recommended to use one of the other installation methods.

  4. After the installation is complete, the Go binaries should be available in your system’s PATH.

Install Go Using Snap

Snap is a package management system for Linux distributions, including Ubuntu. You can install the latest version of Go using the Snap package manager.

  1. Open the terminal.

  2. Install the snapd package if it’s not already installed:

    sudo apt update
    sudo apt install snapd
    
  3. Install the Go package using Snap:

    sudo snap install go --classic
    

    The --classic flag is required to allow the Go snap package to have the necessary permissions to function correctly.

  4. After the installation is complete, the Go binaries should be available in your system’s PATH.

Verify the Golang installation

After installing Go using any of the above methods, you can verify the installation by checking the Go version:

go version

This command should output the installed version of Go.

Test Go installation

To test the Go installation, create a simple “Hello, World!” program.

  1. Open a text editor and create a new file named hello.go.

  2. Add the following code to the file:

   package main

   import "fmt"

   func main() {
       fmt.Println("Hello, World!")
   }
  1. Save the file.

  2. Open the terminal and navigate to the directory containing the hello.go file using the cd command:

    cd /path/to/directory
    

    Replace /path/to/directory with the actual path to the directory containing the hello.go file.

  3. Compile and run the Go program:

    go run hello.go
    

    You should see the output Hello, World! printed in the terminal.

Uninstall Go

If you need to uninstall Go from your Ubuntu system, follow these steps:

  1. Open the terminal.

  2. Remove the Go package using the appropriate method based on how you installed it:

    • For binary installation:

      sudo rm -rf /usr/local/go
      

      This command removes the Go installation directory /usr/local/go.

      Additionally, you should remove the PATH entry you added earlier by editing your shell’s profile file (e.g., .bashrc, .zshrc, etc.) and removing the line that exports the Go binary directory to PATH.

    • For package manager installation:

      sudo apt remove golang-go
      

      This command removes the Go package installed via the Ubuntu package manager.

    • For Snap installation:

      sudo snap remove go
      

      This command removes the Go package installed via Snap.

Conclusion

In this article, we covered multiple methods to install Go on Ubuntu, including using the binary file, wget command, Ubuntu package manager, and Snap. We also demonstrated how to verify the installation, test the Go installation, and uninstall Go if needed. With Go installed on your Ubuntu system, you can start exploring this powerful programming language and build your applications.

How to Install Golang on Ubuntu with Source|Snap|Wget

Written By:

Joseph Chege