You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用messense/rust-musl-cross构建Rust应用遇OpenSSL未找到错误求助

问题描述

尝试用messense/rust-musl-cross构建Rust静态应用替代已废弃的rust-musl-builder,但构建时出现openssl依赖找不到的错误:

#9 13.77   run pkg_config fail: `PKG_CONFIG_ALLOW_SYSTEM_CFLAGS="1" PKG_CONFIG_LIBDIR="/usr/local/musl/x86_64-unknown-linux-musl/lib/pkgconfig:/usr/local/musl/lib/pkgconfig" PKG_CONFIG_PATH="/usr/local/musl/x86_64-unknown-linux-musl/lib/pkgconfig:/usr/local/musl/lib/pkgconfig" PKG_CONFIG_SYSROOT_DIR="/usr/local/musl/x86_64-unknown-linux-musl" "pkg-config" "--libs" "--cflags" "openssl"` did not exit successfully: exit status: 1
#9 13.77   error: could not find system library 'openssl' required by the 'openssl-sys' crate
#9 13.77 
#9 13.77   --- stderr
#9 13.77   Package openssl was not found in the pkg-config search path.
#9 13.77   Perhaps you should add the directory containing `openssl.pc'
#9 13.77   to the PKG_CONFIG_PATH environment variable
#9 13.77   No package 'openssl' found
#9 13.77 
#9 13.77 
#9 13.77   --- stderr
#9 13.77   thread 'main' panicked at '
#9 13.77 
#9 13.77   Could not find directory of OpenSSL installation, and this `-sys` crate cannot
#9 13.77   proceed without this knowledge. If OpenSSL is installed and this crate had
#9 13.77   trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
#9 13.77   compilation process.
#9 13.77 
#9 13.77   Make sure you also have the development packages of openssl installed.
#9 13.77   For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.
#9 13.77 
#9 13.77   If you're in a situation where you think the directory *should* be found
#9 13.77   automatically, please open a bug at https://github.com/sfackler/rust-openssl
#9 13.77   and include information about your system as well as this message.
#9 13.77 
#9 13.77   $HOST = aarch64-unknown-linux-gnu
#9 13.77   $TARGET = x86_64-unknown-linux-musl
#9 13.77   openssl-sys = 0.9.90
#9 13.77 
#9 13.77   ', /root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/openssl-sys-0.9.90/build/find_normal.rs:190:5
#9 13.77   note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
#9 13.77 warning: build failed, waiting for other jobs to finish...
------
executor failed running [/bin/sh -c cargo build --release --target=x86_64-unknown-linux-musl]: exit code: 101

相关文件

Dockerfile

ARG BASE_IMAGE=messense/rust-musl-cross:x86_64-musl

FROM ${BASE_IMAGE} AS builder

WORKDIR /app

COPY . .

RUN sudo apt-get update && apt-get install pkg-config -y

RUN cargo build --release --target=x86_64-unknown-linux-musl

Cargo.toml

[package]
name = "alt-server"
version = "0.1.0"
edition = "2021"

[dependencies]
actix-web = "4"
tokio = { version = "1.17.0", features = ["full"] }
diesel = { version = "2.0.4", features = ["postgres","64-column-tables","chrono","serde_json"] }
reqwest = { version = "0.11.18", features = ["json"] }
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"

main.rs

fn main() {
    println!("hello world!")
}
解决方案

问题根源是reqwest默认依赖系统原生OpenSSL库,但messense/rust-musl-cross镜像未预装适配musl目标的OpenSSL开发包,且系统默认的libssl-dev是针对glibc编译的,无法用于musl静态构建。以下两种方式可解决问题:

方式一:切换reqwest使用纯Rust实现的rustls

修改Cargo.toml中reqwest的配置,启用rustls-tls特性并禁用默认的原生TLS依赖:

reqwest = { version = "0.11.18", features = ["json", "rustls-tls"], default-features = false }

rustls无需依赖系统OpenSSL,完全由Rust代码实现,能完美适配musl静态编译,彻底规避系统库依赖问题。

方式二:安装musl版OpenSSL并指定环境变量

若必须使用原生OpenSSL,需在Dockerfile中安装适配x86_64-musl的OpenSSL开发包,并设置环境变量让openssl-sys找到它:

ARG BASE_IMAGE=messense/rust-musl-cross:x86_64-musl

FROM ${BASE_IMAGE} AS builder

WORKDIR /app

COPY . .

# 安装musl兼容的openssl开发包及编译工具
RUN sudo apt-get update && sudo apt-get install -y pkg-config x86_64-linux-musl-gcc libssl-dev:i386
# 设置环境变量指定openssl路径及跨编译允许标志
ENV OPENSSL_DIR=/usr/lib/x86_64-linux-musl
ENV PKG_CONFIG_ALLOW_CROSS=1

RUN cargo build --release --target=x86_64-unknown-linux-musl

此方式可能引入静态编译兼容性问题,不如方式一简洁可靠。

额外提示

  • 优先选择纯Rust实现的依赖库,减少对系统库的依赖,更适配musl静态编译场景。
  • messense/rust-musl-cross镜像已配置好基本的musl编译环境,切换rustls后通常可直接编译通过。

内容的提问来源于stack exchange,提问作者Dolphin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 15:27:48