This guide explains how to connect Ganache (a local Ethereum-compatible test node) to MetaMask, the software wallet (hot wallet) developers commonly use during smart contract development. You'll find step-by-step instructions for both Ganache UI and Ganache CLI, how to add a local RPC to MetaMask, how to import test accounts, and troubleshooting tips for the common error: "metamask cannot connect to localhost 8545." I’ve tested these flows while building contracts and running front-end dApp tests, and I’ll share what I've learned along the way.
Short answer? It usually works with a small set of checks. But when it doesn't, the problem tends to be a network binding, port, or chain ID mismatch.
If you prefer the terminal, run Ganache CLI and watch the console output for the RPC URL. A common command is:
# example; check your Ganache docs/version
npx ganache-cli -p 8545
Ganache CLI often uses port 8545 by default. Confirm the chain ID printed in the console (common values: 1337, older projects used 5777).
| Ganache variant | Typical default RPC port | Typical chain ID (example) | Notes |
|---|---|---|---|
| Ganache UI | 7545 | 1337 (recent) | GUI, account list and private keys visible in UI |
| Ganache CLI | 8545 | 1337 / 5777 | Terminal output, scriptable |
| Docker / remote | configurable | configurable | Ensure port is published (e.g., -p 8545:8545) |
Why 127.0.0.1? Sometimes using 127.0.0.1 avoids name resolution quirks where "localhost" behaves differently. In my experience that fixed issues on two different machines.
Warning: do not paste a production seed phrase or private key into a test environment. I once mixed test keys into my daily profile and that caused confusion; I now use a separate browser profile for local dev.
For details on importing keys and account management see import-private-key.
Why won't MetaMask connect to localhost 8545? Here are the typical causes and fixes.
If you see a persistent "could not connect" message, check the browser console and Ganache logs. The console often shows CORS or connection refused errors that point to the root cause.
Running Ganache inside WSL2 or a container changes how localhost is reached from the host browser. A few practical notes:
-p 8545:8545). Confirm the container process logs the server address.And one more tip: when in doubt, try curl http://127.0.0.1:8545 from the same machine running the browser. If curl fails, MetaMask will too.
For general MetaMask backup advice see backup-recovery-seed.
Q: How do I connect MetaMask to localhost?
A: Add a custom network in MetaMask using your Ganache RPC URL (e.g., http://127.0.0.1:8545) and the correct chain ID. Then import a Ganache private key for an account.
Q: MetaMask can't connect to localhost 8545 — what now?
A: Confirm Ganache is running and listening on the port, try 127.0.0.1 instead of localhost, check chain ID, and ensure no firewall or container binding blocks the port.
Q: Is it safe to use MetaMask with Ganache?
A: Yes — for local testing it's standard practice. But treat test keys as throwaway and never mix them with your main self-custody seed phrase.
Connecting Ganache to MetaMask is a routine developer step, and usually the problems are easy to fix with a quick port, host, or chain ID check. In my experience the smallest mistake is using the wrong RPC URL. I recommend creating a dedicated browser profile for local testing and importing only Ganache test private keys there.
If you want to connect this setup to a browser-based IDE, see connect-remix. For custom network configuration reference, see add-custom-network and for deeper developer integration patterns check developers-connect.
Try the steps above. If you still hit the "metamask cannot connect to localhost 8545" error, follow the troubleshooting section and then consult the troubleshooting-connect guide.
Thanks for reading — now get back to building (and testing) your contracts.