How to solve "cabal: Codec.Compression.Zlib: premature end of compressed stream" - the easy but time consuming way

04 Mar 2014, by Pang Yan Han


If you are already extremely frustrated by this problem, the solution is just:

cd ~/.cabal/packages/
rm -rf *
cabal update

followed by a cabal install X where X is some package you’d like to install.

Longer version

Doesn’t it irk you when you do a cabal install somepackage, and somewhere along the way you see something like:

Configuring zlib-bindings-
Building zlib-bindings-
Preprocessing library zlib-bindings-
[1 of 2] Compiling Codec.Zlib.Lowlevel ( Codec/Zlib/Lowlevel.hs, dist/dist-sandbox-a9ff9784/build/Codec/Zlib/Lowlevel.o )
[2 of 2] Compiling Codec.Zlib       ( Codec/Zlib.hs, dist/dist-sandbox-a9ff9784/build/Codec/Zlib.o )
In-place registering zlib-bindings-
Installing library in
Registering zlib-bindings-
Installed zlib-bindings-
cabal: Codec.Compression.Zlib: premature end of compressed stream
Failed to install 'hakyll-'

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/ 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/ 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/, 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/ in their package directories.

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

cd ~/.cabal/packages/
rm -rf *

thereby removing everything, under ~/.cabal/packages/, 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. Some tar.gz file within ~/.cabal/packages/ is corrupted, causing the error, just not ~/.cabal/packages/

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/ and only delete the corrupted ones. This should not be too difficult to do.

comments powered by Disqus