Search code examples
pythonlinuxglibcmusl

How to determine which libc implementation the host system uses


In our Python setup code that uses pre-built binaries and a bindings generator, we check the operating system and CPU architecture, and download binaries accordingly.

Now, there are binaries for both manylinux (glibc) and musllinux (musl). How can we find out which libc implementation the host system uses? I am aware of platform.libc_ver(), but for musl hosts, it currently just returns two empty strings (see CPython issue 87414).

However, there has to be more precise code available already, as pip needs means to choose the right wheel.


Solution

  • This is the solution I am using now. It is insipred by @AndrewNelson's answer and also uses the platform module, but it is written as an overlay for platform.libc_ver() and calls into packaging._musllinux directly.

    import sys
    import platform
    import packaging._musllinux
    
    def get_libc_info():
        name, ver = platform.libc_ver()
        if not name:
            musl_ver = packaging._musllinux._get_musl_version(sys.executable)
            if musl_ver:
                name, ver = "musl", f"{musl_ver.major}.{musl_ver.minor}"
        return name, ver
    
    print( get_libc_info() )