4

When I attempt to start my rails server from my local machine, I get the following error message.

/Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:269:in `initialize': Address already in use - bind(2) for "0.0.0.0" port 3000 (Errno::EADDRINUSE)
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:269:in `new'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:269:in `add_tcp_listener'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:105:in `block in parse'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:88:in `each'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:88:in `parse'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/runner.rb:144:in `load_and_bind'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/cluster.rb:391:in `run'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/launcher.rb:174:in `run'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/rack/handler/puma.rb:69:in `run'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rack-2.0.3/lib/rack/server.rb:297:in `start'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands/server.rb:104:in `start'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:90:in `block in server'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:85:in `tap'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:85:in `server'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands.rb:18:in `<top (required)>'
from /Users/zachdobbs/sample_app/bin/rails:9:in `require'
from /Users/zachdobbs/sample_app/bin/rails:9:in `<top (required)>'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:68:in `require'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:68:in `require'
from /Users/zachdobbs/sample_app/bin/spring:15:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'

This error occurred suddenly and I'm not sure what the cause of it could be. When I looked online I noticed that the issue could be another service running on the port 3000, but when I try and run 'lsof -i tcp:3000' I get no results.

Within puma.rb, I define.

port        ENV.fetch("PORT") { 3000 }
rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

When I change the port value to something such as 8080 the server will run. I am confused as to why the server will not launch on port 3000. Every command I have attempted to run has shown me no services running on that port.

Zach Dobbs
  • 63
  • 1
  • 7

3 Answers3

3

I know this is a really old post but I had to answer just in case others get here as I did.

When you declare port or bind commands in the puma config it will try to bind to all those ports or addresses.

In this case you've specified port twice so it's trying to bind to both and the second one will fail.

PhilT
  • 4,166
  • 1
  • 36
  • 26
  • This is correct. When using puma, it use default port 3000. It will throw this error when we specific same port. – babie Nov 24 '21 at 08:01
0

Try the following:

> sudo ps ax | grep rails
#1338 pts/1    Sl+    0:06 /usr/bin/ruby2.3 bin/rails c
#1707 pts/0    Rl+    0:07 /usr/bin/ruby2.3 bin/rails s

> sudo kill -9 1707

> rails s
Abhi
  • 4,123
  • 6
  • 45
  • 77
  • Executing `sudo ps ax | grep rails` gives me one result: `1559 s000 S+ 0:00.00 grep rails` and attempting to do `sudo kill -9 1559` results in `kill: 1559: No such process` – Zach Dobbs Sep 12 '17 at 14:28
  • No you shouldn't kill the **grep rails**, you have to kill the process id that ends with `bin/rails s`. I have `rails c` mentioned as I'm running rails console as well – Abhi Sep 12 '17 at 14:31
  • Ah, I see now. The problem still persists however that no server instances are being displayed and port 3000 appears to be unused, so I am still confused as to why the server is failing to launch – Zach Dobbs Sep 12 '17 at 18:35
0

For me, the solution was pretty easy. But please be aware that (as the command says) shuts down ALL instances of ruby. You could solve it more fine-grained than this, e.g. with a strategy like Abhi mentioned.

killall ruby

All other things didn't help me:

killall rails
# -> rails: no process found
killall puma
# -> puma: no process found

You can also consider these possibly related Q/A:

Cadoiz
  • 1,446
  • 21
  • 31