Failed to build embedding python into rust on musl
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.2k
- Forks
- 1k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 66
Description
Bug Description
I want to build a rust binary, that embeds python into it on musl (to remove dependency on glibc), but with python dynamically linked
Build fails for many reasons, but I think the main culprit is that pyo3 tries to statically link python and fails
Steps to Reproduce
I'll use musl docker container for reproducibility
docker run --rm -it ghcr.io/rust-cross/rust-musl-cross:x86_64-musl(this container already has python preinstalled)- From inside the docker container
python3 --version(prints3.10.12)cargo new fail && cd failcargo add pyo3echo "fn main() { pyo3::prepare_freethreaded_python(); }" > src/main.rscargo build<- fails
Backtrace
Compiling fail v0.1.0 (/home/rust/src/fail)
error: linking with `x86_64-unknown-linux-musl-gcc` failed: exit status: 1
|
= note: "x86_64-unknown-linux-musl-gcc" "-m64" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtbeginS.o" "/tmp/rustc03FHyi/symbols.o" "<6 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/home/rust/src/fail/target/x86_64-unknown-linux-musl/debug/deps/{libpyo3-e5926577516ce3f2.rlib,libmemoffset-32eaddb04f7f553b.rlib,libonce_cell-603ce8c4b62f720c.rlib,libpyo3_ffi-b617245effe3a002.rlib}.rlib" "-lpython3.10" "/home/rust/src/fail/target/x86_64-unknown-linux-musl/debug/deps/{liblibc-ab1ac9a389ae22ed.rlib,libunindent-a53022775b977239.rlib}.rlib" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*}.rlib" "-lunwind" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/{libcfg_if-*,liblibc-*}.rlib" "-lc" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/{liballoc-*,librustc_std_workspace_core-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-nostartfiles" "-L" "/usr/lib/x86_64-linux-gnu" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib" "-o" "/home/rust/src/fail/target/x86_64-unknown-linux-musl/debug/deps/fail-b7b48d50e1e1f9c3" "-Wl,--gc-sections" "-static-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtendS.o" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
= note: some arguments are omitted. use `--verbose` to show all linker arguments
= note: /usr/local/musl/bin/../lib/gcc/x86_64-unknown-linux-musl/11.2.0/../../../../x86_64-unknown-linux-musl/bin/ld: /usr/lib/x86_64-linux-gnu/libpython3.10.a(dynload_shlib.o): in function `_PyImport_FindSharedFuncptr':
(.text+0x8b): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/local/musl/bin/../lib/gcc/x86_64-unknown-linux-musl/11.2.0/../../../../x86_64-unknown-linux-musl/bin/ld: /usr/lib/x86_64-linux-gnu/libpython3.10.a(posixmodule.o): in function `os_getgrouplist':
(.text.unlikely+0x562c): warning: Using 'getgrouplist' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/local/musl/bin/../lib/gcc/x86_64-unknown-linux-musl/11.2.0/../../../../x86_64-unknown-linux-musl/bin/ld: /usr/lib/x86_64-linux-gnu/libpython3.10.a(posixmodule.o): in function `os_initgroups':
[...]
and many other linker errors
[...]
Your operating system and version
Ubuntu 22.04
Your Python version (python --version)
Python 3.10.12
Your Rust version (rustc --version)
rustc 1.86.0 (05f9846f8 2025-03-31)
Your PyO3 version
0.25.1
How did you install python? Did you use a virtualenv?
Looking at https://github.com/rust-cross/rust-musl-cross/blob/main/Dockerfile
apt-get install python3
Additional Info
I also tried to do this with uv and it's managed pythons, and I got it built for regular linux, but when compiling for musl it just fails with
/usr/bin/ld: cannot find -lpython3.12: No such file or directory
Although regular build doesn't fail and finds libpython3.12.so easily. I would assume this error not about .so library, but static one, but then I don't even know why regular build doesn't fail (doesn't it link statically too?)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure with the ghcr.io/rust-cross/rust-musl-cross:x86_64-musl container, a new Cargo project, PyO3 0.25.1, and the shown main.rs, then inspect the emitted linker arguments and Python library selection. Done means the documented musl build embeds dynamically linked Python without the shown static glibc-related linker failures.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, linux, python, rust
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100