171

I am trying to deploy Rails app with the Puma web server. When trying to start Puma server with a config file bundle exec puma -C config/puma.rb I get an error that the address is already in use.

Does someone know how to fix this?

bundle exec puma -C config/puma.rb
[23699] Puma starting in cluster mode...
[23699] * Version 2.11.3 (ruby 2.0.0-p353), codename: Intrepid Squirrel
[23699] * Min threads: 5, max threads: 5
[23699] * Environment: development
[23699] * Process workers: 2
[23699] * Preloading application
Jdbc-MySQL is only for use with JRuby
[23699] * Listening on tcp://0.0.0.0:3000
/.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `new'
    from /Users/lexi87/.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `add_tcp_listener'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:96:in `block in parse'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:82:in `each'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:82:in `parse'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/runner.rb:119:in `load_and_bind'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/cluster.rb:302:in `run'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/cli.rb:216:in `run'
    from /rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/bin/puma:10:in `<top (required)>'
    from /.rvm/gems/ruby-2.0.0-p353/bin/puma:23:in `load'
    from /.rvm/gems/ruby-2.0.0-p353/bin/puma:23:in `<main>'
    from /.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
    from /.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Cornelius Wilson
  • 2,844
  • 4
  • 21
  • 41
  • 1
    it's exactly what it says. someone is already using port 3000. use netstat to figure out who is on port 3000 – Mircea Jun 25 '15 at 02:09
  • 5
    When I try to kill it I get a error `kill -59780 PID`. Tells me `invalid signal specification`. I used `lsof -wni tcp:3000` to show what is using port 3000. – Cornelius Wilson Jun 25 '15 at 02:13
  • 1
    kill -9 59780 (so "kill -9 pid_id" in general) – Mircea Jun 25 '15 at 02:23
  • You can consider [my answer in this related Q/A](https://stackoverflow.com/a/75231668/4575793), where I also point to [111](https://stackoverflow.com/questions/70592376/address-already-in-use-bind2-for-127-0-0-1-port-3000-errnoeaddrinuse) [222](https://stackoverflow.com/questions/46177751/ruby-on-rails-address-already-in-use-bind2-for-0-0-0-0-port-3000-errno) [333](https://stackoverflow.com/questions/25151736/jekyll-2-2-0-error-address-already-in-use-bind2) – Cadoiz Jan 25 '23 at 09:08

10 Answers10

352

You need to use kill -9 59780 with 59780 replaced with found PID number (use lsof -wni tcp:3000 to see which process used 3000 port and get the process PID).

Or you can just modify your puma config change the tcp port tcp://127.0.0.1:3000 from 3000 to 9292 or other port that not been used.

Or you can start your rails app by using:

bundle exec puma -C config/puma.rb -b tcp://127.0.0.1:3001
Karol Selak
  • 4,248
  • 6
  • 35
  • 65
Zoker
  • 3,849
  • 1
  • 12
  • 12
  • 1
    thank you for this answer. I got same error with OP. and what I realized is I already do `rails s` at another terminal. So that is why I got that error. then I used diff port to start the server `rails s -p 9090` – Fai Zal Dong May 04 '17 at 02:00
187

To kill the puma process first run

lsof -wni tcp:3000 

to show what is using port 3000. Then use the PID that comes with the result to run the kill process.

For example after running lsof -wni tcp:3000 you might get something like

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ruby    3366 dummy    8u  IPv4  16901      0t0  TCP 127.0.0.1:3000 (LISTEN)

Now run the following to kill the process. (where 3366 is the PID)

kill -9 3366

Should resolve the issue

Arbitur
  • 38,684
  • 22
  • 91
  • 128
Sawo Cliff
  • 2,888
  • 1
  • 17
  • 21
  • Thanks @sawo-cliff it solved my problem I had a another application running on port 3000. – Nomis Apr 17 '16 at 05:55
  • 3
    How this answer add more information than the accepted one year ago? – Andre Figueiredo Sep 27 '16 at 20:04
  • 1
    @AndreFigueiredo, not quite sure I follow your question, but from the little I can glean from it, my answer added how to know which process id is running so as to know what to kill. – Sawo Cliff Oct 01 '16 at 11:15
  • Yes. I can see now that the original answer DID include the same info. But I didn't fully understand what I needed to do until I read this version. – Jeff Zivkovic Oct 14 '16 at 22:40
45

you can also try this trick:

ps aux | grep puma

sample output:

myname           77921   0.0  0.0  2433828   1972 s000  R+   11:17AM   0:00.00 grep puma
myname           67661   0.0  2.3  2680504 191204 s002  S+   11:00AM   0:18.38 puma 3.11.2 (tcp://localhost:3000) [my_proj]

then:

kill -9 67661
Abel
  • 3,989
  • 32
  • 31
ana
  • 603
  • 6
  • 17
  • 1
    Use `kill -9 67661` if `kill 67661` doesn't actually kill the process for you (as per the example above). That worked for me! Cheers! – Kyle Krzeski May 18 '20 at 19:29
6

Found the script below in this github issue. Works great for me.

#!/usr/bin/env ruby
port = ARGV.first || 3000
system("sudo echo kill-server-on #{port}")

pid = `sudo lsof -iTCP -sTCP:LISTEN -n -P | grep #{port} | awk '{ print $2 }' | head -n 1`.strip
puts "PID: #{pid}"
`kill -9 #{pid}` unless pid.empty?

