Dev_port_allocator - dynamic port allocation for your phoenix apps in development

I created a small library that enables dynamic port allocation for development servers by selecting an available port at runtime.

The main use case is when working on multiple projects at the same time, or when using worktrees and running several versions of the same project in parallel. This has become increasingly common with AI-assisted workflows, where it’s easy to end up working on multiple branches or tasks simultaneously.

While it’s always possible to manually configure different ports for each project, this library removes that friction and lets things work automatically.

After installing the library, you can use it in your runtime config like this:

port_result = DevPortAllocator.resolve_port()

config :my_app, MyAppWeb.Endpoint, http: [port: port_result.port]

This makes it easy to run multiple Phoenix instances locally without worrying about port conflicts.

lib: dev_port_allocator | Hex
docs: DevPortAllocator — dev_port_allocator v0.1.0

3 Likes

This library seems to provide more control and configuration, but I think it should also be mentioned that dynamic ports do not need a library. Using port 0 will give you an unused port. The library is useful for not having the port be completely random.

6 Likes

aha! Never knew about the port 0 giving you an unused port. If I had known about that, maybe I wouldn’t have built this.

But like you said, maybe it can still be useful because it’s still not going to be random. It’s going to be the port that you said or the next available port. So if you’re starting one Phoenix application it’s going to be port four thousand and if it’s two four thousand and four thousand one and so on.

I didn’t know this, we’ve usually written a random_port/0 helper :exploding_head: Thank you :folded_hands:

1 Like