---
description: Fork mainnet or any EVM-compatible chain with Anvil.
---

## Forking

Anvil can fork any EVM-compatible chain, allowing you to test against real-world state without deploying to a live network.

### Basic forking

```bash
$ anvil --fork-url https://ethereum.reth.rs/rpc
```

You can also fork from a specific point in the chain's history:

:::code-group

```bash [At block]
$ anvil --fork-url https://ethereum.reth.rs/rpc --fork-block-number 18000000
```

```bash [At transaction]
$ anvil --fork-url https://ethereum.reth.rs/rpc --fork-transaction-hash $TX_HASH
```

:::

### Chain configuration

When forking, Anvil inherits the chain ID from the remote endpoint by default.

```bash [Override chain ID]
$ anvil --fork-url https://ethereum.reth.rs/rpc --chain-id 1337
```

```bash [Specify hardfork]
$ anvil --fork-url https://ethereum.reth.rs/rpc --hardfork cancun
```

### Rate limiting

RPC providers often rate-limit requests. Anvil handles this automatically but you can tune the behavior:

```bash [Set compute units per second]
$ anvil --fork-url $RPC_URL --compute-units-per-second 100
```

```bash [Disable rate limiting]
$ anvil --fork-url $RPC_URL --no-rate-limit
```

```bash [Set retry count]
$ anvil --fork-url $RPC_URL --retries 10
```

### Caching

Anvil caches forked state to disk at `~/.foundry/cache/rpc/<chain>/<block>/` to speed up subsequent runs.

```bash [Disable storage caching]
$ anvil --fork-url $RPC_URL --no-storage-caching
```

### Custom headers

Some RPC providers require authentication headers:

```bash [Add custom header]
$ anvil --fork-url $RPC_URL --fork-header "Authorization: Bearer $TOKEN"
```

### L2 support

Anvil can fork L2 networks directly. The network family is inferred from the forked chain ID, so forking a network like Optimism needs no extra flags:

```bash [Fork Optimism]
$ anvil --fork-url https://mainnet.optimism.io
```

```bash [Enable Celo features]
$ anvil --fork-url https://forno.celo.org --celo
```

Use `--network` to select a network family explicitly; see the [`anvil` reference](/reference/anvil/anvil) for the supported values.
