Skip to content

Instantly share code, notes, and snippets.

@nikhita
Last active February 24, 2025 03:12
Show Gist options
  • Save nikhita/432436d570b89cab172dcf2894465753 to your computer and use it in GitHub Desktop.
Save nikhita/432436d570b89cab172dcf2894465753 to your computer and use it in GitHub Desktop.
How to update the Go version

How to update the Go version

System: Debian/Ubuntu/Fedora. Might work for others as well.

1. Uninstall the exisiting version

As mentioned here, to update a go version you will first need to uninstall the original version.

To uninstall, delete the /usr/local/go directory by:

$ sudo rm -rf /usr/local/go

2. Install the new version

Go to the downloads page and download the binary release suitable for your system.

3. Extract the archive file

To extract the archive file:

$ sudo tar -C /usr/local -xzf /home/nikhita/Downloads/go1.8.1.linux-amd64.tar.gz

4. Make sure that your PATH contains /usr/local/go/bin

$ echo $PATH | grep "/usr/local/go/bin"
@theaog
Copy link

theaog commented May 24, 2023

thank you @dxps, great work!

@dxps
Copy link

dxps commented May 25, 2023

@theaog Thanks! Although most of the work has been done by you. 😊
One more improvement to it is this version=$(go tool dist version).
Meanwhile, updated the previous post.

@theaog
Copy link

theaog commented Jun 8, 2023

@dxps in your modified script you're assuming the GOROOT is $HOME/apps, users might have a different install path.

I think it's best to retrieve the path using go env |grep GOROOT or any other solution you might come up with, as long as it's not hardcoded and servers as many users as possible.

@theaog
Copy link

theaog commented Jun 8, 2023

e.g.

goroot=$(go env |grep GOROOT |cut -d'=' -f2 |tr -d '"')
rm -f "$goroot"

@dxps
Copy link

dxps commented Jun 8, 2023

@theaog My preference is to keep different apps installed in $HOMEDIR/apps.
Yes, GOROOT can be considered and this would be more flexibile.

But I think it's misleading to extract the release archive into $GOROOT/go
(as you put it in the script above: sudo tar -C "${goroot//go}").
Currently, GOROOT on my side - with the current version - is /home/dxps/apps/go1.20.4.

But if your recently posted script works, that's the whole point! 👏

@DieTime
Copy link

DieTime commented Jun 21, 2023

Just use go-up project that supports both Linux and MacOS and
automatically determines where to install the new go version based
on the previous one.

Single line solution using curl

curl -sL https://raw.githubusercontent.com/DieTime/go-up/master/go-up.sh | bash

Single line solution using wget

wget -qO- https://raw.githubusercontent.com/DieTime/go-up/master/go-up.sh | bash

@laurentknauss
Copy link

I switched to go1.21 version following your prompts , works great!

@dxps
Copy link

dxps commented Sep 1, 2023

Unfortunately, that go-up.sh solution doesn't cover some cases.

See a concrete example of how things can be destroyed:

…/apps ❯ curl -sL https://raw.githubusercontent.com/DieTime/go-up/master/go-up.sh | bash
❌ Go not installed, nothing to update
…/apps ❯ 
…/apps ❯ GOBIN=$(which go 2> /dev/null) && echo $GOBIN
…/apps ❯ 
…/apps ❯ tar zxf /disk2t/files/kits/dev/go/go1.21.0.linux-amd64.tar.gz 
…/apps took 1s❯ 
…/apps ❯ 
…/apps ❯ GOBIN=$(which go 2> /dev/null) && echo $GOBIN
/home/dxps/apps/go/bin/go
…/apps ❯ 
…/apps ❯ curl -sL https://raw.githubusercontent.com/DieTime/go-up/master/go-up.sh | bash
❌ Couldn't download and unpack go with version 1.21.0
time 2023-08-04T20:14:06Z
…/apps took 1s❯ 
…/apps ❯ ls -l go/
total 0
…/apps ❯

@Olanetsoft
Copy link

Thank you

@SahilDhingraa
Copy link

windows update?

@dxps
Copy link

dxps commented Nov 2, 2023

@SahilDhingraa Plenty of options to consider. As an example, check this out.

@Tuanm
Copy link

Tuanm commented Nov 25, 2023

I have sketched a simple script to automate the task:

