Linux musl 构建最简方案
一次性命令(推荐)
# 如果没安装 rust,先安装
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
# 添加目标并安装工具链
rustup target add x86_64-unknown-linux-musl
sudo apt install musl-tools # Ubuntu/Debian
# 一行命令构建(取消代理 + 使用系统 Git)
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY && \
cargo --config net.git-fetch-with-cli=true build --release --target x86_64-unknown-linux-musl
如果经常使用,设置别名
# 添加到 ~/.bashrc
alias cargo-musl='unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY && cargo --config net.git-fetch-with-cli=true build --release --target x86_64-unknown-linux-musl'
# 使用
cargo-musl
清理(如果需要)
cargo clean && cargo-musl
Fix: Add the musl target to your Rust toolchain
bash
# Add the musl target
rustup target add x86_64-unknown-linux-musl
⚠️ But wait — musl also needs system tooling
On WSL/Debian/Ubuntu, statically linking with musl requires additional packages:
## Then retry your build
搞定。