Localhost Not Working on Mac: 7 Fixes
Localhost refuses to load on your Mac? Here are 7 tested solutions: server check, hosts file, firewall, blocked ports. Step-by-step troubleshooting guide.
Locahl Team
Table of Contents
- Why is localhost not working on Mac?
- Quick Diagnosis
- Solution #1: Check that the server is running
- Check your terminal
- Check if a process is listening
- Check the correct port
- Solution #2: Free up an occupied port
- Identify the process
- Kill the process
- Solution #3: Check the firewall
- macOS Firewall
- Third-party applications
- Quick test
- Solution #4: Check the hosts file
- Check the content
- Repair if necessary
- Sources and further reading
- Solution #5: Flush DNS cache
- Solution #6: Check the listening address
- Important difference
- Force the address in your server
- Solution #7: Tool-specific problems
- Node.js / npm
- Docker
- MAMP / XAMPP
- Test with 127.0.0.1 vs localhost
- Use custom local domains
- Framework-specific checks
- Next.js
- Vite
- Docker
- MAMP
- Localhost vs custom domain
- Debug with a clean sequence
- Prevent future problems
- Conclusion
You start your development server, open localhost:3000 in your browser, and... nothing. "This site can't be reached", "ERR_CONNECTION_REFUSED", or an endless loading page. Frustrating, isn't it?
Why is localhost not working on Mac?
If localhost isn't working on Mac, the server is usually not running, is listening on a different port, or the `127.0.0.1 localhost` line is missing from /etc/hosts. Confirm the server is up with `curl -I http://localhost:3000`, check the port it binds to, restore the localhost entry, then flush DNS.
This guide will help you diagnose and fix localhost problems on Mac, step by step.
Quick Diagnosis
Before diving into solutions, let's identify the type of problem:
| Symptom | Likely Cause | Solution |
|---|---|---|
| ERR_CONNECTION_REFUSED | Server not started or wrong port | Solution #1 |
| Page loads forever | Firewall or timeout | Solution #3 |
| localhost OK but domain.local not | Hosts file | Solution #4 |
| Worked yesterday, not today | DNS cache or zombie process | Solution #2, #5 |
Solution #1: Check that the server is running
It seems obvious, but this is the most common cause.
Check your terminal
Does your server display "Listening on port 3000" or similar?
# Example with npm
npm run dev
# Expected output
> ready - started server on 0.0.0.0:3000Check if a process is listening
lsof -i :3000If nothing appears, no process is listening on that port.
Check the correct port
Make sure you're accessing the right port in your browser. If your server says "port 8080", don't go to localhost:3000.
Solution #2: Free up an occupied port
If you see "Port 3000 already in use", another process is blocking the port.
Identify the process
lsof -i :3000Result:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 12345 user 23u IPv4 0x1234 0t0 TCP *:3000 (LISTEN)Kill the process
kill -9 12345Or to kill all processes on a port:
lsof -ti :3000 | xargs kill -9Solution #3: Check the firewall
macOS firewall or security applications can block local connections.
macOS Firewall
1. Open System Settings > Network > Firewall 2. Click Options... 3. Make sure your application (Node, Python, etc.) is allowed
Third-party applications
Apps like Little Snitch, Lulu, or certain antiviruses can block localhost. Check their rules.
Quick test
Temporarily disable the firewall to test:
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate offDon't forget to re-enable it afterwards:
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate onSolution #4: Check the hosts file
If localhost doesn't resolve to 127.0.0.1, the problem is in the hosts file.
Check the content
cat /etc/hostsYou should see:
127.0.0.1 localhost
::1 localhostRepair if necessary
If these lines are missing:
sudo nano /etc/hostsAdd:
127.0.0.1 localhost
::1 localhostSave (Ctrl+O, Enter, Ctrl+X) then flush the DNS cache.
Sources and further reading
- Terminal User Guide (Apple Support)
- localhost and the loopback address (Wikipedia)
- The hosts file explained (Wikipedia)
Solution #5: Flush DNS cache
A corrupted DNS cache can prevent localhost from working.
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponderSolution #6: Check the listening address
Some servers listen on 127.0.0.1 only, others on 0.0.0.0.
Important difference
- 127.0.0.1: Accessible only from your machine
- 0.0.0.0: Accessible from any interface (useful for Docker, VM)
Force the address in your server
// Node.js / Express
app.listen(3000, '0.0.0.0', () => {
console.log('Server running');
});# Python / Flask
app.run(host='0.0.0.0', port=3000)Solution #7: Tool-specific problems
Node.js / npm
# Clean npm cache
npm cache clean --force
# Delete node_modules and reinstall
rm -rf node_modules package-lock.json
npm installDocker
# Check that Docker Desktop is running
docker ps
# Restart Docker if necessary
killall Docker && open /Applications/Docker.appMAMP / XAMPP
1. Check that Apache/Nginx is running (green light) 2. Check the port in preferences (80 vs 8888)
Test with 127.0.0.1 vs localhost
If localhost fails but http://127.0.0.1:3000 works, the problem is in DNS resolution, not your server.
# Test localhost
curl -I http://localhost:3000
# Test direct IP
curl -I http://127.0.0.1:3000Use custom local domains
Rather than localhost, you can use domains like myproject.test for more clarity.
Framework-specific checks
Different tools fail in slightly different ways.
Next.js
Check that the dev server started on the port you are opening:
npm run dev
lsof -i :3000
curl -I http://localhost:3000If you need to test a custom hostname, add it to /etc/hosts:
127.0.0.1 myapp.test
::1 myapp.testThen open http://myapp.test:3000. If the project uses secure cookies, OAuth or service workers, configure HTTPS with mkcert.
Vite
Vite may reject custom hostnames unless you allow them. If localhost works but myapp.test fails, check the Vite server host configuration.
export default {
server: {
host: '0.0.0.0',
port: 5173
}
}Docker
Docker adds two more layers: container ports and host ports.
docker ps
docker compose ps
curl -I http://localhost:8080If the container exposes port 80 but maps it to host port 8080, localhost:80 will fail while localhost:8080 works.
MAMP
MAMP often uses port 8888 instead of 80. Check the MAMP preferences before assuming the server is down.
Localhost vs custom domain
localhost is good for quick testing, but custom domains are better for serious projects.
Use localhost when:
- You are testing a single app.
- No cookies depend on a domain.
- No OAuth redirect uses a custom host.
- You do not need subdomains.
Use .test domains when:
- You need
api.myapp.testandadmin.myapp.test. - You test secure cookies.
- You mirror production hostname structure.
- You work with multiple client projects.
Avoid .local on Mac because Bonjour can intercept it and make resolution slower or inconsistent.
Debug with a clean sequence
When you are stuck, run this exact sequence:
# 1. Confirm the hosts mapping
dscacheutil -q host -a name localhost
# 2. Confirm the port
lsof -i :3000
# 3. Test direct IP
curl -I http://127.0.0.1:3000
# 4. Test localhost
curl -I http://localhost:3000
# 5. Flush DNS
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponderIf Chrome still fails after Terminal tests pass, clear Chrome DNS cache:
Prevent future problems
1. Use different ports per project (3000, 3001, 8000, etc.) 2. Stop servers properly (Ctrl+C) instead of closing the terminal 3. Use a manager like Locahl to avoid hosts file conflicts 4. Prefer .test domains for client and project work 5. Document ports in each project README 6. Use HTTPS locally for auth-heavy apps
Conclusion
Localhost problems on Mac usually have simple causes: server not started, port occupied, or stale DNS cache. By following this guide methodically, you should resolve 99% of cases.
If the problem persists, check your application logs and make sure your development environment is properly configured.
Ready to simplify your workflow?
Stop wasting time with the terminal. Locahl lets you manage your hosts file in a few clicks, with automatic validation and no risk of errors.
- Intuitive visual interface
- Automatic DNS flush
- Multi-environment management
- Automatic backups
- JSON Import/Export
Reader Reviews
"Solution #3 (firewall) was my issue. 2 hours of debugging solved in 5 minutes thanks to this article."
September 12, 2025
"Very comprehensive! The diagnostic table at the beginning helped me quickly identify the cause."
November 28, 2025
"Good troubleshooting article. Would have liked more details on Docker but otherwise perfect."
January 5, 2026
Frequently Asked Questions
Why isn't localhost working on my Mac?
The most common causes are: server not started, port already in use, firewall blocking, corrupted hosts file, or stale DNS cache.
How do I check if a server is running on localhost?
Use the command 'lsof -i :PORT' (e.g., lsof -i :3000) to see if a process is listening on the desired port.
Are localhost and 127.0.0.1 the same?
Technically yes, but localhost goes through DNS resolution (hosts file) while 127.0.0.1 is a direct IP. If localhost fails but 127.0.0.1 works, the problem is in your hosts file.
How do I free up an occupied port on Mac?
Identify the process with 'lsof -i :PORT', then kill it with 'kill -9 PID'. Example: kill -9 12345.
Can Mac firewall block localhost?
Yes, if the firewall is in strict mode or if a third-party security application blocks local connections. Check in System Settings > Network > Firewall.
Related Articles
Localhost Refused to Connect: How to Fix It
Fix "localhost refused to connect" (ERR_CONNECTION_REFUSED) in Chrome, on Windows, Mac, XAMPP and VS Code: check the server, the port, IPv6 vs IPv4, hosts file and firewall.
Locahl Team
Developer tools team
How to Use Subdomains on localhost for Local Development
Use subdomains on localhost: the free *.localhost trick browsers resolve automatically, fixed subdomains via the hosts file, and wildcard subdomains with dnsmasq on Mac and Linux.
Locahl Team
Developer tools team
Fix "This site canβt be reached" on localhost
Fix "This site canβt be reached" / ERR_CONNECTION_REFUSED on localhost: check the server is running, the right port, IPv6 vs IPv4, the hosts file and firewall.
Locahl Team
Developer tools team
Fix mkcert NET::ERR_CERT_AUTHORITY_INVALID
Fix NET::ERR_CERT_AUTHORITY_INVALID with mkcert on Mac by reinstalling the local CA, regenerating certificates and checking hostnames.
Locahl Team
Developer tools team
Hosts File Not Working on Mac: Fix Guide
Your hosts file changes not taking effect? Learn how to fix DNS cache issues, browser cache, file permissions, syntax errors, and encoding problems on macOS.
Locahl Team