Installing GO (1.21.4) on Ubuntu 22.04 the right way

Installing GO (1.21.4) on Ubuntu 22.04 the right way

A beginner's Guide

Introduction

I recently got interested in learning the GO language and almost got discouraged right from the installation process. If you are reading this, that means "maybe" you have issues too. Let's see if we share some things in common.

First of all, the terminal commands on the official website didn't work for me (at first). After trying a ton of suggestions from Stack Overflow and other reliable articles I got the installation working (or so I thought) but then my VS Code extension didn't work!

I kept getting this almost frustrating error in VS Code: Failed to run '/snap/bin/go env' error

Then I returned to The Source- the official docs. After downloading the installation package, I ran the commands provided and kept getting this error:

"tar: Cannot mkdir: Permission denied" error, even when using sudo.

Just so we are on the same page here. This is the command from the official Go website (go.dev/doc/install):

 rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.4.linux-amd64.tar.gz

Fortunately, after resolving this, my VS Code extension worked like a charm!

This issue arises during the extraction process, and in this blog post, we'll delve into a specific solution that involves separating the rm and tar commands.

The Problem

Running the above command from the official docs error typically looks like this:

tar: go: Cannot mkdir: Permission denied
tar: go/src/runtime/msan0.go: Cannot open: No such file or directory
tar: go: Cannot mkdir: Permission denied
tar: go/src/runtime/msan_amd64.s: Cannot open: No such file or directory
...

This occurs when the tar command is used with sudo to extract files, but the permissions aren't correctly set for the target directory.

The Solution:

Separate rm and tar commands

sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf YOUR TAR.GZ FILE

Mine at the time of writing looks like this:

sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.21.4.linux-amd64.tar.gz

This approach involves two steps:

  1. Remove Existing Installation:

    • The first command (sudo rm -rf /usr/local/go) removes the existing Go installation.

    • The -r flag stands for recursive, and the -f flag stands for force, which means it will remove the directory and its contents without asking for confirmation.

  2. Extract New Installation: The second command (sudo tar -C /usr/local -xzf go1.21.4.linux-amd64.tar.gz) then extracts the contents of the new Go archive into the /usr/local directory.

By separating these commands, both the removal and the extraction steps are performed with elevated privileges, resolving the "Cannot mkdir: Permission denied" issue.

Follow steps 2 and 3 on the official installation guide.
When you open a .go file in VS Code, you'll be prompted to install the extension and you're good to go!

Summary:

If you are a total beginner, I got you covered! Here are all the steps:

  1. Head over to Go's official website go.dev/dl and download the current stable version.

  2. Run the following command in your terminal (you MAY have to cd into the Downloads folder.

sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.21.4.linux-amd64.tar.gz
  1. To access and edit your ~/.profile or /etc/profile file on Ubuntu and add the specified export statement, you can use a text editor like nano or vim. Here are the steps:

For ~/.profile (user-specific profile) using nano, run the following command in the terminal:

nano ~/.profile

- Add the following line at the end of the file (copy and paste the following):

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

- Save the changes. In nano, you can usually do this by pressing Ctrl + O to write the changes, then Enter to confirm, and finally Ctrl + X to exit.

- After editing, you may need to either open a new terminal or run the following command to apply the changes to your current session:

source ~/.profile
  1. Verify that you've installed Go by typing the following command in your terminal:
go version

As of the time of writing this article, mine is:

go version go1.21.4 linux/amd64
  1. Open VS Code and open a .go file if you do not have the extension, VS Code will prompt you to install it. Please do!

That's it, folks! ๐Ÿš€ I hope it worked for you.๐Ÿ™ Let me know how it went.
Likes, shares and constructive comments are welcome!๐Ÿค—

ย