<!-- Canonical: https://docs.linea.build/network/how-to/deploy-smart-contract/foundry -->

> For the complete Linea documentation index, see [llms.txt](/llms.txt).
> Agents can fetch this page as Markdown at [https://docs.linea.build/network/how-to/deploy-smart-contract/foundry.md](https://docs.linea.build/network/how-to/deploy-smart-contract/foundry.md).

# Foundry

In this quickstart, we'll create a basic [Foundry](https://book.getfoundry.sh/) project. Here's a video walkthrough:

## Prerequisites

Before you begin, ensure you download and install Foundry:

```bash
curl -L https://foundry.paradigm.xyz | bash
```

Then, open a new terminal, and call `foundryup` to install the latest release.

## Create a Foundry project

To create a Foundry project, run:

```bash
forge init linea-tutorial
```

Running `forge init` sets up a sample contract, test, and script for `Counter.sol`.

Now change into the directory:

```bash
cd linea-tutorial
```

## Deploy a smart contract

Public endpoints are rate limited and not meant for production systems

We recommend using a node provider such as Infura. [Sign up for an Infura account](https://docs.metamask.io/developer-tools/dashboard/) to generate an API key. In the dashboard, enable the Linea endpoints you want to use with that key.

These instructions use API keys and private keys inline. We highly recommend hiding them [in `.env` files](#deploy-a-smart-contract-using-a-env-file)

Deploy your contract using the following syntax:

```bash
forge create --rpc-url YOUR_LINEA_ENDPOINT src/Counter.sol:Counter --private-key YOUR_PRIVATE_KEY
```

In the command:

-   Replace `YOUR_LINEA_ENDPOINT` with the URL of a [supported Infura Linea network](https://docs.metamask.io/services/get-started/endpoints/) or [public endpoint URL](/network/build/connect).
-   Replace `YOUR_PRIVATE_KEY` with your wallet's private key.

Your output should look similar to:

```bash
Deployer: YOUR_ACCOUNT_NUMBER
Deployed to: 0xED0Ff7E8B655dFFfCA471ea3B6B649ce7C2C1b83
Transaction hash: 0x967e1290b285e67b3d74940ee19925416734c345f58bd1ec64dcea134647d7ee
```

## Deploy a smart contract using a `.env` file

Directly pasting your private key into the command line poses security risks. To avoid exposing sensitive information such as wallet private keys or API keys, use files with the `.env` extension to store private data. Create a `.env` file, then add the file to the `.gitignore` file to prevent committing it. Populate the `.env` file with the relevant private information:

```bash
PRIVATE_KEY=YOUR_PRIVATE_KEY
INFURA_API_KEY=YOUR_INFURA_API_KEY
```

Then, run:

```bash
source .env
```

Finally, modify the `foundry.toml` file to store the various RPC endpoints we might be working with. For example:

```bash
[profile.rpc-endpoints]
linea-sepolia = "https://linea-sepolia.infura.io/v3/${INFURA_API_KEY}"
linea-mainnet = "https://linea-mainnet.infura.io/v3/${INFURA_API_KEY}"
```

To deploy the smart contract, run:

-   Mainnet
-   Sepolia

```bash
forge create --rpc-url linea-mainnet src/Counter.sol:Counter --private-key $PRIVATE_KEY
```

```bash
forge create --rpc-url linea-sepolia src/Counter.sol:Counter --private-key $PRIVATE_KEY
```

Next, you can optionally [verify your contract on the network](/network/how-to/verify-smart-contract/foundry).
