How to download latest release from Github
Sometimes you might want to download latest release of binary from Github repo using script. You can do this using API, but lets assume that on some enviroments you have only basic tools, and no jq to extract values from JSon.
-
Git hub has built in redirect, i.e.
https://github.com/user/repo/releases/latest
which redirect to page dedicated to latest release -
We can get redirect url with
curl
, i.e.curl -Ls -o /dev/null -w "%{url_effective}" https://github.com/user/repo/releases/latest
will get ushttps://github.com/user/repo/releases/tag/0.15.9
-
Now we can extract version by using
basename
, i.e.basename https://github.com/user/repo/releases/tag/0.15.9
will return0.15.9
- Using this version we can construct download url.
For example script that downloads and installs latest version of fzf
#!/bin/bash
export FZF_VERSION=$(curl -Ls -o /dev/null -w "%{url_effective}" https://github.com/junegunn/fzf-bin/releases/latest | xargs basename)
curl -L https://github.com/junegunn/fzf-bin/releases/download/$FZF_VERSION/fzf-$FZF_VERSION-linux_amd64.tgz | tar -xz -C /tmp/
sudo mv /tmp/fzf-$FZF_VERSION-linux_amd64 /usr/bin/fzf