Ngrok: From Localhost to The World
Introduction
Have you ever wanted to share your local web application with someone online but didn't know how? That’s where ngrok comes in! Ngrok is a powerful tool that allows you to create secure tunnels to your localhost, making it accessible from anywhere in the world. Whether developing a web app, testing webhooks, or needing a temporary public link, ngrok makes it incredibly easy.
In this article, we’ll explain what ngrok is, its advantages, and how to use it with simple commands. No deep technical knowledge is required—just follow along!
What Is Ngrok?
Ngrok is a tool that creates a secure tunnel between your local machine and the internet. Normally, websites hosted on your computer (localhost) can’t be accessed from outside your network. Ngrok solves this by giving your local server a public URL that can be shared with anyone.
How It Works:
- You run a local web server (e.g., a Flask, Node.js, or Django app) on your computer.
- You start ngrok and tell it which port your server is running on.
- Ngrok generates a temporary, publicly accessible URL.
- You can now share this URL with others or use it for testing external services.
Advantages of Using Ngrok
- Easily Share Local Projects – No need to deploy your app online just to test it with others.
- Test Webhooks Locally – Services like Stripe, PayPal, or GitHub require public URLs for webhooks. Ngrok makes testing easy.
- No Port Forwarding Required – Unlike traditional methods, ngrok eliminates the need for complicated router settings.
- Secured Connection – Ngrok provides HTTPS tunnels by default, ensuring secure communication.
- Supports Multiple Protocols – Works with HTTP, HTTPS, and even TCP.
- Works Behind Firewalls – No need to configure your firewall to expose your local server.
How to Use Ngrok
Step 1: Install Ngrok
Before using ngrok, you need to install it. You can download it from the official site: https://ngrok.com/download
Alternatively, install it via the command line:
For Windows:
- Download the ZIP file, extract it, and place it in a folder.
- Open Command Prompt (
cmd
), navigate to the folder, and runngrok
.
For macOS (using Homebrew):
brew install ngrok/ngrok/ngrok
For Linux:
sudo snap install ngrok
Step 2: Authenticate Ngrok (First-Time Use)
Before you can start using ngrok, you need to authenticate it with your account:
ngrok config add-authtoken YOUR_AUTH_TOKEN
(You can get your Auth Token by signing up on ngrok.com and checking your dashboard.)
Step 3: Start a Local Server
Let's say you're running a local web server on port 5000. If you're using Python's built-in server:
python -m http.server 5000
Or if you're running a Flask app:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Ngrok!"
if __name__ == '__main__':
app.run(port=5000)
Step 4: Start Ngrok
To expose your local server to the internet, run:
ngrok http 5000
Step 5: Get Your Public URL
After running the above command, ngrok will generate a public URL like this:
Forwarding https://randomstring.ngrok.io -> http://localhost:5000
Now, anyone with this link can access your local server!
Additional Ngrok Features
1. Running Ngrok on a Custom Subdomain
If you have a paid ngrok plan, you can specify a custom subdomain:
ngrok http -subdomain=mycustomname 5000
This will give you a public URL like:
https://mycustomname.ngrok.io
2. Using Ngrok for Webhooks
Many APIs (like Stripe and GitHub) require webhooks to communicate with your server. With ngrok, you can easily test them locally. Just use the generated public URL when setting up webhooks in the service's settings.
3. Viewing Ngrok Logs
Ngrok provides a built-in web interface to inspect traffic:
Open this URL in your browser:
http://127.0.0.1:4040
Here, you can see incoming requests, headers, and response details.
Conclusion
Ngrok is an incredibly useful tool for developers. Whether you're testing webhooks, sharing a local project, or working behind firewalls, it simplifies the process of exposing your local server to the internet. With just a few simple commands, you can create secure tunnels and get a public URL in seconds.
So go ahead, try ngrok today, and make your local development process much smoother!