Cabal: Missing dependency on a foreign library: zlib.h

Created on 8 Sep 2019  Â·  10Comments  Â·  Source: haskell/cabal

Hi,

I'm getting this error while trying to compile a project:

Configuring library for zlib-0.6.2.1..
cabal: Missing dependency on a foreign library:
* Missing (or bad) header file: zlib.h
* Missing (or bad) C library: z
Build profile: -w ghc-8.6.5 -O1

I have seen people suggesting that installing the package libghc-zlib-dev fixes the issue. The problem is that I already have this package installed, however it's a newer version:

dpkg -l | grep zlib
ii  libghc-zlib-dev                            0.6.2-2                                 amd64        Compression and decompression in the gzip and zlib formats
ii  zlib1g:amd64                               1:1.2.11.dfsg-1ubuntu2                  amd64        compression library - runtime
ii  zlib1g:i386                                1:1.2.11.dfsg-1ubuntu2                  i386         compression library - runtime
ii  zlib1g-dev:amd64                           1:1.2.11.dfsg-1ubuntu2                  amd64        compression library - development

I'm on Ubuntu 19.04. Does anyone know how to fix this issue?

Thanks in advance,
Gabriel.

Most helpful comment

I'm just getting started

Oh, sorry, I'll go into a bit more of detail then.

nix-env -i installs packages by name by searching for matching ones.

nix-env -iA installs packages by their attribute path, so for example nixpkgs.zlib. Other than being faster since it does not have to search, it can be used to specify subpackages or in general any attribute path in nixpkgs. So, to install zlib.dev you do nix-env -iA nixpkgs.zlib.dev

Finally, ghc.withPackages (hp: with hp; [ zlib ]) is a (part of a) nix expression which tells nix to install ghc _and_ a global environment containing the zlib haskell package. You can install it by first removing the old ghc with nix-env -e and then running nix-env -i -f '<nixpkgs>' -E 'pkgs: (pkgs {}).ghc.withPackages (hp: with hp; [ zlib ])'.

Where do I find such configuration file?

configuration.nix is only for NixOS, it is used to specify your system declaratively

All 10 comments

This is strange; after installing zlib1g-dev the error above should have been resolved.

How did you install GHC? Was it via https://launchpad.net/~hvr/+archive/ubuntu/ghc or some other means?

Can you provide the verbose logging output (via -v3) for when cabal fails to locate zlib?

I installed both using nix:

$ which cabal
/home/gvolpe/.nix-profile/bin/cabal
$ which ghc
/home/gvolpe/.nix-profile/bin/ghc

Here's the output of cabal new-build -v3: https://pastebin.com/7midLWws

What happens when you also install the zlib and zlib.dev nix packages?


A bit of a hack, but installing 'ghc.withPackages (hp: with hp; [ zlib ])' instead of ghc should make it work in the meantime. I also had that problem so I have that line in my configuration.nix. For some reason other libraries work correctly.

I installed zlib using nix-pkgs too, but I get an error when trying to install zlib.dev.

$ nix-env -i zlib
replacing old 'zlib-1.2.11'
installing 'zlib-1.2.11'
$ nix-env -i zlib.dev
error: selector 'zlib.dev' matches no derivations

I'm on the unstable channel:

$ nix-channel --list
nixpkgs https://nixos.org/channels/nixpkgs-unstable

I'm just getting started with nixpkgs in Ubuntu. Where do I find such configuration file?

I'm just getting started

Oh, sorry, I'll go into a bit more of detail then.

nix-env -i installs packages by name by searching for matching ones.

nix-env -iA installs packages by their attribute path, so for example nixpkgs.zlib. Other than being faster since it does not have to search, it can be used to specify subpackages or in general any attribute path in nixpkgs. So, to install zlib.dev you do nix-env -iA nixpkgs.zlib.dev

Finally, ghc.withPackages (hp: with hp; [ zlib ]) is a (part of a) nix expression which tells nix to install ghc _and_ a global environment containing the zlib haskell package. You can install it by first removing the old ghc with nix-env -e and then running nix-env -i -f '<nixpkgs>' -E 'pkgs: (pkgs {}).ghc.withPackages (hp: with hp; [ zlib ])'.

Where do I find such configuration file?

configuration.nix is only for NixOS, it is used to specify your system declaratively

Thanks a lot for your detailed response!

I followed your recommended steps and indeed something happened because I no longer get the zlib error. However, the project still doesn't compile using cabal new-build and the error message doesn't help much. Any ideas?

Warning: Failed to build documentation for basement-0.0.11 (which is required
by exe:musikell from musikell-0.1.0.0).
Failed to build documentation for prettyprinter-ansi-terminal-1.1.1.2 (which
is required by exe:musikell from musikell-0.1.0.0).

FWIW using nix-shell --run 'cabal new-build' it compiles without issues but I want it to compile without nix as well.

