Skip to content

Load libasound at runtime, or make the ALSA host optional, so Linux binaries don't require libasound #1421

Description

@kixelated

Problem

On Linux and the BSDs, cpal always depends on alsa, and alsa-sys links libasound through pkg-config. So every binary that uses cpal has libasound.so.2 in NEEDED, and that has two costs:

  • At run time, the binary won't start without libasound (error while loading shared libraries: libasound.so.2), even when audio is an optional part of a larger program and the host never touches a device: a headless server, a minimal container, a CI runner.
  • At build time, every build needs the ALSA headers and pkg-config (libasound2-dev), including cargo install on a machine that only wants the non-audio parts. This also gets in the way of cross and static builds (see #982 and diwic/alsa-sys#10).

The ALSA host's is_available() is the natural place to report this, but today it returns a hardcoded true.

Proposal

Either of these would solve it. The first is preferred, because it keeps raw ALSA on hosts without a sound server.

  1. Load libasound at runtime (dlopen). An opt-in feature in which the ALSA host resolves the snd_* symbols from libasound.so.2 on first use. is_available() returns false when the library is missing, Host::new() returns HostUnavailable, and default_host()/available_hosts() fall through. The loading would most naturally live in alsa-sys behind a feature (bindgen can generate a dynamic-loading wrapper), and cpal would forward it. The build then needs no ALSA headers or pkg-config either. This is how other audio stacks handle it: SDL (SDL_AUDIO_DRIVER_ALSA_DYNAMIC), miniaudio (runtime linking by default), and Firefox's cubeb all dlopen libasound.
  2. Make the ALSA host optional on Linux. A default-on alsa feature, so a build with default-features = false, features = ["pulseaudio"] uses only the PulseAudio host. That host is built on the pure-Rust pulseaudio protocol crate, so the build needs no system audio library at all. The cost is that hosts without a sound server have no devices. This is close to the alsa feature being considered for FreeBSD in #1404 (also mentioned in #988 and asked about in #929), extended to Linux.

Use case

moq ships moq-cli, a tool for publishing and playing live media, as prebuilt binaries and through cargo install. Microphone capture and speaker playback go through cpal (0.18.2). On Linux, cpal is the one dependency that links a system library at load time: camera capture needs none, and our hardware video codecs (VA-API, NVENC) already dlopen theirs. So capture and playback are off by default today. Turning them on would make every Linux user install libasound2-dev to cargo install, and the same binary that runs on a desktop wouldn't start on a server or in a container without ALSA, even when it's only publishing a file. We'd like to ship one Linux binary that uses ALSA (or PulseAudio) where it exists and reports no audio host where it doesn't.


(Written by Opus 5.5)

Activity

  1. Marco-Farruggio commented on Nov 4, 2026

    @Marco-Farruggio
    Collaborator

    I have quite a few related things in consideration regarding this and similar (especially the behaviour of an alsa feature flag).

    One of those things is the infallibility of default_host(), on a Linux build with no running Pulse/Pw server, and missing alsa files (e.g, headless server), default_host() must panic, which is obviously not ideal at all, this is not a problem for people who manually select hosts, other than that may end up without any host.

    Is static linking a possibility? If it is, would it grow the file too much?

    As for proposal 2: I see the rationale here and it would definitely work, but it would break existing builds, and alsa being default-on for FreeBSD seems illogical to me, and also for android/tmux, there are plans to make compilation with ALSA possible on there, and again, making alsa default on android seems illogical to me, (AFAIAA there is no way to make default-features different per platform, plus doing so could be very odd even if it was possible)

    I need to think this through a bit more, but any suggestions would be much appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions