This document describes how to switch between different Go versions when you have multiple Go versions installed on your system.
Prerequisites
- Multiple Go versions installed (e.g., go1.22, go1.24)
Steps
-
Identify the installation paths of your Go versions.
For example:
- go1.22:
/usr/lib/go-1.22
- go1.24:
/home/xxx/sdk/go1.24.4
- go1.22:
-
Modify your shell configuration file.
The shell configuration file depends on the shell you are using. For example:
- Bash:
~/.bashrc
or~/.bash_profile
- Zsh:
~/.zshrc
- Fish:
~/.config/fish/config.fish
- Bash:
-
Set the
GOROOT
andPATH
environment variables.Add the following lines to your shell configuration file, replacing the paths with the actual installation paths of your desired Go version:
1 2
export GOROOT=/path/to/go/version export PATH=$GOROOT/bin:$PATH
For Fish shell, use:
1 2
set -gx GOROOT /path/to/go/version set -gx PATH $GOROOT/bin $PATH
For example, to switch to go1.24 in Fish shell, add the following lines to
~/.config/fish/config.fish
:1 2
set -gx GOROOT /home/xxx/sdk/go1.24.4 set -gx PATH /home/xxx/sdk/go1.24.4/bin $PATH
-
Reload your shell configuration.
Run the following command to reload your shell configuration:
1 2 3
source ~/.bashrc # For Bash source ~/.zshrc # For Zsh source ~/.config/fish/config.fish # For Fish
-
Verify the Go version.
Run the following command to verify that you have successfully switched to the desired Go version:
1
go version
The output should show the Go version you selected.
Example
To switch to go1.24 in Fish shell, follow these steps:
-
Edit
~/.config/fish/config.fish
and add the following lines:1 2
set -gx GOROOT /home/xxx/sdk/go1.24.4 set -gx PATH /home/xxx/sdk/go1.24.4/bin $PATH
-
Run
source ~/.config/fish/config.fish
to reload the shell configuration. -
Run
go version
to verify that you are using go1.24.4.