I just fiddled around a bit to find out how to download the latest GitHub release via the command line. Turns out that GitHub doesn’t provide a universal download URL to release binaries like it does for the release browser page itself. The latest release page can always be reached via https://github.com/ORGANIZATION/REPO/releases/latest
, e.g. https://github.com/gvenzl/csv2db/releases/latest
. Unfortunately that is not true for the binaries, which are available under https://github.com/ORGANIZATION/REPO/archive/RELEASE_TAG.zip
and https://github.com/ORGANIZATION/REPO/archive/RELEASE_TAG.tar.gz
but not under something generic like https://github.com/ORGANIZATION/REPO/archive/latest.zip
and https://github.com/ORGANIZATION/REPO/archive/latest.tar.gz
The issue here is that GitHub only provides the release binaries under their actual release tag, e.g. v1.0.0
, but that tag is entirely up to the user to define and changes as more releases are added, of course. It’s therefore hard to guess what the release tag would be and what to download. Nevertheless, thanks to the GitHub developer APIs it’s not that tricky to figure out the release tag for the latest release and instruct a utility like curl to download the binary. I’ve written a little one-liner for the UNIX command line to do exactly that. It took me a bit to get it right and so I thought it might come handy to you, rather than trying to reinvent the wheel. it It’s also available as a public Gist but I thought for visibility purposes it makes sense to blog it here, too.
LOCATION=$(curl -s https://api.github.com/repos/YOUR_ORGANIZTION/YOUR_REPO/releases/latest \ | grep "tag_name" \ | awk '{print "https://github.com/YOUR_ORGANIZATION/YOUR_REPO/archive/" substr($2, 2, length($2)-3) ".zip"}') \ ; curl -L -o OUTPUT_FILE_NAME $LOCATION
For example:
LOCATION=$(curl -s https://api.github.com/repos/gvenzl/csv2db/releases/latest \ | grep "tag_name" \ | awk '{print "https://github.com/gvenzl/csv2db/archive/" substr($2, 2, length($2)-3) ".zip"}') \ ; curl -L -o csv2db.zip $LOCATION
Here is how it goes:
LOCATION=$(...)
stores the output of all the commands in the brackets in the variable $LOCATION
,
curl -s https://api.github.com/repos/gvenzl/csv2db/releases/latest
gets the latest release from your repository, in my case https://github.com/gvenzl/csv2db
,
grep "tag_name"
grabs the tag name of the latest release (e.g. v1.0.0
),
awk '{print "https://github.com/gvenzl/csv2db/archive/" substr($2, 2, length($2)-3) ".zip"}'
prints https://github.com/gvenzl/csv2db/archive/ + v1.0.0 + .zip
–>
https://github.com/gvenzl/csv2db/archive/v1.0.0.zip.
Now the $LOCATION
environment variable is set to that string. From that point on you can do with that variable whatever you like, in case you have another use than downloading the binary. Or, just like below, you can complete the download process:
curl -L -o csv2db.zip $LOCATION
invokes cURL and downloads $LOCATION
into csv2db.zip
. The -L
parameter is important so that cURL follows the URL, i.e. redirect.
Hope this helps.
Thank you so much for sharing this post on how to download the latest Github repo release. I’ve been searching this guide for a while, tried several methods I found, but this method is the most effective. I have tried this already and it is really working. For those who want to try, just read carefully and follow what is being suggested on this post and you can successfully do it too.
LikeLike
is there an equivalent/similar for windows 10 od?
LikeLike
Hi LaljiG,
Unfortunately, I only use Mac.
Thx,
LikeLike
Hi @LaljiG.
I have just done a Batch file script that does exactly this. Not sure if you still need it. I needed something like this for my server and I have believe this will work for what you need.
@echo off
::Name of the Git owner
set Owner=NAME_OF_OWNER_HERE
::Name of the Repository
set Repo=REPOSITORY_NAME
::Release Filename to download
set filename=FILENAME.zip
::Where to extract Release Zip
set OutputDir=C:\OUTPUT_FOLDER
::—————————————————–
:: DO NOT EDIT BELOW THIS LINE
::—————————————————–
for /f “tokens=* usebackq” %%F in (`curl https://github.com/%Owner%/%Repo%/releases/latest`) DO (SET output=%%F)
set output=%output:~54,-31%
for /f “delims=/ tokens=5” %%G in (‘echo %output%’) do (set version=%%G)
cls
curl -L -o %filename% https://github.com/%Owner%/%Repo%/releases/download/%version%/%filename%
“C:\Program Files\7-Zip\7z.exe” e %filename% -o%OutputDir% -y
LikeLiked by 1 person
Thanks a lot, Aaron! 🙂
LikeLiked by 1 person
I’m guessing it isn’t really possible if the github organization is private / requires authentication. Anyone know if there’s ANY way to do this? Supposing the auth details were available, would it be possible?
LikeLike
Hey Gene,
It should be possible, yes. Looking at GitHub’s Resources in the REST API, it appears that one can pass in an OAuth2 Token in the header or a basic username and password.
Hope this helps!
Thx,
LikeLike
By the way, thank you for this, its amazingly helpful for public repos.
LikeLike
Thanks a lot, Gene! Glad it helps others as well!
Thx,
LikeLike
Easy way for first download item:
curl https://api.github.com/repos/fusioninventory/fusioninventory-for-glpi/releases/latest -s | jq -r ‘.assets[0].browser_download_url’
https://github.com/fusioninventory/fusioninventory-for-glpi/releases/download/glpi10.0.6%2B1.1/fusioninventory-10.0.6%2B1.1.tar.bz2
LikeLike