# How to compile and run nearcore
I need to compile nearcore from its source code, and I found multiple sources of information:
in the contribute/nearcore section
in the develop/running-testnet section
mentioned in nearup README.md in "using a locally compiled binary" at line 67
Solution I'd like
As a sysadmin, I need instructions to put this process in my automated pipeline.
Given what I want to do, I want to avoid any trial-and-error in the process of cloning and compiling the right mix of:
prerequisites (e.g. I need rustup, but not docker.io if I don't use containers)
nearcore branch (main, stable, beta)
make parameters (release, debug)
network configuration (mainnet, testnet, betanet, devnet, localnet)
any additional node configuration (e.g. boot nodes, tracked accounts and shards, archival mode)
startup command (correct binary path, and if I have to use .py scripts or nearup)
Additional context
This part of the documentation is needed by validators who build their infrastructure-as-code pipeline
---
This doc is written for developers, sysadmins, devops or curious people who want to know how to compile and run a regular NEAR node natively (without containerization) for one of the network:
- mainnet
- testnet
- localnet
## Prerequisites
- [Rust](https://www.rust-lang.org/). If not already installed, please [follow these instructions](https://docs.near.org/docs/tutorials/contracts/intro-to-rust#3-step-rust-installation).
- [Git](https://git-scm.com/)
### Recommended hardware:
- 4 CPU cores
- 16GB RAM
- 100GB SSD (HDD will be enough for localet only)
## 1. Clone nearcore project from GitHub
You need to clone [`nearcore`](https://github.com/near/nearcore)
```bash
$ git clone https://github.com/near/nearcore
$ cd nearcore
```
Depending on the purpose you are running NEAR node for you may consider to choose the branch:
- `master` if you want to play around with the latest code and experiment. This branch is not guaranteed to be in fully working state and absolutely no guarantee it will be compatible with current state of *mainnet* or *testnet*.
- [Latest release branch](https://github.com/near/nearcore/releases) if you want to run NEAR node for *mainnet* or *tesnet*. This version is used by validators and other nodes and fully compatible with current state of *mainnet* or *testnet*.
## 2. Compile nearcore binary
In the `nearcore` folder run the commands:
```bash
$ cargo build --release --package neard --bin neard
```
This will start the compilation process, it will take some time depending on your machine power (e.g. i9 8-core CPU, 32 GB RAM, SSD it takes approximately 25 minutes)
The binary path is `nearcore/target/release/neard`
## 3. Initialize working directory
In order to work NEAR node requires to have working directory and a couple of configuration files.
- `config.json` with a lot of parameters which responsive for how the node will work
- `genesis.json` a file with all the data the network had started with. In contains initial accounts, contracts, access keys and other records which represents the initial state of the blockchain
- `node_key.json` a file which contains public and private key for the node. Also includes optional `account_id` parameter which is required to run validator node (not covered in this doc)
- `data/` folder in which NEAR node will write the state.
Initially the required working directory needs to be generated. The command to generate it looks like:
```bash
$ neard --home </path/to/node/working/dir> init --chain-id <chain-id> --download
```
Where:
- `</path/to/node/working/dir>` is a path where you want NEAR node to have working directory (you can skip `--home` argument to set default -- `~/.near`)
-`<chain-id>` is for `mainnet` or `testnet` (local one commands looks a bit different)
> For localnet the command is simpler
> ```bash
> $ neard --home ~/.near init --chain-id localnet
> ```
This command will create the required directory structure, will generate `config.json` and `node_key.json` and will donwload `genesis.json` for specified network.
> **Heads up**
> In case of localnet the command also will generate `validator_key.json` for `test.near` user which will be the only validator for your local network
## 3.1 Commands cheatsheet
### localnet
```bash
$ ./target/release/neard --home ~/.near init --chain-id localnet
```
### mainnet
```bash
$ ./target/release/neard --home ~/.near init --chain-id mainnet --download
```
### testnet
```bash
$ ./target/release/neard --home ~/.near init --chain-id testnet --download
```
## 4. Replacing config.json