Create a Public URL for Your Localhost Using Ngrok

Emiliano
EmilianoApril 14, 2021

With Ngrok we can expose a locally running web server to the Internet. This can be useful for testing our projects on various devices such as mobile phones very easily before going to production.

Installation

Open a new terminal and run the following command:

bash
npm i -g ngrok

Another alternative is to download ngrok from its website https://ngrok.com/download.

Run the project on localhost

To use Ngrok, we need our project running on localhost. A simple way to do this is by using Python's SimpleHTTPServer module.

To use it, make sure Python is installed on your machine. If not, you can download it from the official website.

Once installed, open a terminal, navigate to the folder where the project is located, and run the following command:

On Mac and Linux

bash
python -m SimpleHTTPServer

On Windows

bash
python -m http.server

This will start a local server on port 8000 by default, accessible at http://localhost:8000. To change the port, simply specify it at the end of the command:

text
python -m http.server 4000

After following these steps, your project will be running on localhost.

Start ngrok

It is recommended to create an account at https://ngrok.com before starting. While it is possible to use Ngrok without registering, not having an account comes with certain limitations, such as the duration of your URLs and more.

If you decide not to register, to get a new URL just run in the terminal:

bash
ngrok http 8000

The final number depends on the port your project is running on locally.

If you have an account, you need to link it to your machine by entering the authentication token in the terminal. This token can be found in your user profile under the Your Authtoken section.

Enter the command provided by Ngrok in the terminal:

bash
ngrok authtoken <your-auth-token>

After doing this, your account will be linked.

Password-protect URLs

One of the extra features available when you have a Ngrok account is the ability to protect your URLs with a username and password. The command to do this is:

bash
ngrok http -auth="username:password" 8080

For example:

bash
ngrok http -auth="emi:239294sA" 3000

This is one of the many features Ngrok offers. For more information, check out the official documentation at https://ngrok.com/docs.