cargo-bins/cargo-binstall

GitHub: cargo-bins/cargo-binstall

cargo-binstall 是一个 Rust 二进制工具的快速安装器,通过下载预编译产物替代从源码编译,大幅缩短安装时间。

Stars: 2578 | Forks: 97

# Cargo B(inary)Install Binstall 提供了一种低复杂度的机制来安装 Rust 二进制文件,作为从源代码构建(通过 `cargo install`)或手动下载包的替代方案。 这旨在与现有的 CI artifacts 和基础设施协同工作,并为包维护者带来极低的开销。 Binstall 的工作原理是从 `crates.io` 获取 crate 信息,并在关联的 `repository` 中搜索匹配的 release 和 artifacts,如果找不到,则会回退到 [quickinstall](https://github.com/alsuren/cargo-quickinstall) 第三方 artifact 托管服务,接着在支持的情况下回退到备用 targets,最终将 `cargo install` 作为最后的手段。 [![CI build](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/8fbe786c9d183053.svg)](https://github.com/cargo-bins/cargo-binstall/actions) [![GitHub tag](https://img.shields.io/github/tag/cargo-bins/cargo-binstall.svg)](https://github.com/cargo-bins/cargo-binstall/releases) [![Crates.io](https://img.shields.io/crates/v/cargo-binstall.svg)](https://crates.io/crates/cargo-binstall) _你可能想要[查看此页面发布最新版本时的状态](https://crates.io/crates/cargo-binstall)。_ ## 用法 ``` $ cargo binstall radio-sx128x@0.14.1-alpha.5 INFO resolve: Resolving package: 'radio-sx128x@=0.14.1-alpha.5' WARN The package radio-sx128x v0.14.1-alpha.5 (x86_64-unknown-linux-gnu) has been downloaded from github.com INFO This will install the following binaries: INFO - sx128x-util (sx128x-util-x86_64-unknown-linux-gnu -> /home/.cargo/bin/sx128x-util) Do you wish to continue? [yes]/no ? yes INFO Installing binaries... INFO Done in 2.838798298s ``` Binstall 旨在成为许多场景下 `cargo install` 的直接替代品,并支持类似的选项。 用于无人值守场景(例如在 CI 中),请使用 `--no-confirm` 标志。 有关更多选项,请参阅 `cargo binstall --help`。 ## 安装 ### 如果你已经安装了它 要升级 cargo-binstall,请使用 `cargo binstall cargo-binstall`! ### 快速安装 以下是一行命令,用于下载并安装预编译的 `cargo-binstall` 二进制文件。 #### Linux 和 macOS ``` curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash ``` 或者如果你安装了 [homebrew](https://brew.sh/): ``` brew install cargo-binstall ``` #### Windows ``` Set-ExecutionPolicy Unrestricted -Scope Process; iex (iwr "https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.ps1").Content ``` ### 手动安装 从下方下载适用于你系统的相应包,将其解压,并将 `cargo-binstall` 可执行文件移动到 `$HOME/.cargo/bin`: | OS | Arch | URL | | ------- | ------- | ------------------------------------------------------------ | | Linux | x86\_64 | https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-x86_64-unknown-linux-musl.tgz | | Linux | armv7 | https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-armv7-unknown-linux-musleabihf.tgz | | Linux | arm64 | https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-aarch64-unknown-linux-musl.tgz | | Mac | Intel | https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-x86_64-apple-darwin.zip | | Mac | Apple Silicon | https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-aarch64-apple-darwin.zip | | Mac | Universal
(both archs) | https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-universal-apple-darwin.zip | | Windows | Intel/AMD | https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-x86_64-pc-windows-msvc.zip | | Windows | ARM 64 | https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-aarch64-pc-windows-msvc.zip | ### 从源代码安装 在安装了最新版 [Rust](https://rustup.rs) 的情况下: ``` cargo install cargo-binstall ``` ### 在 GitHub Actions 中 我们提供了一个第一方的精简 action 来安装 Binstall: ``` - uses: cargo-bins/cargo-binstall@main with: version: "1.2.3" # optional; defaults to latest ``` 为了获取更多功能,我们推荐优秀的 [taiki-e/install-action](https://github.com/marketplace/actions/install-development-tools),它对选定的工具提供专门支持,并使用 Binstall 处理其余的所有工具。 ## 伴侣工具 这些是与 Binstall 配合良好的有用的*第三方*工具。 ### [`cargo-update`](https://github.com/nabijaczleweli/cargo-update) 虽然你可以通过再次运行 `cargo binstall` 来显式升级 crates,但 `cargo-update` 负责根据需要更新所有的工具。 如果环境中存在 Binstall,它会自动使用 Binstall 来安装更新。 ### [`cargo-run-bin`](https://github.com/dustinblackman/cargo-run-bin) Binstall 和 `cargo install` 默认都会全局安装工具,这对于系统级工具来说没问题。 然而,在为某个项目安装工具时,你可能更希望将这些工具的作用域限定在该项目中,并在代码中控制它们的版本。 这正是 `cargo-run-bin` 的用武之地,它提供了 Cargo.toml 中的专属配置段和一个简短的 cargo 子命令。 当 Binstall 可用时,它会尽可能通过二进制文件进行安装……你甚至可以用 `cargo-run-bin` 来管理 Binstall 本身! ## 不支持的 crates 在大多数情况下,Binstall 足够智能,可以自动检测 artifacts。 但是,如果某个包安装失败,你可以根据需要在命令行手动指定 `pkg-url`、`bin-dir` 和 `pkg-fmt`,其相关值记录在 [SUPPORT.md](https://github.com/cargo-bins/cargo-binstall/blob/main/SUPPORT.md) 中。 ``` $ cargo-binstall \ --pkg-url="{ repo }/releases/download/{ version }/{ name }-{ version }-{ target }.{ archive-format }" \ --pkg-fmt="txz" \ crate_name ``` 希望让用户更省心的维护者,可以在 `Cargo.toml` 中添加 [显式 Binstall 元数据](https://github.com/cargo-bins/cargo-binstall/blob/main/SUPPORT.md),以便为给定的版本和 target 定位到相应的二进制包。 ## 签名 我们为维护者提供了初步且有限的[支持](https://github.com/cargo-bins/cargo-binstall/blob/main/SIGNING.md),以便指定签名公钥以及获取包签名的位置。 启用此功能后,Binstall 将为该包下载并验证签名。 你可以使用 `--only-signed` 来拒绝安装未签名的包。 如果你喜欢冒险(请不要在测试环境之外使用),可以使用 `--skip-signatures` 来完全禁用检查甚至下载签名。 ## 常见问题 ### 为什么要使用这个? 因为使用 `wget` 下载 release 令人抓狂,`cargo install` 在资源受限的设备上要耗费漫长的时间,而且通常去组装实际意义上的_包_又有些小题大做。 ### 为什么要使用 cargo manifest? Crates 已经包含了这些信息,而且它们已经包含了所需信息的很大一部分。 此外,还有一个很棒但(在我看来)未被充分利用的 `[package.metadata]` 字段。 ### 这安全吗? 既安全也不安全? 我们已[初步支持](https://github.com/cargo-bins/cargo-binstall/blob/main/SIGNING.md)验证签名,但目前生态系统中生成签名的项目并不多。 请参阅 [#1](https://github.com/cargo-bins/cargo-binstall/issues/1) 以进行更多讨论。 我们始终通过 HTTPS 从 crates.io 拉取元数据,并验证 crate tar 包的校验和。 我们还强制要求在下载实际的包文件时使用 TLS >= 1.2 的 HTTPS。 与 `curl ... | sh` 脚本之类的方式相比,我们不会执行任意代码,当然,你正在下载包的那个 crate 本身可能是恶意的! ### 错误代码是什么意思? 你可以在此处找到包含退出代码在内的完整错误描述: ### 有调试符号吗? 有的! 提供了带有 `.full` 后缀的额外预编译包,其中包含分离的 debuginfo、文档文件以及像 `detect-wasi` 实用程序这样的额外二进制文件。 ## 遥测数据收集 默认情况下,某些 crate 安装策略可能会收集匿名化的使用统计数据。 当前,当使用 `quickinstall` artifact 托管时,只会将要安装的 crate 名称、其版本、目标平台三元组以及收集信息的 user agent 发送到 `https://cargo-quickinstall-stats-server.fly.dev/record-install` URL 下的端点。 `quickinstall` 项目的维护者使用这些数据来确定哪些 crate 版本最值得构建和托管。 汇总收集的遥测数据可在 公开访问。 如果你对此感兴趣,这些端点的后端代码可以在 找到。 如果你不想参与此数据收集,可以通过以下任一方式选择退出: - 在命令行界面中设置 `--disable-telemetry` 标志。 - 将 `BINSTALL_DISABLE_TELEMETRY` 环境变量设置为 `true`。 - 使用 `--disable-strategies quick-install` 禁用 `quickinstall` 策略,或者在使用 `--strategies` 指定要使用的策略列表时,避免在该列表中包含 `quickinstall`。 - 将 `quick-install` 添加到 crate 元数据中的 `disabled-strategies` 配置键中(有关更多详细信息,请参阅[相关支持文档](SUPPORT.md#support-for-cargo-binstall))。 如果你有任何想法/贡献,或者任何功能没有按预期工作(在这种情况下,请附上包含 `--log-level debug` 的输出),欢迎随时开启 issue 或 PR。
标签:Cargo, crates.io, GitHub发布, Rust, 二进制文件, 包管理器, 可视化界面, 威胁情报, 安装工具, 底层编程, 开发者工具, 源码构建替代方案, 统一API, 编译加速, 网络流量审计, 通知系统