If you’ve ever tried to start up multiple Virtual Machines using Laravel Homestead, you may have encountered the following error:
1 |
Vagrant cannot forward the specified ports on this VM, since they would collide with some other application that is already listening on these ports. The forwarded port to 8000 is already in use on the host machine. |
Basically, Vagrant is complaining that you’ve told it to try to forward a port on the guest (VM) machine to a port that is already in use on your host machine. It’s unable to do that, so it spits out this error and stops. But you haven’t told it to do any port forwarding; you may not even want any ports forwarding! So what’s going on?
This is due to Homestead having a default set of port mappings which automatically forwards the ports of typical services a web developer would use, to the host machine (such as the web server and MySQL database). This is usually very convenient and follows Laravel’s style of providing you with a default sane configuration which lets you get something up and running as quickly as possible.
82 83 84 85 86 87 88 89 90 |
# Default Port Forwarding default_ports = { 80 => 8000, 443 => 44300, 3306 => 33060, 5432 => 54320, 8025 => 8025, 27017 => 27017 } |
Changing the port mappings
If you wish to change the port mappings that are in conflict you can do this via the Homestead.yaml file. In fairness to the Laravel team, they do provide instructions on these port mappings and how to forward additional ports if you need too here. However they don’t really tell you that you can use the same syntax to override these default ports too. So all you have to do is open up that file and add the overrides.
1 2 3 4 5 |
ports: - send: 80 to: 8001 - send: 443 to: 8002 |
Keep the to port above 1024 (below that they are restricted) and you’re good to go. You can also use guest and host in replace of send and to respectively if you wish.
Disable default port mapping
Alternatively, you may prefer to disable the default port mappings completely. I don’t use the forwarded ports as I prefer to create .test domains instead that point to my VM. In fact this is what I wanted to do prior to deciding to document it in this blog post. I have a variety of programs and VM’s online at any given time, normally they don’t collide (albeit out of pure luck) but this time they did and it annoyed me enough to look into it.
The answer isn’t documented (that I can see at the time of writing this) but if you take a quick look inside the homestead.rb file of the homestead source code, you’ll find this line:
94 |
unless settings.has_key?("default_ports") && settings["default_ports"] == false |
Notice the false check on default_ports? So all you have to do, is add this to your homestead.yaml file, and you’re good to go!
15 16 17 18 19 20 21 22 |
sites: - map: homestead.test to: /home/vagrant/code/public databases: - homestead default_ports: false |