User Agents

Web browsers and everyday web tech, explained

WSL Ubuntu How to Open Localhost in Browser

· 5 min read · Browsers

In this article
  1. Understanding WSL and Networking
  2. Starting a Local Web Server in WSL Ubuntu
  3. Accessing Localhost from Windows Browser
  4. Common Problems and Fixes
  5. Security and Safety Considerations
  6. Frequently Asked Questions

To open localhost in a browser from the Bash terminal in WSL Ubuntu, run a local web server (e.g., using Python or Node.js), then access http://localhost:<port> in your Windows browser. WSL automatically handles networking so that services running in Ubuntu are accessible from Windows.

Understanding WSL and Networking

Windows Subsystem for Linux (WSL) allows you to run a Linux environment—such as Ubuntu—directly on Windows, without the overhead of a traditional virtual machine. One of its key features is seamless integration between Linux and Windows, especially in networking. When you run a web server in WSL Ubuntu (e.g., via Python's http.server or a Node.js app), it binds to localhost (127.0.0.1) within the Linux environment. Thanks to WSL 2's built-in networking bridge, that same service is accessible from Windows using the same localhost address.

It's important to note that WSL 1 and WSL 2 handle networking differently. WSL 1 uses a translation layer that maps Linux network calls directly to Windows, so localhost behaves almost identically in both environments. WSL 2, however, runs Linux in a lightweight virtual machine with its own IP address. Despite this, Microsoft has implemented a localhost proxy that forwards connections from Windows localhost to services running in WSL 2, so you don’t need to use the VM’s IP manually in most cases.

Starting a Local Web Server in WSL Ubuntu

Before you can open localhost in a browser, you need a web server running. Here are common ways to start one:

  1. Python HTTP Server: If you're serving static files, use Python’s built-in server.
    • For Python 3: python3 -m http.server 8000
    • This starts a server on port 8000. You can replace 8000 with any available port (e.g., 3000, 8080).
  2. Node.js Server: If using a Node.js app (e.g., Express), ensure it listens on 0.0.0.0 or localhost.
    • Example: app.listen(3000, 'localhost') or app.listen(3000)
    • Run with node server.js
  3. Other Frameworks: Ruby on Rails, PHP, or Django servers work similarly—start the development server and note the port.

After starting the server, you’ll typically see output like Serving HTTP on port 8000 or Listening on http://localhost:3000. This confirms the service is active.

Accessing Localhost from Windows Browser

Once the server is running in WSL Ubuntu, open any browser on Windows (Chrome, Edge, Firefox) and navigate to:

  • http://localhost:8000 (if using Python server on port 8000)
  • http://localhost:3000 (for a Node.js app on port 3000)

You do not need to use the WSL machine’s IP address (like 172.x.x.x) unless you’re accessing the server from another device or troubleshooting firewall issues. The localhost proxy in WSL 2 ensures that localhost:port in Windows routes directly to the same port in WSL, provided the service is bound to localhost or 0.0.0.0.

If your app binds only to 127.0.0.1 or localhost, it’s accessible. If it binds to a specific IP, you may need to adjust the host setting in your server configuration.

Common Problems and Fixes

Several issues can prevent localhost from opening in the browser:

  • Server not starting: Ensure the port isn’t in use. Check with lsof -i :8000 or netstat -tuln | grep 8000. Kill the process if needed.
  • Firewall blocking: Windows Defender or third-party firewalls may block WSL connections. Temporarily disable to test, but re-enable and create a rule if needed.
  • App binding to 127.0.0.1 only: Some apps bind strictly to IPv4. Use 0.0.0.0 to allow all interfaces, which WSL supports.
  • WSL version mismatch: Confirm you’re using WSL 2 with wsl -l -v. WSL 1 has fewer networking issues, but WSL 2 is preferred for performance.
  • Browser caching or DNS issues: Try a hard refresh (Ctrl+F5) or use an incognito window.

A common misconception is that you must use the WSL machine’s IP (e.g., http://172.20.123.4:8000). While this works, it’s unnecessary for localhost access from the host Windows machine. Use localhost unless you’re testing cross-device access.

Security and Safety Considerations

Running a local web server in WSL is generally safe because the service is only accessible from your own machine by default. However, binding to 0.0.0.0 makes the server listen on all network interfaces, which could expose it to other devices on your local network if not firewalled properly.

Best practices:

  • Use localhost or 127.0.0.1 during development to limit access.
  • Close servers when not in use.
  • Use non-privileged ports (above 1024) to avoid requiring sudo.
  • Be cautious with sensitive data in development environments.

The localhost proxy in WSL is secure by design—it does not expose WSL services to external networks unless explicitly configured.

Frequently Asked Questions

Can I use localhost in WSL2?

Yes, WSL2 includes a localhost forwarding feature that allows you to access services running in Linux using localhost from Windows applications, including browsers. No extra configuration is needed for most use cases.

Why can't I connect to localhost from Windows?

This can happen if the server isn’t running, the port is blocked by a firewall, or the app binds to an IP not accessible via localhost. Ensure the server is active, use localhost or 0.0.0.0 as the host, and check Windows firewall settings.

Do I need to use the WSL IP address?

No. For accessing services from the host Windows machine, use localhost:port. The WSL IP address (e.g., 172.x.x.x) is only needed for external devices or if localhost forwarding is disabled.

How do I change the port for my local server?

Specify the desired port when starting the server. For example, python3 -m http.server 8888 runs on port 8888. Then access http://localhost:8888 in the browser.

Can I access WSL localhost from another computer?

Not directly via localhost. To allow external access, bind your server to 0.0.0.0 and use the Windows machine’s IP address on the local network (e.g., http://192.168.1.10:8000). Firewall rules may need adjustment.

Does this work with all browsers?

Yes. Any browser installed on Windows (Chrome, Edge, Firefox, etc.) can access localhost services running in WSL Ubuntu, as long as the server is active and the port is correct.