GOLANG

GO: How to Install Go on Ubuntu 18.04

Nishee Jaiswal
7 min readJun 29, 2021

An environment setup on our local machine is required so that we are able to run, build and compile Go projects.

The operating system which we will be working on is Linux and all the directory structure and folder path descriptions will be based on Ubuntu 18.04 which is a Linux distribution. This environment setup will be similar for other versions of Ubuntu as well.

Let’s start by installing Go on our local machine which is Ubuntu 18.04.

☛ Step 1: Install Go installer

Open your browser and go to the link https://golang.org/doc/install to get the Golang installer. Click on Download Go for Linux blue card to download the archive for Linux.

https://golang.org/doc/install

Now, open your terminal and check your pwd(present working directory). The pwd command will return /usr/username and on my local system, it will be /home/nishee.

nishee@ubuntu:~$ pwd
/home/nishee

Goto the path of the folder where the archive(tar file) gets downloaded and run ls command to check if the archive got downloaded inside the Downloads folder or not.

nishee@ubuntu:~$ cd Downloads
nishee@ubuntu:~/Downloads$ ls
go1.1......

The folder which gets downloaded will be something similar to this format go$VERSION.$OS-ARCH.tar.gzfolder name, here it is go1.16.5.linux-amd64.tar.gz.

Now, run the below command to extract the archive you downloaded into /usr/local, creating a Go tree in /usr/local/go.

nishee@ubuntu:~/Downloads$ sudo rm -rf /usr/local/go 
nishee@ubuntu:~/Downloads$ sudo tar -C /usr/local -xzf go1.16.5.linux-amd64.tar.gz

Here, the go1.16.5.linux-amd64.tar.gz archive is extracted into /usr/local . You can change the archive name according to the archive which you have downloaded. The go1.16.5 tells us that the go version which we are installing on our system is 1.16.5.

The sudo command is used because /usr/local is at the root of your local system. The rm-rf/usr/local/go will remove any folder by the name go and this way a previous installation of go is removed. tar -C /usr/local -xzf go1.16.5.linux-amd64.tar.gz unzips the tar file.

Now, go to the root directory to verify if the tar file is unzipped properly at the desired location, by executing the below commands.

nishee@ubuntu:~/Downloads$ sudo su
root@GS-4548:/home/nishee/Downloads#

Move out of the /home/nishee/Downloadsfolder and list the folders. The ls command will list all the folders in the root directory along with usr folder.

root@ubuntu:/home/nishee/Downloads# cd /
root@ubuntu:/# ls
bin usr etc sys ... ... ...

Go inside the /usr/local directory and list the files and directories inside it.

root@ubuntu:/# cd usr
root@ubuntu:/usr# cd local
root@ubuntu:/usr/local# ls
bin etc go ... ... ...

Here, you are now able to see the go folder inside /usr/local path.

To ensure that Go runtime is properly installed and to check the version of go installed on your local system, use the below command.

nishee@ubuntu:~$ go version
go version go1.16.5 linux/amd64

You are now ready with Go on your system. Now, all the necessary code and binaries needed for Go to function will be copied to /usr/local/go directory. This is where now Go’s command-line tools, standard library, and compiler will live. Whenever you import a package from Go’s standard library, Go looks for the package in /usr/local/go directory.

The actual Go runtime is now installed and this will allow you to build, compile and execute go code on your local system. But before that, you need to create a Go workspace.

☛ Step 2: Create Go Workspace

A workspace is Go’s way to facilitate project management. A workspace, in a nutshell, is a directory on your system where Go looks for source code files, manages dependency packages, and builds distribution binary files.

A Go workspace is by default set to $GOPATH environment variable. One can check the GOPATH environment variable value by executing the echo $GOPATHcommand on the terminal.

nishee@ubuntu:~$ echo $GOPATH
/usr/local/go

There are also other Go environment variables such as $GOROOT and $GOBIN. The default path set for these environment variables can also be checked by executing the echo command on the terminal.

nishee@ubuntu:~$ echo $GOROOT
/usr/local/go
nishee@ubuntu:~$ echo $GOBIN

