How to Install and Use ngrok in Termux on Android

Install and use ngrok in Termux on Android. Learn how to expose a local server, fix common errors, and create secure tunnels.

If you are working on a local project in Termux, you may need to access it from the internet. This could be for testing webhooks, sharing a demo, or checking how your app behaves outside your phone. This is where ngrok helps.

ngrok lets you expose a local server to the internet using a secure tunnel. Instead of setting up port forwarding or router rules, it gives you a public URL that points directly to your local service.

Install and set up ngrok in Termux on Android to share a local server online

In this guide, I will show you how to install and use ngrok in Termux step by step. By the end, you will know how to start a tunnel and share your local projects safely from your Android device.


Requirements

Before we start, make sure you have the following ready. These are basic things and easy to set up.

  • Android device: Any modern Android phone should work.
  • Termux installed: Make sure you are using the correct and updated Termux version.
    If you need help installing and setting it up properly, follow this guide: How to Install and Set Up Termux on Android Device .
  • Stable internet connection: ngrok needs an active internet connection to create tunnels.
  • Free ngrok account: You need an ngrok account to get an authtoken. This token connects ngrok on your phone to your account.

Once you have these ready, you can move on to installing and setting up ngrok in Termux.


Installing ngrok in Termux

Without wasting time, below are the steps to install ngrok in Termux.

1. Update Termux Packages

Before installing ngrok, make sure your Termux packages are up to date. This helps prevent common installation errors.

Run the command below:

pkg update && pkg upgrade -y

This updates the package list and upgrades any existing packages.

2. Install Required Packages

ngrok is downloaded as a zip file, so we need a few tools to handle that. Install them using:

pkg install wget unzip -y
  • wget is used to download files from the internet
  • unzip is used to extract zip files

3. Download ngrok

Now download the ngrok binary for Android (Linux ARM architecture) using the command below:

wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-stable-linux-arm.zip

This will download the ngrok zip file into your current Termux directory.

4. Extract ngrok

After the download is finished, extract the ngrok file using:

unzip ngrok-stable-linux-arm.zip

Once extracted, you will see a file named ngrok in your directory. This is the ngrok executable that we will use in the next steps.


Setting Up ngrok in Termux

After installing ngrok, the next step is to set it up using an authtoken. The authtoken is a unique key that connects ngrok on your device to your account. Without it, ngrok will not work properly.

1. Create a ngrok Account

To get started, visit the official ngrok website:

https://ngrok.com

Click Sign up for free and create an account using your email and password. Once the signup process is complete, log in to your account dashboard.

Screenshot of the ngrok website homepage showing the headline 'All-in-one API gateway, Kubernetes Ingress, DDoS protection, firewall, and global load balancing as a service,' with a 'Sign up for free' button highlighted by an arrow.

2. Get Your ngrok Authtoken

Once you’re logged in, open the dashboard by clicking on the menu (three lines) at the top left of the page.

Screenshot of the ngrok dashboard homepage with an orange arrow pointing to the hamburger menu icon on the top left.

Here, you will find your unique authtoken under the "Your Authtoken" section.

Screenshot of the ngrok dashboard showing the dropdown menu after clicking the hamburger icon, with a yellow arrow highlighting the "Setup & Installation" option under "Getting Started.

Copy the authtoken. You will need it in the next step.

Screenshot of the "Your Auth Token" page on the ngrok dashboard, with an orange arrow pointing to the "Copy" button next to the Auth Token.

3. Add the Authtoken in Termux

Now, go back to Termux and enter the following command to set your ngrok authtoken:

./ngrok authtoken PASTE_YOUR_AUTH_TOKEN_HERE
Replace PASTE_YOUR_AUTH_TOKEN_HERE with the actual authtoken you copied from the ngrok dashboard.

If the command runs successfully, ngrok is now linked to your account and ready to use in Termux.


Using ngrok in Termux

Now that ngrok is installed and linked to your account, you can start creating secure tunnels to expose your local servers to the internet.

Start a ngrok Tunnel

To create a tunnel, run the following command in Termux:

./ngrok http 8080

This command creates an HTTP tunnel on port 8080.

Make sure your local server is already running on this port.

If your server uses a different port, replace 8080 with the correct one.

What Happens Next

Once the command runs, ngrok will connect to its servers and generate a public URL. This URL forwards traffic from the internet to your local server.

Anyone with this link can access your server while the tunnel is running.

Note! Make sure your device has a stable internet connection before starting the tunnel. If the connection drops, the tunnel will stop working.

With your tunnel running, you can now access your local server from anywhere using the generated URL. This is especially useful for testing webhooks, sharing your work with clients, or accessing your home server while on the go.

Accessing Your Server

In the Termux output, look for the Forwarding URL.

Open this URL in a browser or share it with others to access your local server from anywhere.

Screenshot of ngrok running in the Termux terminal. It displays session details such as account name, version (3.19.0), region (India), latency (38ms), and the forwarding URL (https://78f7-137-115-0-10.ngrok-free.app -> http://localhost:8080). The terminal also shows the web interface link and no active connections.

Troubleshooting Common Issues

1. Session Status reconnecting

If you see an error like this:

Error! Session Status reconnecting (failed to dial ngrok server with address "connect.ngrok-agent.com:443": dial tcp: lookup connect.ngrok-agent.com on [::1]:53: read udp [::1]:43475->[::1]:53: read: connection refused)

Why this happens:
This usually means ngrok cannot reach its servers. The most common reasons are an unstable internet connection, DNS problems, or Android restricting Termux in the background.

What to try first:

  • Make sure your internet connection is active and stable
  • Disable battery optimization for Termux
  • Stop any running ngrok process and start again
./ngrok http 8080

About the hotspot tip:
You do not need to turn on your mobile hotspot in most cases. ngrok works fine on normal mobile data or Wi-Fi. Toggling the hotspot is only a last-resort workaround for some devices or mobile networks if the tunnel keeps reconnecting.

2. DNS Resolution Errors

If ngrok fails to resolve domain names, your DNS configuration in Termux may be the problem.

Solution: Edit the DNS configuration file:

nano /data/data/com.termux/files/usr/etc/resolv.conf
# Add the following lines:
nameserver 8.8.8.8
nameserver 8.8.4.4

Save the file, close the editor, and restart ngrok.

3. IPv6 Connectivity Issues

Some mobile networks have unstable IPv6 support. If ngrok keeps failing, forcing IPv4 can help.

Solution:

nano /data/data/com.termux/files/usr/etc/sysctl.conf
# Add the following lines:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1

# Apply the changes:
sysctl -p

This disables IPv6 and forces ngrok to use IPv4, which is more reliable on many networks.


Congratulations! You’ve successfully installed and configured ngrok in Termux. Now you can securely expose your local server to the internet with just a few commands. This means you can test webhooks, share your projects, or debug APIs from anywhere without complicated setup.

Remember to always keep your tunnels safe. Avoid exposing sensitive services, and only share your ngrok URLs with people you trust. If you follow the steps in this guide, you’ll be able to work confidently with ngrok on your Android device.

Don’t stop here! After setting up ngrok, you can try building some easy Termux projects on Android to practice what you’ve learned and share them online with friends using ngrok’s secure tunnels.

If this guide helped you, leave a comment below and share it with others who use Termux. Your feedback and shares help more people find and learn from this tutorial.

About the author

Stephano kambeta
Proudly African 🇲🇼. Modern life, shaped by the echoes of traditional drums.

إرسال تعليق