Connect Ganache & Localhost to MetaMask (Dev Guide)
Introduction
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.
Who this guide is for (and who should look elsewhere)
- For: local dApp developers, smart contract authors, QA engineers, and anyone running Ganache locally and wanting to connect the MetaMask extension to that node.
- Not for: people who need hardware-wallet integration for mainnet. If you're looking for Ledger/Trezor steps or mobile-only flows, see the guides on connect-ledger and install-metamask-mobile-app.
Quick checklist
- Start Ganache (UI or CLI) and note RPC endpoint and chain ID.
- In the MetaMask extension, add a new custom network pointing at your RPC URL (127.0.0.1 or localhost and the correct port).
- Import one of Ganache's test private keys into MetaMask (use a separate browser profile for safety).
- Confirm chain ID matches (MetaMask expects the same decimal chain ID).
- If connection fails, try 127.0.0.1 instead of localhost, and check WSL/Docker binding.
Step-by-step: Start Ganache (UI vs CLI)
Ganache UI
- Open the Ganache application.
- Start a new workspace or quickstart. Ganache typically exposes an RPC server (common defaults: 7545 for the UI).
- Look at the top or the workspace details for the RPC server address and chain ID. Copy the RPC URL.
Ganache CLI (example)
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) |

Step-by-step: Add your local network to MetaMask
- Open the MetaMask extension in your browser.
- Click the network dropdown and choose "Add network" (or "Custom RPC" in older versions).
- Fill fields:
- Network Name: Ganache Local (or any label).
- New RPC URL: http://127.0.0.1:8545 (or http://127.0.0.1:7545 depending on Ganache).
- Chain ID: Enter the decimal chain ID Ganache reports (e.g., 1337).
- Currency Symbol: ETH (optional for local testing).
- Block Explorer URL: leave blank for local nodes.
- Save. Switch to that network in the dropdown.
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.
Importing Ganache accounts into MetaMask
- In Ganache UI you'll see a list of test accounts and their private keys (copy one).
- In MetaMask: Account options → Import Account → Private Key → paste the Ganache private key.
- The imported account will appear in your MetaMask account list. Use it only for local testing.
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.
Common errors and fixes (metamask cannot connect to localhost 8545)
Why won't MetaMask connect to localhost 8545? Here are the typical causes and fixes.
- Ganache not running or listening on that port.
- Fix: Confirm Ganache process is running and check the RPC URL printed by Ganache.
- Wrong port or using the wrong RPC URL.
- Chain ID mismatch.
- Fix: Make sure the Chain ID you entered in MetaMask matches Ganache's chain ID (decimal). MetaMask will warn if the chain ID differs.
- Binding issue (WSL2, Docker, remote VM).
- Fix: Ensure Ganache is bound to 0.0.0.0 or the host interface (see next section).
- Firewall, proxy, or browser extension blocking the request.
- Fix: Temporarily disable interfering extensions (ad blockers, privacy nets) or test in a clean browser profile.
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.
WSL2 / Docker / VM tips
Running Ganache inside WSL2 or a container changes how localhost is reached from the host browser. A few practical notes:
- WSL2: newer Windows builds map WSL localhost to Windows localhost automatically, but if not, bind Ganache to 0.0.0.0 so the host can reach it.
- Docker: publish the container port (e.g.,
-p 8545:8545). Confirm the container process logs the server address.
- Remote VM: use the VM's IP address in the MetaMask RPC URL instead of localhost.
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.
Security and best practices for local development
- Never import your main wallet's seed phrase into a test environment.
- Use a dedicated browser profile or separate MetaMask profile for local testing.
- Treat Ganache accounts as throwaway accounts. Don't use real ETH on them.
- If you want to avoid importing private keys, consider using a temporary MetaMask vault created from a throwaway seed phrase.
For general MetaMask backup advice see backup-recovery-seed.
FAQ
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.
Conclusion & next steps
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.