Running a ruby on rails project on my mac. I need to test it on my android phone. Is there a way to view my mac localhost on my android phone?
-
I tried all the solutions on this page and none of them worked but this answer to what someone claimed as a duplicate question has a different and correct answer that worked for me. http://stackoverflow.com/a/9515414/627702 – SWoo Nov 11 '16 at 19:28
-
To run localhost on Android phone try this - https://stackoverflow.com/a/60500370/12333430 – kumudgupta76 Aug 18 '20 at 17:09
13 Answers
The name "localhost" is just a loopback to your own computer. To access it on your Android, you need to find the computer's IP address.
The most general way of finding this info, which works on both Mac and Linux, is to go into the Terminal and type ifconfig
. Look for either "en0" or "en1", and under that entry look for the "inet" listing. It will be something along the lines of "192.168.1.100". When you find that address, that's what you'll want to put in your browser's address bar.
(On a Mac specifically, you can go to the Sharing pane in System Preferences and it'll tell you there.)

- 234,037
- 30
- 302
- 389
-
13Just to add what I found. By putting in 192.168.1.107 (my Mac ip) in Android browser, it will not work. But if I put in 192.168.1.107:3000, it will work. – VHanded Mar 28 '14 at 10:33
-
6
-
8Make sure you type `ifconfig` and _not_ `ipconfig` like I did (recently moved from pc to mac). Took quite a while to see that typo. – SMBiggs Oct 27 '16 at 05:39
-
3
-
2
-
nice article which help me https://prowe214.medium.com/tip-how-to-view-localhost-web-apps-on-your-phone-ad6b2c883a7c – Aleksandr Golovatyi Sep 09 '21 at 12:43
Here's a quick to-do to have your localhost available for tests on other devices :
1) identify the IP of your Android : select the Wi-Fi you're connected (the same as the one the Mac is on), you'll have the IP detailed on it). For the example : we suppose your Android IP is : 192.168.0.10
2) open a Shell on your Mac and edit your host :
sudo nano /etc/hosts
3) edit the file as this :
127.0.0.1 192.168.0.10
4) Identify your Mac IP : (as @Chuck perfectly explained) (in your shell) :
ifconfig
5) Open your favourite webbrowser you use on your phone and connect to your Mac IP (with the port if needed) with directly something like :
http://192.168.x.x:8000/
6) Enjoy your test :)
Notice you can do that for every support connected on your Wi-Fi.

