Skip to content

Instantly share code, notes, and snippets.

@nikhita
Last active April 26, 2024 04:46
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"
@cognivore
Copy link

Oh my. 😭

@abdennour
Copy link

Oh my zsh

@omarelsayed23
Copy link

Thanks man! @udhos

@saikumar1607
Copy link

Thank you

@Himanshu372
Copy link

Faced a strange issue with Goland when update was done. Goland wasnt able detect Go SDK.
Solution for is here - https://stackoverflow.com/questions/19108421/the-selected-directory-is-not-a-valid-home-for-go-sdk

Need to edit src/runtime/internal/sys/zversion.go file, for anyone facing similar issues.

@HosMercury
Copy link

good

@guidorafael
Copy link

Thank you ! Very clear !

@TarifEzaz
Copy link

Nicely done!

@mathaou
Copy link

mathaou commented Nov 10, 2022

@udhos legend

@gpluta
Copy link

gpluta commented Nov 11, 2022

Thanks :) A lovely simple process :)

@sigridjineth
Copy link

Download : curl -LO https://go.dev/dl/go1.18.2.linux-arm64.tar.gz
things like that

@sigridjineth
Copy link

sigridjineth commented Nov 22, 2022

If you are facing -bash: /usr/local/go/bin/go: cannot execute binary file: Exec format error error, please make sure that you are installing with correct OS version of go binary file.

  • remove rm-rf /usr/local/go/bin/go for older version
  • then tar -C /usr/local -xzf go again

@TechC3br
Copy link

TechC3br commented Dec 6, 2022

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

thanks for the help ...

@shiva2021
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!

@tikg
Copy link

tikg commented Jan 12, 2023

Thanks @udhos !

I paired it with installation of mosint

https://gist.github.com/tikg/a2d1e0d2abe83bba553351872883369e

@secfinder
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 !!

Thank's Man

@zhongweili
Copy link

Thanks!

@theaog
Copy link

theaog commented Mar 7, 2023

$ cat upgrade_go.sh

#!/usr/bin/env bash

version=$(go version|cut -d' ' -f 3)
release=$(curl --silent https://go.dev/doc/devel/release | grep -Eo 'go[0-9]+(\.[0-9]+)+' | sort -V | uniq | tail -1)

if [[ $version == "$release" ]]; then
    echo "latest go release already installed: $release"
    exit 0
fi

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

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

curl -OL https://go.dev/dl/$release
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf $release
rm -rf $tmp

go version

@RodricBr
Copy link

  • Hope this script that I made a while ago helps:

cat go-update.sh

#!/usr/bin/bash

function GO_UPD(){
  local GO_VER=$(curl -sk "https://go.dev/VERSION?m=text")
  echo -e " + Installing GO version: ${GO_VER} linux amd64"
  sudo apt install -qq -y golang-go
  wget -q "https://golang.org/dl/${GO_VER}.linux-amd64.tar.gz"
  sudo tar xf "${GO_VER}".linux-amd64.tar.gz -C /usr/local
  [ -f "$HOME/.bashrc" ]&& echo -e "Info: .bashrc exists!\n" || echo -e "Error: .bashrc doesn't exist! Creating... $(> $HOME/.bashrc)\n"
  echo -e '\nexport GOROOT=/usr/local/go\nexport GOPATH=$HOME/go\nexport PATH=$GOPATH/bin:$GOROOT/bin:$PATH' >> $HOME/.bashrc
  source "$HOME"/.bashrc
}
GO_UPD

@dxps
Copy link

dxps commented Apr 30, 2023

@theaog Your script is nice and clean. 👏
Since in that release page they also mention about upcoming release, it doesn't work now
(trying to download go1.20.4 which is not yet released at the time of this writing).

A more accurate approach to get the latest release is wget -qO- "https://golang.org/VERSION?m=text"

I did small adjustments to it, so that it matches my setup, having:

  • export PATH=$PATH:$HOME/apps/go/bin:$HOME/go/bin
  • placed this script in ~/apps/bin
#!/usr/bin/env bash

version=$(go tool dist version)
release=$(wget -qO- "https://golang.org/VERSION?m=text")

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

rm -f ${HOME}/apps/go 2>/dev/null

tar -C ${HOME}/apps -xzf $release_file
rm -rf $tmp

mv ${HOME}/apps/go ${HOME}/apps/$release
ln -sf ${HOME}/apps/$release ${HOME}/apps/go

version=$(go tool dist version)
echo "Now, local Go version is $version"

@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

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