How to use multiple go / golang versions in Linux / Ubuntu

To install and manage different versions of Go (Golang) on Ubuntu, you can use several methods. One of the easiest and most flexible ways is using a tool like gvm (Go Version Manager)

Using gvm (Go Version Manager)

  1. Install Prerequisites

    sudo apt-get update
    sudo apt-get install curl git mercurial make binutils bison gcc
    
  2. Install gvm

    bash < <(curl -sSL https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
    
  3. Source the gvm script (or restart your terminal)

    source ~/.gvm/scripts/gvm
    
  4. Install Go versions with gvm
    To install a specific Go version, for example, 1.20, run:

    gvm install go1.20
    
  5. Set the Go version to use
    You can use the installed version by running:

    gvm use go1.20 --default
    
  6. Check Go version

    go version