Install Go 1.21 and Setup GO Workspace in Ubuntu


GO

This tutorial will show you how to install Go 1.21 on Ubuntu in a step-by-step guide. It covers everything from uninstalling older versions to creating a suitable environment and workspace for Go development.

Developed by Google, Go 1.21 is an open-source programming language that’s straightforward and efficient. It was designed to create fast, reliable software, making it increasingly popular for server-side applications. This version continues to evolve with improvements and new features, so if you’re working on complex applications or scalable systems as a developer, this could be the right choice.

 

Steps to Install (or Upgrade) GO 1.21 and setup GO Workspace Properly

1. Uninstalling Previous Versions of Go

Don’t forget to remove any previous versions of Go before you get started! Doing this will prevent conflicts later on. There are multiple steps involved in uninstalling previous versions of Go—two of the most common being package manager (apt) and package management tool (snap). Here’s a guide on how to check for and remove Go using these installation methods:

Via Apt (Package Manager):

  • Open up terminal.
  • We will use dpkg command to determine if Go was installed via apt. Type dpkg -l | grep golang, if there are any results afterward, it means that it was.
  • To delete Go from your system, type: sudo apt remove --autoremove golang-go into the terminal window. This command deletes all traces of the go program including dependencies.
  • Make sure you replace golang-go with the exact package name if yours isn’t named this way.
  • You should also check for remaining files after deleting go. Afterward delete them manually if necessary.

Via Snap (Package Management Tool):

  • Open up terminal.
  • Type snap list | grep go into the window now. If anything pops up after running it then that means you installed go from snap
  • To finish deleting go from your system type sudo snap remove go into the terminal window

After completely removing go using snap you can move onto checking for residual configuration files or directories and deleting them when necessary as well. You’ll want to search in directories such as /usr/local/go and even your home directory for any go related folders (such as ~/go).

 

2. Downloading Go 1.21

Downloading Go 1.21 is quite simple and only takes a few steps. The first thing you’ll need to do is visit the official Go download page to get the direct link for the Go 1.21 tarball. After that, you can use command line instructions to download and extract it properly.

wget https://golang.org/dl/go1.21.6.linux-amd64.tar.gz

 

3. Installing Go 1.21

  • Once you’re finished downloading the tarball, you have to take it a step further and give it a good squeeze by extracting it. This is usually done in the /usr/local directory.
  • First, open your terminal.
  • Then, run this command: replace go1.21.x.linux-amd64.tar.gz with the real name of the file you downloaded. What this command does is extract the Go files into a directory named go in /usr/local.
sudo tar -xvf go1.21.x.linux-amd64.tar.gz -C /usr/local

 

4. Configuring the PATH Environment Variable

  • You need to add Go’s bin directory to your PATH environment variable if you want to use the Go command from any location.
  • You can do this by adding the following line to your ~/.profile or ~/.bashrc file. If you are not familiar why we are choosing these files then you should read .bashrc vs .bash_profile [Which one to use?]
export PATH=$PATH:/usr/local/go/bin
  • Once you’ve added that line, apply your changes with either source ~/.profile or source ~/.bashrc depending on which file you used in your environment.
  • If you want to modify things system-wide (for all users), then edit /etc/profile instead of one at a time for each user. However, modifying system-wide settings like that isn’t suggested unless you’re managing a multi-user system and need everyone to have access to Go.

 

4. Verify Installation

To confirm that everything worked correctly up until now, reopen your terminal and type echo $PATH. You should see the path leading to the Go bin directory somewhere in there.

$ echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/usr/local/go/bin

To confirm that Go is installed correctly, close and reopen your terminal, and then type:

go version

This should display the installed version of Go, confirming that the installation was successful.

go version go1.21.6 linux/amd64

 

5. Setting up the Go workspace

Establishing the Go workspace is an essential part of managing and organizing projects efficiently. It stores source code, dependencies, and compiled binaries - all crucial components of Go’s package structure and build process. Here are some details on how to get started:

In general, the workspace directory is named go and can be found in your home directory. You may have to create it:

mkdir ~/go
  • The workspace has three subdirectories: src, bin, and pkg.
  • src contains your Go source files, bin contains your compiled executables, and pkg houses your package objects.
  • Create these directories using the following commands:
mkdir -p ~/go/src ~/go/bin ~/go/pkg

GOPATH indicates where you keep all your Go projects and their dependencies. It tells Go where to find source files (src), compiled binaries (bin), or package objects (pkg). When it comes time for the compilation stage, GOPATH is necessary for locating the proper source code. This applies to both your own projects as well as their dependencies. The variable makes sure that the compiler knows which files are needed.

Set GOPATH to point at your workspace by adding this line to your ~/.profile or ~/.bashrc file:

export GOPATH=$HOME/go >> ~/.bashrc
export PATH=$PATH:$GOPATH/bin >> ~/.bashrc

GOROOT refers specifically to the location where Go is installed. Though not a standard case since we’ve installed it using an archive but still you can declare this variable inside our .bashrc file with extracted archive PATH:

export GOROOT=/usr/local/go >> ~/.bashrc

Unlike GOPATH which focuses on one user's space GOROOT covers system-wide configurations of Go tools. This information helps scripts know where to find toolchains and standard libraries.

 

This guide should help you install version 1.21 on Ubuntu as well as set up an efficient environment for working with Go. With clear steps provided for uninstalling old versions or setting up a fresh environment from scratch even beginners will be able to make sense of things quickly.
No matter what level of experience you have in development there’s great potential here waiting for its chance to shine through innovation!

For detailed information and guidance, visit Go's Official Documentation.

 

Views: 696
Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!