Errno::EADDRINUSE
RubyERRORNotableNetwork

Address already in use

Quick Answer

Set SO_REUSEADDR on the socket before binding, or identify and stop the process using the port.

What this means

Raised when a server tries to bind to a port that is already in use by another process. Common when restarting a server before the OS releases the previous port. Maps to POSIX errno 98.

Why it happens
  1. 1Restarting a server before the previous instance has fully released the port
  2. 2Another process is already listening on the same port
  3. 3TIME_WAIT state from a recent connection keeping the port occupied

Fix

Set SO_REUSEADDR on the server socket

Set SO_REUSEADDR on the server socket
server = TCPServer.new(3000)
server.setsockopt(:SOCKET, :SO_REUSEADDR, true)

Why this works

SO_REUSEADDR allows binding to a port in TIME_WAIT, enabling fast server restarts.

Code examples
Reproducing the errorruby
TCPServer.new(80)
# Errno::EADDRINUSE: Address already in use - bind(2) for 0.0.0.0:80
Find and kill the process using the portruby
# Shell: lsof -i :3000 | grep LISTEN
# Then: kill -9 <PID>
Rescue in server startupruby
begin
  server = TCPServer.new(port)
rescue Errno::EADDRINUSE
  abort "Port #{port} is already in use"
end
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors