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?
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.)
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.
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
Although one answer has been accepted but this worked for me:
ifconfig
inside terminal and look for en0/en1 -> inet
it should be something like 19*.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 is 1**.1**.15*.2**
1**.1**.15*.2**:port/
from your mobile device.It is as simple as this.
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:
(Sorry about not linking to the last two, SO spam protection is preventing me from being fair to my competitors. ;-)
MacOS Catalina 10.15.4
en0: flags0=<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 0
...
inet 192.168.0.123 netmask 0xffffff00 broadcast 192.168.0.255
...
http://<IP address found in en0 -> inet>:<port if needed>