Here's the last part of the verbose output:

Redirecting build log to {handle:
/home/gvolpe/.cabal/logs/ghc-8.6.5/basement-0.0.11-780b096067d775ef7b58f88db7134361f8e2ce911b9bd5ef2229940b196ad2e7.log}
CallStack (from HasCallStack):
  die', called at ./Distribution/Client/ProjectOrchestration.hs:1002:55 in main:Distribution.Client.ProjectOrchestration
cabal: Failed to build RSA-2.3.1 because it depends on RSA-2.3.1 which itself
failed to build.
Failed to build asn1-encoding-0.9.5 because it depends on asn1-encoding-0.9.5
which itself failed to build.
Failed to build asn1-parse-0.9.4 because it depends on asn1-parse-0.9.4 which
itself failed to build.
Failed to build asn1-types-0.3.3 because it depends on asn1-types-0.3.3 which
itself failed to build.
Failed to build authenticate-oauth-1.6 because it depends on
authenticate-oauth-1.6 which itself failed to build.
Failed to build documentation for
basement-0.0.11-780b096067d775ef7b58f88db7134361f8e2ce911b9bd5ef2229940b196ad2e7.
Failed to build connection-0.3.1 because it depends on connection-0.3.1 which
itself failed to build.
Failed to build crypto-pubkey-types-0.4.3 because it depends on
crypto-pubkey-types-0.4.3 which itself failed to build.
Failed to build cryptonite-0.26 because it depends on cryptonite-0.26 which
itself failed to build.
Failed to build dhall-1.24.0 because it depends on dhall-1.24.0 which itself
failed to build.
Failed to build hasbolt-0.1.3.5 because it depends on hasbolt-0.1.3.5 which
itself failed to build.
Failed to build http-client-0.6.4 because it depends on http-client-0.6.4
which itself failed to build.
Failed to build http-client-tls-0.3.5.3 because it depends on
http-client-tls-0.3.5.3 which itself failed to build.
Failed to build memory-0.14.18 because it depends on memory-0.14.18 which
itself failed to build.
Failed to build musikell-0.1.0.0 because it depends on musikell-0.1.0.0 which
itself failed to build.
Failed to build musikell-0.1.0.0 because it depends on musikell-0.1.0.0 which
itself failed to build.
Failed to build pem-0.2.4 because it depends on pem-0.2.4 which itself failed
to build.
Failed to build documentation for
prettyprinter-ansi-terminal-1.1.1.2-beb1b8bee5bbafa74e6cc7e7bde5f0c63afa2fdc44ae0955a4c0b254732a441a.
Failed to build socks-0.6.0 because it depends on socks-0.6.0 which itself
failed to build.
Failed to build tls-1.5.1 because it depends on tls-1.5.1 which itself failed
to build.
Failed to build wreq-0.5.3.2 because it depends on wreq-0.5.3.2 which itself
failed to build.
Failed to build x509-1.7.5 because it depends on x509-1.7.5 which itself
failed to build.
Failed to build x509-store-1.6.7 because it depends on x509-store-1.6.7 which
itself failed to build.
Failed to build x509-system-1.6.6 because it depends on x509-system-1.6.6
which itself failed to build.
Failed to build x509-validation-1.6.11 because it depends on
x509-validation-1.6.11 which itself failed to build.

Update:

After learnt more about nixpkgs, I think the problem is expected as documented in

https://nixos.org/nixpkgs/manual/

15.9.4.7. Builds using Stack complain about missing system libraries

...

When you run the build inside of the nix-shell environment, the system is configured to find libz.so without any special flags – the compiler and linker “just know” how to find it. Consequently, Cabal won’t record any search paths for libz.so in the package description, which means that the package works fine inside of nix-shell, but once you leave the shell the shared object can no longer be found. That issue is by no means specific to Stack: you’ll have that problem with any other Haskell package that’s built inside of nix-shell but run outside of that environment.

You can remedy this issue in several ways. The easiest is to add a nix section to the stack.yaml like the following:

...

I don't feel I fully understand all said there, but I suppose the problem applies to ghc built (thus installed) by nix, from my own experience.

unfortunately none of the solutions documented there is pleasant imho, per my understands by far, you'd better stick to nix-shell if you prefer the ghc installed by nix, or better install ghc from another source than nix, to avoid this issue.

I'm searching nix integration related issues and see this, I had the same problem, and I found simply add

{
# ...
  # to workaround missing zlib.h problem during build
  librarySystemDepends = [ pkgs.zlib ];
}

to your <pkg>.nix (whether generated by cabal2nix or crafted by hand) can workaround it.

hope helpful to someone.

How does this work when using cabal2nix. Is there a way to make the package.yaml specify the correct zlib to get.

This should probably be closed as it's a nix related issues (that seems to be solved in the link thread).

Was this page helpful?
0 / 5 - 0 ratings