Local development requires you to edit your /etc/hosts
file constantly to add custom local domains. Maintaining this file across machines and projects can become taxing. Let's use a service like dnsmasq
to dynamically set any .localhost
domain to point to 127.0.0.1
# Install dnsmasq
brew install dnsmasq
sudo brew services start dnsmasq
# Configure
emacs /usr/local/etc/dnsmasq.conf
# M1 mac: /opt/homebrew/etc/dnsmasq.conf
# Add something like `address=/localhost/127.0.0.1` to the file
# Restart the service
sudo launchctl stop homebrew.mxcl.dnsmasq
sudo launchctl start homebrew.mxcl.dnsmasq
# Setup the resolver
sudo mkdir -p /etc/resolver
sudo tee /etc/resolver/localhost >/dev/null <<EOF
nameserver 127.0.0.1
EOF
Now you can make new DNS names under the .localhost
domain at your leisure! Try ping lala.localhost
now and you should get valid responses.
- Based on this article