Yan Han's blog

On Computer Technology

04 Mar 2014

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

TLDR

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

cd ~/.cabal/packages/hackage.haskell.org
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-0.1.1.3...
Building zlib-bindings-0.1.1.3...
Preprocessing library zlib-bindings-0.1.1.3...
[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-0.1.1.3...
Installing library in
/home/yh/haskellsandbox/hakyll-4.4.2.0/.cabal-sandbox/lib/x86_64-linux-ghc-7.4.1/zlib-bindings-0.1.1.3
Registering zlib-bindings-0.1.1.3...
Installed zlib-bindings-0.1.1.3
cabal: Codec.Compression.Zlib: premature end of compressed stream
Done.
Failed to install 'hakyll-4.4.2.0'

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. Some tar.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.

Disclaimer: Opinions expressed on this blog are solely my own and do not express the views or opinions of my employer(s), past or present.

comments powered by Disqus