106

I am trying to download a tarball from GitHub using cURL, but it does not seem to be redirecting:

$ curl --insecure https://github.com/pinard/Pymacs/tarball/v0.24-beta2
<html><body>You are being <a href="https://nodeload.github.com/pinard/Pymacs/tarball/v0.24-beta2">redirected</a>.</body></html>

Note: wget works for me:

$ wget --no-check-certificate https://github.com/pinard/Pymacs/tarball/v0.24-beta2

However I want to use cURL because ultimately I want to untar it inline with something like:

$ curl --insecure https://github.com/pinard/Pymacs/tarball/v0.24-beta2 | tar zx

I found that the URL after redirecting turned out to be https://download.github.com/pinard-Pymacs-v0.24-beta1-0-gcebc80b.tar.gz, but I would like cURL to be smart enough to figure this out.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
saltycrane
  • 6,541
  • 6
  • 34
  • 43

5 Answers5

169

Use the -L option to follow redirects:

curl -L https://github.com/pinard/Pymacs/tarball/v0.24-beta2 | tar zx
tom
  • 21,844
  • 6
  • 43
  • 36
saltycrane
  • 6,541
  • 6
  • 34
  • 43
  • 4
    For `https`, you'll also likely need `-k`. – nicerobot Dec 29 '11 at 00:41
  • 1
    Why lately there are certificates problems on raw.github.com? I had problems to install homebrew and rvm on a new machine. I used to copy and paste from the homepage and was working. Now I get the certificate problem: ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" curl: (60) SSL certificate problem: unable to get local issuer certificate More details here: http://curl.haxx.se/docs/sslcerts.html curl performs SSL certificate verification by default, using a "bundle".. If you'd like to turn off curl's verification of the certificate, use the -k (or --insecure) option. – Chris Cinelli Sep 12 '13 at 18:58
  • Mmmm ... is there a possibility to include the sub-modules in the tarball as well? It seems, that for sub-modules only empty directories are created in the tarball. – aurora Oct 09 '13 at 08:18
  • Not sure if this is a temporary problem right now, but github is returning a 500 internal server error for this at the moment. – B T Oct 29 '13 at 21:15
  • 17
    And just to potentially save someone a google, `-L` means follow redirects. – Steve Kehlet May 16 '14 at 19:56
  • this unzips to a funky directory. possible to name that directory? Even better name it the version that was downloaded. – lostintranslation May 18 '17 at 20:29
  • wont this just download the html page that hosts the file? – Jonathon May 28 '22 at 22:05
50

The modernized way of doing this is:

curl -sL https://github.com/user-or-org/repo/archive/sha1-or-ref.tar.gz | tar xz

Replace user-or-org, repo, and sha1-or-ref accordingly.

If you want a zip file instead of a tarball, specify .zip instead of .tar.gz suffix.

You can also retrieve the archive of a private repo, by specifying -u token:x-oauth-basic option to curl. Replace token with a personal access token.

Pavel Repin
  • 30,663
  • 1
  • 34
  • 41
  • 3
    of all the answers, this was exactly what i was looking for! If you have an oauth token you can use that directly; `https://token@github.com/user-or-org/repo/archive/sha1-or-ref.tar.gz` – svenevs May 14 '17 at 20:01
  • how would this work for downloading from releases instead of archives? – waspinator Nov 26 '19 at 13:31
  • 2
    @waspinator , I use `curl -OL https://github.com/user-or-org/repo/releases/download/version/.tar.gz` See example [here](https://github.com/fomightez/gohugo-binder/blob/4713e967d083fc00f120b08715855912f52d6a61/postBuild#L49) – Wayne Jan 28 '20 at 03:59
  • Can you explain how this is better than the accepted answer? – Robin Métral May 07 '20 at 14:18
  • Maybe because `.../tarball/...` method only produces tar.gz? GH added a more flexible method later that allows you extract archive in format you want based on suffix `.tar.gz` or `.zip`. I don't remember any more why I wrote this post. – Pavel Repin Nov 09 '20 at 20:13
14

You can also use wget to »untar it inline«. Simply specify stdout as the output file (-O -):

wget --no-check-certificate https://github.com/pinard/Pymacs/tarball/v0.24-beta2 -O - | tar xz
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
knittl
  • 246,190
  • 53
  • 318
  • 364
3

All the other solutions require specifying a release/version number which obviously breaks automation.

This solution- currently tested and known to work with Github API v3- however can be used programmatically to grab the LATEST release without specifying any tag or release number and un-TARs the binary to an arbitrary name you specify in switch --one-top-level="pi-ap". Just swap-out user f1linux and repo pi-ap in below example with your own details and Bob's your uncle:

curl -L https://api.github.com/repos/f1linux/pi-ap/tarball | tar xzvf - --one-top-level="pi-ap" --strip-components 1
F1Linux
  • 3,580
  • 3
  • 25
  • 24
  • This is beautiful, but could really use some explanation. e.g. which GitHub API version is this and where is it documented? – l3l_aze Jul 22 '20 at 04:33
  • 1
    @l3l_aze Just edited the answer it include the current Githib API version number my solution is tested and known to work with. Thanks for your feedback!- T – F1Linux Aug 18 '20 at 09:53
  • Whoa. I didn't think this could possibly get better than what you'd already shared. Thank you very much! – l3l_aze Aug 18 '20 at 15:57
1

with a specific dir:

cd your_dir && curl -L https://download.calibre-ebook.com/3.19.0/calibre-3.19.0-x86_64.txz | tar zx
KyleMit
  • 30,350
  • 66
  • 462
  • 664
zhengquan
  • 39
  • 8
  • more better way ```cd your_dir && curl -L https://download.calibre-ebook.com/3.19.0/calibre-3.19.0-x86_64.txz | tar zx; cd -``` – zhengquan Mar 21 '18 at 06:00