install go in ubuntu

How to Install Go (Golang) in Ubuntu

Go (Golang) is a popular programming language created by Google that provides high concurrency support and multiprocessing capabilities. It also provides a robust collection of libraries. In this article, we will look at how to install go (Golang) in Ubuntu. You can follow these steps for installing Go in any Debian Linux.


How to Install Go (Golang) in Ubuntu

Here are the steps to install Go in Ubuntu.


1. Download Tarball

Open terminal and run the following command to download latest version of Go from its download page. In our case, it is 1.16.6.

$ sudo wget https://golang.org/dl/go1.16.6.linux-amd64.tar.gz


2. Extract Tarball

Extract downloaded tarball to /usr/local folder

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


3. Update Environment Variable

Add Go’s installation path to PATH environment variable to .bashrc file in /etc/profile. Open it in a text editor.

$ sudo vi ~/.bashrc

Add the following line to .bashrc.

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


4. Verify Go Installation

Verify Go installation with the following command.

$ go version


Alternative Installation

You may also install Go from snap store with the following command.

$ sudo snap install --classic --channel=1.16/stable go 


5. Test Go installation

Let us create a simple go script to output “Hello World” message. Run the following command to create an empty .go file. Go files have a .go extension.

$ sudo cat > hello-world.go

Add the following lines to the above file.

package main

import "fmt"

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

Save and close the file. Run the program with the following command.

$ go run hello-world.go


Remove/Uninstall Go in Ubuntu

If you need to remove or uninstall Go language in Ubuntu, just run the following commands. There are 3 steps involved in this case. First command removes the installation folder. The second command opens .bashrc file. After the second command you need to remove the PATH variable added in step 3 above. Finally, the last command updates the .bashrc file.

$ sudo rm -rf /usr/local/go
$ sudo vi ~/.bashrc        # remove the entry from $PATH
$ source ~/.bashrc

That’s it. In this article, we have learnt how to install Go language, create & run a simple test program using Go language, and also how to remove/uninstall Go in Ubuntu. Although Golang is newer than other programming languages like Python, Java, PHP, it offers good speed, clarity and scalability required for modern day applications and websites. It offers built-in testing modules, robust documentation. Since it is easy to learn, developers can quickly understand other people’s code and maintain it better. However, it does not have classes like other programming languages.

Also read:

How to Extract Substring from String in Bash
How to Split String into Array in Shell Script
How to Check if String Contains Substring in Shell
Shell Script to Remove Last N Characters from String
How to Find Unused IP Address in Network

Leave a Reply

Your email address will not be published. Required fields are marked *