Using ganache-cli
ganache-cli -d --db <DATA_DIR> -i <NETWORK_ID> --accounts 20 --deterministic --mnemonic="myth like bonus scare over problem client lizard pioneer submit female collect"
Options used
-d, --deterministic
Generate deterministic addresses based on a pre-defined mnemonic. [boolean]
this will generate the same addresses anytime.
-m, --mnemonic
bip39 mnemonic phrase for generating a PRNG seed, which is in turn used for hierarchical
deterministic (HD) account generation [string]
This is mandatory while using --deterministic option, you have to supply the same mnemonic each time you start.
--db
Directory of chain database; creates one if it doesn't exist [string] [default: null]
You can add parameter db, to save/persist the chain data (with all the transactions) so you can load same contracts state again in the next executions
-i, --networkId
The Network ID ganache-cli will use to identify itself.
[number] [default: System time at process start or Network ID of forked blockchain if configured.]
this make sure that random networkID is not selected everytime you restart ganache
And, make sure that the folder already exists where you intend to store/persist your local network data
Freebie
If you want to avoid leaving console open to run ganache, you can run as a background process by adding an ampersand(&) at the end of the command and write its output to log instead of your terminal. If you want to look for accounts, you can open a file and use tail -f file.log
to watch for transaction log details.
so I'd recommend redirecting its output to a file like below:
<cmd> 2>&1 > ganache-output-$(date +%Y-%m-%d-%H:%M:%S).log &
If you want to find if the process is running, use ps -ef | grep ganache
# ps -ef | grep ganache
root 323 1 0 02:54 pts/2 00:00:03 node /usr/bin/ganache-cli -d --db /app/ganache_data -i 344 --accounts 20 --deterministic --mnemonic=myth like bonus scare over problem client lizard pioneer submit female collect
root 635 32727 0 03:49 pts/2 00:00:00 grep --color=auto ganache
where 323 is what's known as the "pid".
Finally, when you're done, you can either use kill <PID>
(In this case kill 323
) or you can just fg 1 and hit ctrl-c.
One last tip, I'd recommend creating a bash file say start_ganache.sh, so that you don't need to remember options and their values whenever you want to start/run.
start_ganache.sh
#!/bin/bash
ganache-cli -d --db /app/ganache_data -i 344 --accounts 20 --deterministic --mnemonic="myth like bonus scare over problem client lizard pioneer submit female collect" 2>&1 > /app/ganache/logs/ganache-output-$(date +%Y-%m-%d-%H:%M:%S).log &
give executable permission using chmod +x start_ganache.sh
and execute ./start_ganache.sh
P.S: I know this is more verbose. But believe me, many junior developers tend to have a much smaller zone of focus when working with bash. I hope someone 'll find this useful