Install Go 1.21 and Setup GO Workspace in Ubuntu


Written by - Deepak Prasad

This tutorial guides you through detailed steps to install Go 1.21 on Ubuntu, covering steps from uninstalling older versions, downloading and installing Go, to setting up your environment and workspace for Go development.

Go 1.21, developed by Google, is a modern, open-source programming language known for its simplicity and efficiency. It's designed to build fast, reliable software, making it increasingly popular for server-side applications. This version continues to evolve with improvements and new features, making it a go-to choice for developers working on scalable systems and complex applications.

 

1. Uninstalling Previous Versions of Go

Before installing Go 1.21, it's essential to remove any previous versions to avoid conflicts. Uninstalling previous versions of Go involves several steps, depending on how Go was initially installed on your system. The two most common installation methods are via the package manager (apt) and the package management tool (snap). Here's how to check for and remove Go based on these methods:

Via Apt (Package Manager):

  • Open a terminal.
  • Type dpkg -l | grep golang to check if Go was installed through the package manager. If it returns any results, it indicates that Go was installed via apt.
  • To remove Go, use the command: sudo apt remove --autoremove golang-go. This command uninstalls Go and removes any unused dependencies.
  • Alternatively you can replace golang-go with the exact package name in case the name varies in your environment.
  • It's recommended to also check for any remaining Go files in the system and remove them if necessary.

Via Snap (Package Management Tool):

  • Open a terminal.
  • Type snap list | grep go to check for Go installed via Snap. If you get an output, it implies Go was installed through Snap.
  • To remove Go, use the command sudo snap remove go.
  • This command will uninstall the Go package managed by Snap.

After uninstalling Go, it's a good practice to check if there are any residual configuration files or directories related to Go. Look in directories like /usr/local/go or your home directory for any Go-related folders (~/go), and remove them if they are no longer needed. This ensures a clean environment for a fresh installation of Go 1.21.

 

2. Downloading Go 1.21

Downloading Go 1.21 involves a few simple steps. First, you'll need to visit the official Go download page to get the direct link for the Go 1.21 tarball. Then, you can use command-line instructions to download and extract the tarball. Here's how to do it:

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

 

3. Installing Go 1.21

  • After downloading the tarball (as explained in the previous step), you need to extract it. This is typically done in the /usr/local directory.
  • Open your terminal.
  • Run the following command, making sure to replace go1.21.x.linux-amd64.tar.gz with the actual name of the file you downloaded. This command will 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

  • To use the Go command from any location, you need to add the Go bin directory to your PATH environment variable.
  • 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
  • After adding this line, run the following command to apply the changes to your current session using either source ~/.profile or source ~/.bashrc based on the file you have chosen in your environment.
  • If you want to make the changes system-wide (for all users), you can edit the /etc/profile file instead. However, modifying system-wide settings is generally not recommended unless you are managing a multi-user system and need to ensure that all users have access to Go.

 

4. Verify Installation

After completing these steps, you can verify that the PATH variable has been updated correctly by reopening the terminal and typing echo $PATH. You should see the path to the Go bin directory in the output.

$ 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

Setting up the Go workspace is a crucial step in organizing and managing Go projects efficiently. The workspace is where you'll store Go source code, dependencies, and compiled binaries, and it's fundamental for Go's package structure and build process. Here's an explanation of its importance and how to set it up:

Typically, the workspace directory is named go and is located in your home directory. Create it using the command:

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

GOPATH signifies the root of your Go workspace. This is where Go looks for source files (src), compiled binaries (bin), and package objects (pkg). During compilation, Go uses GOPATH to locate source code for both your projects and their dependencies. It ensures that the Go compiler and other tools know where to find the necessary files.

Set the GOPATH environment variable to point to your workspace. Add this line to your ~/.profile or ~/.bashrc:

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

GOROOT is used to point to the directory where Go is installed. This is particularly important if you have installed Go in a non-standard location. But as a standard practice we can also declare this variable inside our .bashrc file with the PATH where we have extracted the archive:

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

While GOPATH is specific to a user's workspace, GOROOT is more about the system-wide configuration of Go. It tells Go tools and scripts where to find the Go toolchain and standard library.

 

With this comprehensive guide, you are now equipped to install Go 1.21 on Ubuntu, set up your Go workspace, and embark on your journey of Go development. The process, from uninstalling previous versions to configuring your environment, has been streamlined for ease of understanding. Whether you're a beginner or an experienced developer, the world of Go offers robust tools and a supportive community to bring your innovative projects to life. Start coding with Go today and explore the exciting opportunities in software development!

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

 

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 reach out to him on his LinkedIn profile or join on Facebook page.

Categories GO

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!!