Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

How to Install Go Language on Ubuntu

How to Install Go Language on Ubuntu

Google developed the Go programing language to create a language that is as powerful as C++ but follows simple syntax like Python.

And because of its efficiency, platforms such as Docker, Kubernetes, and Terraform are written in the Go language.

If you are writing code in Go or compiling a Go-based application, you must install it first.

In this tutorial, I'll show the following ways of installing Go on Ubuntu:

  • Using apt from Ubuntu repositories: Simplest option but not the latest version.
  • Using snap: Simple and often newer version.
  • Using source code: Complex method that gives you the latest version.

Let's start with the simplest and most recommended method.

If you don't care about having the most recent version, it can be installed with the following command:

sudo apt install golang

You can check if it has been installed, using the following command:

go version

While writing, you get Go version 1.18.1 which is one point release behind the most recent version:

How to Install Go Language on Ubuntu

Great! So you have checked that Go is installed on your Ubuntu system. But is it working correctly? Let's check that by running a simple Go program.

Run the Simple Go program

Here, I am going to create a simple Hello world program to cross-check the installation.

I am going to use nano to create and open the file named Hello_world.go:

nano Hello_world.go

And my file contents are the following (don't judge me, I'm pro okay):

package main

import "fmt"

func main() {
        fmt.Printf("Fingers crossed\n About to print Hello world!\n")
}
How to Install Go Language on Ubuntu

Save the file contents and exit Nano by pressing Ctrl + X.

Now, you will have to append the run flag and filename to the command to run the program:

go run Hello_world.go
How to Install Go Language on Ubuntu

Running as expected!

You could also build an executable Go file like this:

go build Hello_world.go

That was simple. You have Go language installed properly. Let's quickly see the removal steps.

Remove Go from Ubuntu

Since you installed it from Ubuntu's repositories using the apt command, the removal is pretty simple.

sudo apt remove golang

You can also run the autoremove afterward.

sudo apt autoremove

Method 2:  Using snap to install a newer version of Go

This is the easiest way to get the most recent version of any package as you don't have to go through any complex rather than executing one command.

And Ubuntu comes pre-configured with snaps so why not to benefit from this? Snap can be easily installed on other distributions as well.

How to Install and Use Snap in Various Linux Distributions
Snaps are Canonical’s way of providing a cross-distribution package management system. In this article, we will see how to install and use snaps in various Linux distributions.
How to Install Go Language on Ubuntu

To install the recent version of Go with snap, use the following command :

sudo snap install go --classic
How to Install Go Language on Ubuntu

And as you can see, it installed Go language version 1.19.5 which is the most recent stable release while writing.

Remove the snap Go

If you no longer need Go language installed on your system or you are just not into using snaps anymore and want to remove it, follow the given command:

sudo snap remove go

To crosscheck whether it is removed successfully, check the installed version:

go version
How to Install Go Language on Ubuntu

And if you get the same error, Go has been removed successfully.

🚧
This is the most complicated method and I do not recommend this. I included it for informational purposes.

To build Go from source, visit its official download page and choose the package for your system accordingly.

How to Install Go Language on Ubuntu

Next, extract the binaries to the /usr/local directory using the tar command:

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

Next, you are required to set the environment variable so that system can figure out where to look for the Go executable.

So first open the /etc/profile using the following command:

sudo nano /etc/profile

And press Alt + / to jump to the end of a line in the nano text editor and paste the following line:

export PATH=$PATH:/usr/local/go/bin
How to Install Go Language on Ubuntu

Now, save changes and exit from the nano text editor.

To take effect from the environment path you have just added, you will have to source the file:

source /etc/profile

Go has successfully been installed on your system. You can check the installed version:

go version
How to Install Go Language on Ubuntu

How to uninstall

Removing packages installed from the source code is even more complicated. But you can remove the Go from your system by following three easy steps.

First, remove the Go binaries that you extracted while installing:

sudo rm -rf /usr/local/go

Second, remove the environment path variable form /etc/profile:

sudo nano /etc/profile
How to Install Go Language on Ubuntu

And third, source the file to apply the changes:

source /etc/profile
How to Install Go Language on Ubuntu

Looking for an open-source IDE to run Go programs?

So you learned to install Go on Ubuntu Linux. You also ran a simple Go program. But if you have to write programs in Go, you'll be better off with a proper code editor.

I use VSCode for my daily coding tasks but this can't be the same for you.

So we have compiled a list of modern open-source IDEs on which you can run your Go programs efficiently:

7 Best Modern Open Source Text Editors For Coding in Linux
Looking for the best text editors in Linux for coding? Here’s a list of the best code editors for Linux. The best part is that all of them are free and open-source software. If you ask experienced Linux users, their answers would probably include Vim, Emacs, Nano, etc. No
How to Install Go Language on Ubuntu

And if you are a VSCode user like me and looking for an alternative, you can use the VSCodium 100% open-source version of Microsoft VSCode.

Please use the comment section for any queries related to this guide or suggestions on what I should cover next.

Enregistrer un commentaire

0 Commentaires