Skip to main content
Golang

How to install multiple versions of Go?

Prerna Sharma

When do we need multiple Go versions?

As a user of the Go programming language, you might need multiple versions of Go. This might have multiple reasons. For example, you probably have different services running in different Go versions or you want to work with external dependencies which works with only specific Go version.

Installing Go means you have a single go command you can run to build and start your project. This is simple for getting started, but also can be limiting.

A more flexible set up is to enable running multiple versions within the same environment via go1.20 or go1.21 commands.

Prerequisites

You need to have Go and Git installed in your machine.

  1. Go – https://golang.org/doc/install
  2. Git – https://git-scm.com/

How to install multiple Go versions?

While I am writing this article, I have go1.19.4 version installed in my machine. If you want to check current Go version installed in your machine. Just fire the below command in your terminal or powershell.

go version

This should give you output like below:

go version go1.19.4 darwin/arm64

To install additional Go versions, run the go install command, specifying the download location of the version you want to install. The following example illustrates with version go1.21.5.

Installation Example for go1.21.5

Fire the below command in your terminal or powershell.

go install golang.org/dl/go1.21.5@latest

You will see something like this in the output.

go: downloading golang.org/dl v0.0.0-20231205181335-d8bde1699968

You need to run one more command now.

go1.21.5 download 

You will see something like this in the output.

Downloaded   0.0% (   16384 / 65096621 bytes) ...
Downloaded   7.2% ( 4685792 / 65096621 bytes) ...
Downloaded  29.8% (19398512 / 65096621 bytes) ...
Downloaded  49.3% (32063248 / 65096621 bytes) ...
Downloaded  68.7% (44727984 / 65096621 bytes) ...
Downloaded  87.2% (56770128 / 65096621 bytes) ...
Downloaded 100.0% (65096621 / 65096621 bytes)
Unpacking /Users/aakashverma/sdk/go1.21.5/go1.21.5.darwin-arm64.tar.gz ...
Success. You may now run 'go1.21.5'

Now you can run command to check your go1.21.5 version.

go1.21.5 version

And there we go.

go version go1.21.5 darwin/arm64

If you want to understand how does it work behind the scenes, please continue reading the article.

How does it work behind the scenes?

The repository https://go.googlesource.com/dl holds the magic for this functionality. It features:

  1. A small application as per Go version – example: go1.20/main.go
  2. An internal package that implements the Go wrapper functionality: internal/version/version.go

For each version of Go the main.go, for the Go wrapper looks like:

package main

import "golang.org/dl/internal/version"

func main() {
    version.Run("go1.20")
}

As you can see there isn’t much going on. We simply call the common code for the version to be served.

A packaged version’s Run function performs the two main tasks (based on command line arguments):

  1. Download – running go<version> download
  2. Wrapper for Go toolchain – running go<version> <anything else>

I hope you enjoyed 😄