$GOBIN is another environment variable that Go uses to put binary files created using the go installcommand. By default, $GOBIN is $GOPATH/bin but you can change it to whatever directory you like. Here, the $GOPATH is not set.

Firstly, we will create a go directory along with src ,pkg and bin folder inside the go directory in the /home/nishee folder .

nishee@ubuntu:~$ pwd
/home/nishee
nishee@ubuntu:~$ mkdir go
nishee@ubuntu:~$ cd go
nishee@ubuntu:~/go$ mkdir src pkg bin

The src folder is for the source files, pkgfor compiled packages, and library and bin for compiled programs.

The workspace is now ready for your project to get started.

☛ Step 3: Setup Go environment variables

Now, in the bashrc file, we will tell the GO environment variables i.e. $GOPATH, $GOROOT, and $GOBIN where to find the go directory.

Firstly, check if bash is installed on your system using the command.

nishee@ubuntu:~$ bash --version

If bash is not installed on your local system then run the below commands.

nishee@ubuntu:~$ sudo apt-get update && sudo apt-get install bash
nishee@ubuntu:~$ sudo apt-get upgrade

bashrc file is a script file that’s executed when a user logs in. The file itself contains a series of configurations for the terminal session. This includes setting up or enabling: coloring, completion, shell history, command aliases, and more. It is a hidden file and simple ls command won’t show the file.

Now, open the bashrc file in the editable mode using vim editor or gedit, or vscode text editor. Run the below command to edit the bashrc file using vim text editor.

nishee@ubuntu:~$ vi ~/.bashrc

To open the bashrc file in gedit text editor, use the below command.

nishee@ubuntu:~$ gedit ~/.bashrc

To open the bashrc file in vscode(Visual Studio Code) text editor, use the below command.

nishee@ubuntu:~$ code ~/.bashrc

Now, after opening the bashrc file in editable mode, add the below lines at the end of the bashrc file.

export GOROOT=/usr/local/go
export GOPATH=/home/nishee/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

These variables’ paths will be set whenever we log in to the system automatically from bashrc file. And the above lines will tell Go environment variables to look for source code files and packages at the above-mentioned folder path.

After saving and exiting the bashrc file, execute the below command once to reflect the changes.

nishee@ubuntu:~$ source ~/.bashrc

Now, write all your go source code files inside the /home/nishee/go/src as whenever a Go program encounters an import statement, it looks for the package in the Go’s standard library ($GOROOT/src). If the package is not available there, then Go refers to the system’s environment variable GOPATH which is the path to the Go workspace directory and looks for packages in $GOPATH/srcdirectory.

You can have as many workspaces as you want, as long as you keep the $GOPATH environment variable pointed to the current working workspace directory whenever you are working on a given project.

☛ Step 4: Write code in Go

Now, you are all set to write code in Go. Open your Visual Studio Code editor on the src folder available on the path /home/nishee/go .

nishee@ubuntu:~$ pwd
/home/nishee
nishee@ubuntu:~$ cd go/src
nishee@ubuntu:~/go/src$ code .

You can also directly go to thesrc folder inside go and right-click on the src folder and select the option “Open with other application” and select Visual Studio Code.

Now, create your project directory inside the src folder and start writing your first Hello World! program in Go.

Here, we will manually create the project directory inside the Go workspace from the terminal by executing the below commands.

nishee@ubuntu:~$ cd go/src
nishee@ubuntu:~/go/src$ mkdir helloworld
nishee@ubuntu:~/go/src$ cd helloworld
nishee@ubuntu:~/go/src/helloworld$ touch main.go

Now, write the below code inside your main.go file.

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

Execute the below command after saving the main.go file.

nishee@ubuntu:~/go/src/helloworld$ go run main.go

Below is the output of the above go source code file on the terminal.

Hello World!

If you are writing your Go code using Visual Studio Code text editor, then install the Go extension inside it.
To execute code .from the terminal, you need to install it using the following command on the terminal sudo apt install code

You have successfully installed Go and you also executed your first Go program on your Ubuntu 18.04 system. Now, you are on the right path to becoming a Golang developer.

Congratulations! 🎉

--

--