# 安装 foundry

Foundry 是一个智能合约开发工具链。

Foundry 管理你的依赖关系、编译项目、运行测试、部署，并允许你通过命令行和 Solidity 脚本与链交互。

登链社区

[**<mark>https://learnblockchain.cn/docs/foundry/i18n/zh/index.html</mark>**](https://learnblockchain.cn/docs/foundry/i18n/zh/index.html)

HITA WEB3 学习社区

[**<mark>https://binschool.org/</mark>**](https://binschool.org/)

---

* 使用Foundryup
    

但foundryup运行错误，只能换一种方法

**<mark>$ curl -L </mark>** [**<mark>https://foundry.paradigm.xyz</mark>**](https://foundry.paradigm.xyz) **<mark> | bash</mark>**

% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed

100 167 100 167 0 0 162 0 0:00:01 0:00:01 --:--:-- 163

100 2196 100 2196 0 0 1212 0 0:00:01 0:00:01 --:--:-- 1072k

Installing foundryup... curl: (56) Recv failure: 连接被对方重设

---

* 从源代码构建
    

先决条件。你需要 Rust 编译器和 Cargo，Rust 的包管理器。最简单的安装方法是使用 [rustup.rs](http://rustup.rs)

curl --proto '=https' --tlsv1.2 -sSf [https://sh.rustup.rs](https://sh.rustup.rs) | sh

$rustup

rustup 1.28.2 (e4f3ad6f8 2025-04-28) The Rust toolchain installer

$ rustup -V

rustup 1.28.2 (e4f3ad6f8 2025-04-28)

info: This is the version for the rustup toolchain manager, not the rustc compiler.

info: The currently active `rustc` version is `rustc 1.87.0 (17067e9ac 2025-05-09)`

使用 rustup 更新

$rustup update stable

使用 Cargo 通过以下命令安装

$cargo install --git [https://github.com/foundry-rs/foundry](https://github.com/foundry-rs/foundry) --profile release --locked forge cast chisel anvil

大约需要等待60分钟，太慢，最后一步卡住 也是需要更换国内源

[**<mark>https://www.cnblogs.com/mxnote/p/18117962</mark>**](https://www.cnblogs.com/mxnote/p/18117962)

rust 更换国内源

```plaintext
步骤一：设置 Rustup 镜像， 修改配置 ~/.zshrc or ~/.bashrc （配置环境变量）
export RUSTUP_DIST_SERVER="https://rsproxy.cn"
export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"

步骤二：设置 crates.io 镜像， 修改配置 ~/.cargo/config，已支持git协议和sparse协议，>=1.68 版本建议使用 sparse-index，速度更快。
[source.crates-io]
replace-with = 'rsproxy-sparse'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
[net]
git-fetch-with-cli = true
```

即便更换国内源，也还是卡死在 Building \[=======================&gt; \] 968/969: forge(bin)

---

* 从 Foundry 仓库的本地副本手动构建
    

$ git clone [https://github.com/foundry-rs/foundry.git](https://github.com/foundry-rs/foundry.git)

正克隆到 'foundry'...

致命错误：无法访问 '[https://github.com/foundry-rs/foundry.git/'：Recv](https://github.com/foundry-rs/foundry.git/'：Recv) failure: 连接被对方重设

通过Github Proxy加速 [**<mark>https://github.akams.cn/</mark>**](https://github.akams.cn/)

$ git clone [https://tvv.tw/https://github.com/foundry-rs/foundry.git](https://tvv.tw/https://github.com/foundry-rs/foundry.git)

正克隆到 'foundry'...

remote: Enumerating objects: 92539, done.

remote: Counting objects: 100% (15443/15443), done.

remote: Compressing objects: 100% (1834/1834), done.

remote: Total 92539 (delta 15147), reused 13609 (delta 13609), pack-reused 77096 (from 3)

接收对象中: 100% (92539/92539), 61.23 MiB | 4.42 MiB/s, 完成.

处理 delta 中: 100% (67976/67976), 完成.

```plaintext
# 克隆仓库
git clone https://github.com/foundry-rs/foundry.git
cd foundry
# 安装 Forge
cargo install --path ./crates/forge --profile release --force --locked
# 安装 Cast
cargo install --path ./crates/cast --profile release --force --locked
# 安装 Anvil
cargo install --path ./crates/anvil --profile release --force --locked
# 安装 Chisel
cargo install --path ./crates/chisel --profile release --force --locked
```

同样卡死在 Building \[=======================&gt; \] 968/969: forge(bin)

---

* 二进制文件安装
    

最终还是选择：使用二进制文件安装

从 GitHub 发布页面 获取预编译的二进制文件 [https://github.com/foundry-rs/foundry/releases](https://github.com/foundry-rs/foundry/releases)

选择linux对应的版本，再通过github proxy加速，下载压缩包后解压

添加目录到PATH变量

nano ~/.bash\_profile

在文件末尾添加以下行

export PATH=$PATH:/path/to/directory

替换 /path/to/directory 为您想要添加的目录的实际路径

保存文件并关闭编辑器

使配置文件的更改生效

source ~/.bashrc

```plaintext
$ wget https://tvv.tw/https://github.com/foundry-rs/foundry/releases/download/nightly-4435b4602bcbcf2b2e0e26b2fbc6626fe2e0b3ad/foundry_nightly_linux_amd64.tar.gz
2025-06-18 12:50:15 (9.13 MB/s) - 已保存 “foundry_nightly_linux_amd64.tar.gz” [70042458/70042458])
$ gzip -d foundry_nightly_linux_amd64.tar.gz 
$ mkdir foundry
$ tar -xvf foundry_nightly_linux_amd64.tar -C foundry
forge
cast
anvil
chisel
$ nano .bashrc
$ source .bashrc
$ forge
Build, test, fuzz, debug and deploy Solidity contracts
$ cast
A Swiss Army knife for interacting with Ethereum applications from the command line
```

终于成功
