Friday, March 1, 2024
HomeHow toHow to Install GO (golang 1.22) in Ubuntu 22.04

How to Install GO (golang 1.22) in Ubuntu 22.04

Go programming language announced the new 1.22 release this Tuesday! Here’s the new features and how to install guide for Ubuntu & other Linux.

What’s New in Go 1.22:

  • The variables declared by a “for” loop were created once and updated by each iteration. In new 1.22 release, each iteration of the loop creates new variables, to avoid accidental sharing bugs.
  • “For” loops may now range over integers.
  • Commands in workspaces can now use a vendor directory containing the dependencies of the workspace.
  • go get is no longer supported outside of a module in the legacy GOPATH mode
  • 2% ~ 14% improvement from enabling PGO.
  • Requires the final point release of Go 1.20 or later for bootstrap
  • New math/rand/v2 package
  • New go/version package
  • See release note for more details.

How to Install Golang 1.22 in Ubuntu

1. Download the Linux Tarball

Go provides official Linux tarball for i386, amd64, arm64, and armv6l CPU architecture types. They are available to download at the link below:

In case you don’t know your system architecture type, press Ctrl+Alt+T to open terminal and run dpkg --print-architecture command to tell.

Or, run command to download the Linux tarball from command line (amd64 package in the case):

wget -c https://go.dev/dl/go1.22.0.linux-amd64.tar.gz

2. Extract Go Tarball to /usr/local

After downloaded the tarball, open terminal (Ctrl+Alt+T), and run commands:

After successfully extracted the tarball, use ls /usr/local to verify. It will output a list of sub-folders including go.

3. Set PATH Environment Variable

To let your Ubuntu system know where to find Go command, user can add it to the PATH.

Without logging out, run the command below to set PATH environment, which works until your close the terminal window or command console.

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

To make it permanent, open home folder, press Ctrl+H, then click edit the .profile file (or .bashrc). When file opens, add following lines and save it.

# set PATH so it includes /usr/local/go/bin if it exists
if [ -d "/usr/local/go/bin" ] ; then
    PATH="/usr/local/go/bin:$PATH"
fi

This works for current user only, and applies in next login.

To set the PATH environment variable for all users, create & edit a config file under /etc/profile.d directory instead. To do so, run command:

sudo nano /etc/profile.d/go.sh

Then, paste the same lines above. Press Ctrl+S to save, and Ctrl+X to exit. Also, log out and back in to apply.

Create your First Go program

When done setting PATH Environment Variable, you can run command to verify go version:

go version

To create your first Go project, create new file hello.go either in file manager or by using the command below:

nano hello.go

Then, add following lines and save it (For nano, press Ctrl+S then Ctrl+X):

package main

import "fmt"

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

Then, either use go run hello.go command to run it. Or go build hello.go to build into a binary file.

Uninstall Go

To uninstall golang, simply delete the go directory under /usr/local by running command:

sudo rm -R /usr/local/go

That’s it.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


spot_img

Most Popular