1. 环境准备
系统要求
- Node.js: 版本 10.0.0 或更高
- npm: 通常随 Node.js 一起安装
- 操作系统: Windows, macOS, Linux
检查当前环境
# 检查 Node.js 版本
node --version
# 检查 npm 版本
npm --version
# 检查是否已安装 Ganache-CLI
ganache-cli --version
2. 安装方法
方法一:使用 npm 全局安装(推荐)
# 全局安装
npm install -g ganache-cli
# 或者使用 yarn
yarn global add ganache-cli
方法二:使用 npx(临时使用,无需安装)
# 直接运行,不安装到本地
npx ganache-cli [options]
方法三:项目本地安装
# 在项目目录中安装为开发依赖
npm install --save-dev ganache-cli
# 然后在 package.json 中添加脚本
{
"scripts": {
"ganache": "ganache-cli"
}
}
3. 基础启动命令
最简单的启动方式
# 使用默认配置启动
ganache-cli
使用助记词启动
ganache-cli -m "system light please garage table make luxury insane board custom light engage"
完整参数启动
ganache-cli \
--mnemonic "system light please garage table make luxury insane board custom light engage" \
--host 0.0.0.0 \
--port 8545 \
--networkId 1337 \
--chainId 1337 \
--accounts 10 \
--defaultBalanceEther 100 \
--secure \
--db ./ganache-data \
--debug
4. 常用启动参数详解
账户相关参数
# 指定账户数量
--accounts 20
# 设置每个账户的初始余额(ETH)
--defaultBalanceEther 1000
# 指定确定的助记词
--mnemonic "your twelve word mnemonic here"
# 锁定账户(需要手动解锁)
--secure
# 指定账户私钥(可多次使用)
--account "0xprivkey1,balance"
--account "0xprivkey2,balance"
网络配置参数
# 监听地址
--host 0.0.0.0
# 监听端口
--port 8545
# 网络ID
--networkId 1337
# 链ID
--chainId 1337
# 禁用CORS限制
--disableHostCheck
区块链行为参数
# 设置区块Gas限制
--gasLimit 0x6691b7
# 设置Gas价格(wei)
--gasPrice 20000000000
# 关闭自动挖矿
--blockTime 0
# 设置固定区块时间(秒)
--blockTime 5
# 指定数据存储目录
--db ./blockchain-data
# 解锁特定账户
--unlock "0xaddress1"
--unlock "0xaddress2"
5. 配置文件方式
创建配置文件 ganache-config.js
module.exports = {
// 网络配置
network_id: 1337,
port: 8545,
hostname: "0.0.0.0",
// 账户配置
mnemonic: "system light please garage table make luxury insane board custom light engage",
total_accounts: 10,
default_balance_ether: 100,
secure: true,
// 区块链配置
gasLimit: 0x6691b7,
gasPrice: 20000000000,
blockTime: 0,
// 数据存储
db_path: "./ganache-data",
// 调试选项
verbose: true,
debug: false
};
使用配置文件启动
ganache-cli --config ganache-config.js
6. 不同场景的启动配置
开发测试环境
ganache-cli \
--accounts 20 \
--defaultBalanceEther 100 \
--gasLimit 0xfffffffffff \
--gasPrice 1 \
--hardfork istanbul \
--networkId 5777 \
--port 7545 \
--chainId 1337
智能合约测试环境
ganache-cli \
--mnemonic "test test test test test test test test test test test junk" \
--accounts 50 \
--defaultBalanceEther 1000 \
--gasLimit 8000000 \
--gasPrice 20000000000 \
--blockTime 2 \
--db ./contract-test-data
生产模拟环境
ganache-cli \
--accounts 10 \
--defaultBalanceEther 10 \
--gasLimit 6721975 \
--gasPrice 20000000000 \
--blockTime 15 \
--networkId 1 \
--chainId 1 \
--secure
7. 验证安装和运行
检查服务是否正常运行
# 检查端口监听
netstat -an | grep 8545
# 或者使用 lsof (macOS/Linux)
lsof -i :8545
# 测试 RPC 连接
curl -X POST http://localhost:8545 \
-H "Content-Type: application/json" \
--data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}'
使用 Postman 测试连接
{
"jsonrpc": "2.0",
"method": "eth_accounts",
"params": [],
"id": 1
}
预期响应:
{
"jsonrpc": "2.0",
"id": 1,
"result": [
"0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1",
"0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0",
"..."
]
}
8. 集成开发环境配置
与 Hardhat 集成
在 hardhat.config.js 中配置:
require("@nomiclabs/hardhat-waffle");
module.exports = {
networks: {
ganache: {
url: "http://127.0.0.1:8545",
chainId: 1337
}
},
solidity: "0.8.4"
};
与 Truffle 集成
在 truffle-config.js 中配置:
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*",
gas: 6721975,
gasPrice: 20000000000
}
}
};
9. 常见问题解决
端口被占用
# 查找占用端口的进程
lsof -i :8545
# 杀死进程
kill -9 <PID>
# 或者使用其他端口
ganache-cli --port 8546
内存不足
# 增加Node.js内存限制
node --max-old-space-size=4096 $(which ganache-cli) [options]
权限问题(Linux/macOS)
# 给Node.js可执行权限
chmod +x $(which node)
# 或者使用sudo(不推荐)
sudo npm install -g ganache-cli
10. 启动脚本示例
Windows 批处理文件 start-ganache.bat
@echo off
echo Starting Ganache-CLI...
ganache-cli ^
--mnemonic "system light please garage table make luxury insane board custom light engage" ^
--accounts 15 ^
--defaultBalanceEther 500 ^
--port 8545 ^
--networkId 1337 ^
--chainId 1337
pause
Linux/macOS Shell 脚本 start-ganache.sh
#!/bin/
echo "Starting Ganache-CLI..."
ganache-cli \
--mnemonic "system light please garage table make luxury insane board custom light engage" \
--accounts 15 \
--defaultBalanceEther 500 \
--port 8545 \
--networkId 1337 \
--chainId 1337 \
--db ./ganache-data
赋予执行权限
chmod +x start-ganache.sh
11. 高级功能
分叉主网
# 分叉以太坊主网(需要Infura等节点服务)
ganache-cli --fork https://mainnet.infura.io/v3/YOUR_PROJECT_ID
# 分叉指定区块
ganache-cli --fork https://mainnet.infura.io/v3/YOUR_PROJECT_ID@13234450
使用自定义矿工地址
ganache-cli --account="0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501200,1000000000000000000000" --unlock "0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501200"