You can either run it in irb or inside a ruby file.

For the latter, create server_killer.rb then run it with ruby server_killer.rb

Flavio Wuensche
  • 9,460
  • 1
  • 57
  • 54
2

If the above solutions don't work on ubuntu/linux then you can try this

sudo fuser -k -n tcp port

Run it several times to kill processes on your port of choosing. port could be 3000 for example. You would have killed all the processes if you see no output after running the command

Nsoseka
  • 223
  • 2
  • 9
2

SOLUTION FOR GENERAL Address already in use - bind(2) (Errno::EADDRINUSE)

This issue is because we are trying to use the same port which is already is use. so we have to stop the services running on that port so that we can run another services. we can use kill like kill -9 {PID}where {PID} is the PID of the services running on that port. To know the PID of any services lets say "firefox" we can use commands like pidof firefox, ps aux | grep -i firefox ,pgrep firefox and then use the kill command to stop that service.

sometime we might get into the situation where we don't know the PID or the service name to search for in this case we can use the following little ruby code to do it for us.(in this case port 3000, you can change it according to your need)

#!/usr/bin/env ruby
port = ARGV.first || 3000
system("sudo echo kill-server-on #{port}")

pid = `sudo lsof -iTCP -sTCP:LISTEN -n -P | grep #{port} | awk '{ print $2 }' | head -n 1`.strip
puts "PID: #{pid}"
`kill -9 #{pid}` unless pid.empty?

save it as something.rb and run sudo ruby something.rb

1

You can find and kill the running processes: ps aux | grep puma Then you can kill it with kill PID

Ali
  • 664
  • 3
  • 13
  • 21
0

I had this issue on my Macbook Air, running Rails 5.0.3, Puma 5.2.2

Tried running lsof -wni tcp:3000 but there's no process on this port number.

Managed to fix this by running:

export PORT=3000 on my terminal, then I just added this extra line to my .bash_profile

Dharman
  • 30,962
  • 25
  • 85
  • 135
marco8757
  • 91
  • 1
  • 4
  • I know this is an older post, but I'm currently in the process of updating an older Rails app and came across this answer after running into the problem from the original post. This worked, but I'm not really understanding why that's the case. Could you offer a further explanation of this? I'm going to keep digging in the meantime and update this comment as I find more information. – E Newmy Jun 09 '23 at 21:24
0

Just simple open tmp/pids/server.pid and there you will find pid e.g->42345 just type kill -9 42345

-1

It might be old but in my case, it was because of docker. Hope it will help others.

bxorcloud
  • 659
  • 1
  • 7
  • 20