34

When I try and run the server with EventMachine::run I keep getting the error saying that the port is in use. This started ever since I ran the server in the background with the command nohup.

I am pretty sure I have killed the process I started:

  • I found the ruby process with ps, and killed it. It no longer shows up.
  • I also ran lsof -i :8081 (8081 is the port I ran it on) and nothing shows up.
  • Finally, I have changed the port in the ruby program a number of times to obscure ports, and still get the error!

I also thought it could be the lack of me being the root user, so tried it as root to no avail.

I have also restarted the server.

Please let me know if there's anything else I can try.

Note: this is on debian.

6 Answers 6

50

Had the same problem.

Ran lsof -i :3000 (3000 is the port I ran it on).

I found out that the port was being used by ruby. I killed the process using kill -9 *pid*.

When I ran lsof -i :3000 again, nothing showed up.

I then ran rails s and everything works fine now.

1
  • Worked for me too - added to my mental list of future workarounds. Thanks!
    – cjs1978
    Jul 12, 2018 at 12:12
26

It happens when you didn't stop your server correctly, for example when suspended with Ctrl+Z or running the server twice.

If stopped by Ctrl+Z, this works for me:

Get the running process with:

$ ps ax | grep rails
18192 pts/28 Sl+  0:05 /home/admin/.rvm/rubies/ruby-2.1.2/bin/ruby bin/rails c
20496 pts/23 Tl 0:08 /home/admin/.rvm/rubies/ruby-2.1.2/bin/ruby bin/rails s
20919 pts/23 S+ 0:00 grep --color=auto rails

And then kill the process in which rails server running:

$ kill 20496

If it's not closed after a few seconds you can try to "force" close with with -9 (but this prevents any cleanup from Rails):

$ kill -9 20496

Now you can start the server again:

$ rails s
1
  • You can also use fg to bring the process to the foreground again :-) Aug 28, 2016 at 15:44
21

I have finally figured it out: it was actually the IP address I was binding to that was incorrect!

So essentially it is a very misleading error message, and if you get it, check the IP address too.

3
  • 2
    For me it helped changing rails s -b 0.0.0.0 to -b 127.0.0.1 Dec 6, 2017 at 9:33
  • Thank you! I didn't realize this :) Apr 10, 2019 at 16:52
  • This is exactly what happened to me. The computer switched to another wifi network without me noticing. Nov 18, 2022 at 16:57
1

Maybe it will save someone's time: In my case rails server won't start on any port returning the same error. Event after restarting my machine. Other servers that refer to localhost didn't work as well.

The problem was in my /etc/hosts file that somehow became empty. After restoring it to default state the problem was solved.

-1

Running on free port solves the problem by either:

rails s -p 3001

or

ruby script/rails server webrick -e production -p 3001

or

ruby script/rails server thin -e production -p 3001
-1

In my case it was just internet connection issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.