git clone https://github.com/udhos/update-golang
cd update-golang
sudo ./update-golang.sh

Thanks, bro!

@cryptopunkstar
Copy link

Thank you I practice V2 Sei Network on Ubuntu !!

@renanbastos93
Copy link

I have sketched a simple script to automate the task:

git clone https://github.com/udhos/update-golang
cd update-golang
sudo ./update-golang.sh

Thanks @udhos !!

🔝 thanks a lot

@herloncosta
Copy link

I have sketched a simple script to automate the task:

git clone https://github.com/udhos/update-golang
cd update-golang
sudo ./update-golang.sh

Great work man!

Very good solution, thanks!!

@HassanAarzoo
Copy link

You can also try brew upgrade go if you have installed go via brew

@renanbastos93
Copy link

Of course @HassanAarzoo but the problem to updated using raspibian.

@pask01
Copy link

pask01 commented May 21, 2024

Worked fine, thanks! I also had to remove old versions of Go from /usr/lib and /usr/share

$ rm -rf /usr/lib/go
$ rm -rf /usr/lib/go-1.13

$ rm -rf /usr/share/go
$ rm -rf /usr/share/go-1.13

@okedialf
Copy link

I have sketched a simple script to automate the task:

git clone https://github.com/udhos/update-golang
cd update-golang
sudo ./update-golang.sh

Great work man!

Awesome

@okedialf
Copy link

Thank you @Tuanm

@Tuanm
Copy link

Tuanm commented May 27, 2024

Thank you @Tuanm

What did you thank me for, bro? :'v

@CollinsWanjau
Copy link

After installing the latest go-system-wide, i get this version colloso@colloso-ThinkCentre-M73:~/Documents/Go/hello$ go version go version go1.20.3 linux/amd64

@catgoose
Copy link

catgoose commented Jul 6, 2024

Thank you @Tuanm

What did you thank me for, bro? :'v

Thanks chief

@michalis-papamichael
Copy link

Thanks

@pa035
Copy link

pa035 commented Sep 10, 2024

I have sketched a simple script to automate the task:

git clone https://github.com/udhos/update-golang
cd update-golang
sudo ./update-golang.sh

Worked for me. Any script that automatically cleans and removes the temporary files used for the installation?

@GuilhermeBn198
Copy link

guys, i've combined your implementations and gave it a little plus:

#!/usr/bin/env bash

version=$(go version 2>/dev/null || echo "none")
release=$(wget -qO- "https://golang.org/VERSION?m=text" | awk '/^go/{print $0}')

if [[ $version == *"$release"* ]]; then
    echo "The local Go version ${release} is up-to-date."
    exit 0
else
    echo "The local Go version is ${version}. A new release ${release} is available."
fi

release_file="${release}.linux-amd64.tar.gz"

tmp=$(mktemp -d)
cd $tmp || exit 1

echo "Downloading https://go.dev/dl/$release_file ..."
curl -OL https://go.dev/dl/$release_file

sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf $release_file

rm -rf $tmp

cd ~

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

version=$(go version)
if [[ $version == *"$release"* ]]; then
    echo "Now, local Go version is $version"
else
    echo "Failed to update Go. Current version is still $version."
    exit 1
fi

@marcelloinfoweb
Copy link

marcelloinfoweb commented Sep 22, 2024

Modified for ARM64 versions

#!/usr/bin/env bash

version=$(go version 2>/dev/null || echo "none")
release=$(wget -qO- "https://golang.org/VERSION?m=text" | awk '/^go/{print $0}')

if [[ $version == *"$release"* ]]; then
    echo "The local Go version ${release} is up-to-date."
    exit 0
else
    echo "The local Go version is ${version}. A new release ${release} is available."
fi

release_file="${release}.linux-arm64.tar.gz"

tmp=$(mktemp -d)
cd $tmp || exit 1

echo "Downloading https://go.dev/dl/$release_file ..."
curl -OL https://go.dev/dl/$release_file

sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf $release_file

rm -rf $tmp

cd ~

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

version=$(go version)
if [[ $version == *"$release"* ]]; then
    echo "Now, local Go version is $version"
else
    echo "Failed to update Go. Current version is still $version."
    exit 1
fi

@wathika-eng
Copy link

@draco1725
Copy link

@wathika-eng

Thanks bro, well scripted and it helped me great work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment