It’s the dreaded cabal: Codec.Compression.Zlib: premature end of compressed stream error. Googling for this error shows you advice dating years back, asking you to check your network connection, inspect the output of cabal update -v 3, removing the ~/.cabal/packages/hackage.haskell.org/00-index.tar.gz file, and so on.

I don’t know about you, but none of the advice / answers worked for me. Don’t get me wrong, it’s sound advice, but it’s completely useless if the same error pops up when I do another cabal update followed by cabal install.

However, let’s take a closer look at the error message:

cabal: Codec.Compression.Zlib: premature end of compressed stream

and combine this with the advice of removing the ~/.cabal/packages/hackage.haskell.org/00-index.tar.gz file. Does that ring a bell?

On curiosity (and frustration after cabal install failed repeatedly), I thought the error might have to do with uncompressing some other compressed file that is not ~/.cabal/packages/hackage.haskell.org/00-index.tar.gz, and set out to execute:

find ~/.cabal -type f -name '*.gz'

and I was rewarded with a whole bunch of tar.gz files under ~/.cabal/packages/hackage.haskell.org in their package directories.

I was impatient and quite frustrated at this point, so I proceeded with:

cd ~/.cabal/packages/hackage.haskell.org
rm -rf *

thereby removing everything, under ~/.cabal/packages/hackage.haskell.org, whether good or bad.

This is followed by:

cabal update

After which, I proceeded with the cabal install, which successfully installs the package.

As such, my guess is correct. Sometar.gz file within ~/.cabal/packages/hackage.haskell.org is corrupted, causing the error, just not ~/.cabal/packages/hackage.haskell.org/00-index.tar.gz.

Why do I call this the easy but time consuming way? Because we delete all previously downloaded packages, regardless of whether they are corrupted or otherwise. Subsequent cabal operations could have made use of the working packages. Now, missing packages must be downloaded, which does take a little time.

A more clever solution will be to check every single tar.gz file under ~/.cabal/packages/hackage.haskell.org and only delete the corrupted ones. This should not be too difficult to do.