- 1,209
- 2
- 14
- 26
-
13
-
About to try but thought I'd quickly add that for most people it should actually be: `sudo nano /etc/hosts` Note the plural – bryceadams Jun 02 '14 at 08:32
-
1yes it is working. To get android ip I used: [this app](https://play.google.com/store/apps/details?id=aws.apps.networkInfoIi) – Maxim Yefremov Jun 16 '14 at 03:40
-
5
-
-
Note for Android phones, the default IP address when being used as a hotspot is 192.168.43.1 (https://stackoverflow.com/questions/17302220/android-get-ip-address-of-a-hotspot-providing-device) – Jezzamon May 10 '18 at 14:30
-
Yes i have try this way and it is working. The key is first you got the IP of your android device and place that IP into your /etc/hosts in your mac. Afer that you should know your Mac IP. Then access your Mac IP from your smartphone. Note you have to connect your smartphone and Mac on same network – Faris Rayhan May 18 '18 at 07:25
-
This worked for me using @MaximYefremov's two tips. Note: `ifconfig | grep 192` will return two IPs: `inet` and `broadcast`. Use the one next to `inet` – Chris Perry Oct 26 '22 at 23:48
-
-
I tried to connect to the wifi in my office and it did not work, possibly due to some configuration/security settings. After switching to my personal hotspot it worked. – Dror Bar May 02 '23 at 06:54
Chunk's answer is correct, assuming your mobile device and your computer are on the same network. However, if you want your localhost server to be visible to the wider Internet (e.g. for testing over 3G, developing webhooks or collaboration with a remote colleague/client), more is needed as local addresses (starting with 10.* or 192.168.*) are not visible to the wider Internet.
The traditional solution to that is port forwarding and dynamic DNS, but lately a few services have popped up which aim to make this process simpler (disclaimer: I am the author of one of them, PageKite).
These services provide you with a public DNS name and software which connect your "localhost" with an in-the-cloud relay server (a.k.a. a reverse proxy). For example, if you are using PageKite, you can run the following command in the terminal:
$ pagekite.py 80 yourname.pagekite.me
... to create a mapping from http://yourname.pagekite.me/
to the web server running on http://localhost:80
. While the program is running, your localhost site will be visible to the rest of the Internet. In order to make it private again, you simply turn off the pagekite.py connector program.
For completeness, here are some of the localhost tunneling services I am aware of:
- PageKite is Free Software (Python) with a "pay-what-you-want" on-line service. You can create as many long-lived subdomains as you want, a wild-card SSL certificate is included and front-end relays run in multiple geographic locations to provide redundancy and responsiveness.
- Localtunnel is a free-of-charge (sponsored by Twilio) Ruby solution which gives connections temporary names. Note that names are recycled so you may see unexpected traffic while the connection is live.
- Showoff.io and Tunnlr.com are proprietary paid service comparable to Localtunnel, based on the same basic technology (ssh tunnels).
(Sorry about not linking to the last two, SO spam protection is preventing me from being fair to my competitors. ;-)

- 624
- 4
- 6
-
Another similar paid option that's appeared since your post is Forward (www.forwardhq.com). – Taylor D. Edmiston Feb 15 '14 at 22:07
Found this great, free tool today - really easy to set up and works like a charm! Versions for Mac OS, Linux and Windows also available.
(am not associated with it in any way)

- 2,896
- 5
- 34
- 45
This worked for me for accessing rails server with IP over local network:
- The firewall has to be turned off.
/etc/hosts should have this entry:
127.0.0.1 192.168.100.12
where 192.168.100.12 is the ip address which can be found by ifconfig command in terminal.
Start rails server with this command:
rails server -b 0.0.0.0 -p 8080
I was able to access my localhost through http://192.168.100.12:8080/

- 493
- 1
- 5
- 16
-
1I too was running a ruby server and `-b 0.0.0.0` fixed my problem! I was losing my mind over this. Ty! – Tristan Ratchford Aug 02 '17 at 16:41
-
i followed the steps and i got "You don't have authorisation to view this page" 403 error – Ahmed El Kilani Apr 20 '22 at 12:22
Additionally, if you want to test on Android/iOS device a PWA Apps developed with Angular, you will need to use:
ng serve --host 0.0.0.0
to start up the server CLI.
If you receive "Invalid Host Header", uses:
ng serve --host 0.0.0.0 --disableHostCheck true

- 4,002
- 1
- 34
- 32
after reading this thread (and the suggestions working!) I put together a single guide to solving this issue. This link to that guide that has screenshots for every step and where to look. Or the text is pasted below. Thank you for the help!
First off, both your phone and laptop must be connected to the same WiFi network. If you're using your phones mobile hotspot, it will still work. However, make sure to connect both devices to the same network before moving forward.
Next, collect your phones IP address. To do so, I use the Network Info II app. There is most definitely another way to accomplish this. However, Network Info II works as needed and is document for this tutorials sake.
Now open the terminal on your computer (don't worry about which directory you're within) and run the command sudo nano /etc/hosts/. After entering your system password, you'll see near the top of the terminal a string of numbers with the word localhost following. Localhost is in fact just an alias for your computer's own server address so that when you go to localhost in a browser it simply routes the http request to your local machine. In order to tie your phone into this loop, enter its IP address right between the string of numbers and Localhost.
To save this, hit control + X and then Y when prompted to save. After that, the enter key will bring you back to the standard command line.
Open a second tab in the terminal and launch a local server. I've only tested this using a simple python server, which can be run by running in the terminal python -m SimpleHTTPServer 8000. However, I'm assuming that you can launch any local server you like, being that all you'll need to reference is the port number. If you do use the simple python server, that port number is 8000.
Run ifconfig in the terminal. This will bring up a slew of information, to which you should scroll about halfway down. What you are looking for is a string of numbers that follow after an inet and before netmask within either the en0: or en1: key.
Done! On your Android, open up a browser and visit the inet number, followed by a colon (:) and the port number.

- 31
- 1
-
3Why does Chrome think your link is an attack site? That plus your reputation is mighty suspicious. – SMBiggs Oct 27 '16 at 05:11
MacOS Catalina 10.15.4
- Go to Settings -> Security and Privacy.
- Select Firewall tab and unlock the settings using your laptop password:
- Select Firewall Options... and in the dialog uncheck the box for Block all incoming connections and hit OK.
- Open Terminal and type ifconfig. Look for the en0. In the section find the address sitting near inet - this is the address of your laptop in a local network.
en0: flags0=<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 0
...
inet 192.168.0.123 netmask 0xffffff00 broadcast 192.168.0.255
...
- Use the IP address + the port (in case you are using localhost:4200 for development), otherwise omit the port:
http://<IP address found in en0 -> inet>:<port if needed>
- Profit!

- 184
- 1
- 9
Although one answer has been accepted but this worked for me:
- Make sure both your computer and android device are on same network.
- get your computer's ip address. (in case of mac type
ifconfig
inside terminal and look foren0/en1 -> inet
it should be something like19*.16*.1.4
.) Open your mobile settings and go to network-> long press your wifi network-> modify network -> scroll down to advanced options -> chose proxy manual and in proxy hostname type your address i.e.
19*.16*.1.4
inside port enter your port. and save it.search for
what is my ip
in google chrome, and get your ip, lets say it is1**.1**.15*.2**
- try accessing
1**.1**.15*.2**:port/
from your mobile device.
It is as simple as this.

- 5,789
- 1
- 34
- 59
If you run your localhost with php the solution was replacing localhost
with the Macs IP
php -S 192.168.x.x:8080
instead of
php -S localhost:8080

- 3,419
- 2
- 25
- 49
For MacOs, open up System Preferences > Network.
you should see the message " and has the IP address: my-ip-address"
That will be your IP Address, then in the browser navigate to http://my-ip-address/port-number. Remember not to put an "s" in the http.

- 2,132
- 3
- 21
- 33
As mentioned in other answers, url has to consist of the IP address of computer and not localhost. If that isn't working, before trying anything else, check if the port is correctly mentioned along with IP address when making an api call in the Android app. That is, you should be making an api call to a url which is of the format: http://192.168.X.X:80/api/..

- 469
- 1
- 4
- 13
For using localhost on Android you should use your local ip address when connecting to it. First check your local ip address of your computer. Then follow along:
In addition to the good accepted answer, if you need to use localhost via your local ip address follow this steps:
Add your local ip address into httpd.conf (/etc/apache2/httpd.conf) as below:
(Change the port according to server you are using)
Listen 12.34.56.78:80
Listen: Allows you to bind Apache to specific IP addresses and/or ports, instead of the default.
Save, close and restart apache.

- 1,780
- 12
- 15