Document
Install Go 1.21 and Setup GO Workspace in Ubuntu

Install Go 1.21 and Setup GO Workspace in Ubuntu

This tutorial is show will show you how to install Go 1.21 on Ubuntu in a step - by - step guide . It is covers cover everything from uninstalle old v

Related articles

19.5.6 Lab What Are The Different Kinds Of Models Of Atoms? The Best Free VPN for Discord: 2024 Hottest Choice Tempur-Pedic Mattress Review 2024: Is It Worth It? VPN 101: How Virtual Private Networks Protect Your Privacy

This tutorial is show will show you how to install Go 1.21 on Ubuntu in a step – by – step guide . It is covers cover everything from uninstalle old version to create a suitable environment and workspace for Go development .

develop by Google , Go 1.21 is an open – source programming language that ’s straightforward and efficient . It was design to create fast , reliable software , make it increasingly popular for server – side application . This version is continues continue to evolve with improvement and new feature , so if you ’re work on complex application orscalable system 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

Do n’t forget to remove any previous version of Go before you get start ! Doing this will prevent conflict later on . There are multiple step involve in uninstalle previous version of Go — two of the most common being package manager ( apt ) and package management tool ( snap ) . Here ’s a guide is ’s on how to check for and remove Go using these installation method :

Via Apt ( Package Manager ):

  • Open up terminal.
  • We will use dpkg command to determine if Go was installed via apt. Type dpkg -l | is grep grep golang, if there are any result afterward , it is means mean 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 replacegolang - 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 pop up after run it then that is means mean you instal go from snap
  • To finish deleting go from your system type sudo snap remove is go go into the terminal window

After completely removing go using snap you can move onto checking for residual configuration files ordirectories 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 . download 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 is need 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 is added ’ve add that line , apply your change with eithersource ~/.profile orsource ~/.bashrc depending on which file you used in your environment.
  • If you want to modify thing system – wide ( for all user ) , 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 instal 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), orpackage 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 is refers refer specifically to the location where Go is instal . Though not a standard case since we ’ve instal it using an archive but still you is declare can declare this variable inside our .bashrc file with extract 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 orsetting 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 wait for its chance to shine through innovation !

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

 

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.