February 6, 2023

How To Install GoLang On Ubuntu 20.04

Abhishek Thakur


Go is a statically typed, high-level programming language developed by Robert Griesemer, Rob Pike, and Ken Thompson at Google. In its syntax part it is similar to C language. It is often referred as GoLang because of its former domain golang.org, but it’s proper name is GO. Go was designed to improve programming productivity in the era of multicore, networked systems. The main objective behind this project is to address the criticism of other languages like C and C++ while keeping their core functionality. In this guide we will see how to install GoLang on Ubuntu 20.04, and compile the famous hello-world example.

Requirements

  • 99RDP VPS/Dedicated Server (Running Ubuntu 20.04)
  • Terminal Access

Steps To Install GoLang On Ubuntu 20.04

Firstly SSH into the server, and update all the repo and packages for your Ubuntu system. To do that run the given command in terminal.

# apt update && apt upgrade -y

Once the updates are finished head to your /home directory or any directory of your choice to download and extract the GoLang source file. Run the given command to download the .tar file.

# wget https://go.dev/dl/go1.20.linux-amd64.tar.gz

This will initiate the download, once it’s done we can extract the .tar file for the next step. To extract the file enter the given command in terminal.

# tar -C /usr/local -xvf go1.20.linux-amd64.tar.gz

Once the tar file is extracted you can check the files in /usr/local/go using the ls command.

# ls /usr/local/go

Install Go

 

Adding GoLang To The $PATH Variable

Next,we are going to add the Go directory path to our environment variables so that the system can catch GoLang codes. You can add the Go directory path to either the /etc/profile file for the system-wide installation or the $Home/.profile file specifically defined for the current user’s installation. Using a source code editor, open the /etc/profile file as follows:

# nano /etc/profile

Next add the go PATH at the end of the file like this and save the file.

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

Adding GoLang to Path

Next reload the profile file using the given command. Now we can also check the GoLang installed version to verify it’s installation.

# source /etc/profile

# go version

Check Go version

 

Running The Famous hello-world

Now as the GoLang is installed and the path variables are set we can run the hello-world application. Create a directory named hello-world  and browse to that directory using the cd command.

# mkdir hello-world

# cd hello-world

When importing packages in GoLang, you have to manage dependencies through modules in the code itself. To do this, run the given command in terminal.

# go mod init example/hello-world

Now create a hello-world.go file using an editor of your choice. Then enter the given commands in the file and save it.

// You can edit this code!
// Click here and start typing.
package main

import "fmt"

func main() {
	fmt.Println("Hello, Welcome to 99RDP.COM")
}

Once the file is saved we can run it using the given command in terminal. This will print the given output in terminal.

# go run hello-world.go

Hello-World Output

Conclusion

In this guide we explained how to to install GoLang in Ubuntu 20.04. We also ran our first hello-world program to see the basic working of Go modules and files. You now have a system to use for Go development.


But Dedicated Root Server @ $89/m Only

Popular Blog Posts