28

How do I connect to a remote debugging instance of Byebug (for use with Pow, etc)?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Joseph Siefers
  • 1,282
  • 1
  • 12
  • 23

3 Answers3

40

Joseph's answer was good but confusing in some small ways. He places the starting of the byebug server in config/environments/development.rb, when it would be better in an initializer. Additionally the exporting of the environment variable go in .powenv or .powrc. This is how I got it to work.

In your gemfile:

gem 'byebug'

On the command line:

bundle install

If you are using Pow, add the following to .powenv:

export BYEBUGPORT=3001

If you are using other frameworks (e.g. just foreman), you may have to modify .env instead.

In config/initializers/byebug.rb

if Rails.env.development? and ENV['BYEBUGPORT']
  require 'byebug/core'
  Byebug.start_server 'localhost', ENV['BYEBUGPORT'].to_i
end

And finally on the command line:

touch tmp/restart.txt

Once you go to your pow site, the byebug server should be started. On the command line you can now do:

[bundle exec] byebug -R localhost:3001
slhck
  • 36,575
  • 28
  • 148
  • 201
Nick Gronow
  • 1,597
  • 14
  • 14
  • 1
    Yup, followed the instructions and it works like a charm. Thank you Nicholas! – Patrick Reiner Mar 01 '15 at 19:08
  • 1
    Note that "touch tmp/restart.txt" may not be needed, depending on app server. Needed for Passenger in production, usually not needed in development on any app server. – Noah Gibbs Apr 14 '15 at 00:42
  • Works nicely, also with [puma-dev](https://github.com/puma/puma-dev). Use ~/.powconfig instead of .powenv for global configuration. – Manuel Meurer Jun 13 '17 at 08:12
33

I had to piece together information from several different sources to accomplish the above, so I thought I'd include a consolidated guide here for convenience:

Here are the steps:

  1. In config/environments/development.rb, add:

    require 'byebug'
    
    #set in your .powconfig
    if ENV['RUBY_DEBUG_PORT']
      Byebug.start_server 'localhost', ENV['RUBY_DEBUG_PORT'].to_i
    else
      Byebug.start_server 'localhost'
    end
    
  2. Restart Pow and visit yourapp.dev

  3. Run the following:

    [bundle exec] byebug -R localhost:<port_you_defined_in_pow_config>
    

You should see a successful connection to the remote instance.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Joseph Siefers
  • 1,282
  • 1
  • 12
  • 23
  • Thank you for this. I'm able to connect to the server, but my breakpoints aren't breaking. Any ideas? Places to start? Thanks! – Andy Adams Jun 06 '14 at 02:06
  • Are you using pow? If so, try the first answer in http://stackoverflow.com/questions/8996498/ruby-debug-with-pow-breakpoints-never-hit – Joseph Siefers Jun 06 '14 at 16:18
  • 2
    I am using pow. I tried the answer, no luck. I'm sitting at a terminal with `Connecting to byebug server localhost:12346. Connected.` and lots of `byebug` calls in my code, but no breaking! I'm at a loss at this point - if you have any other ideas, I'm all ears! Otherwise, thank you anyhow for your help! – Andy Adams Jun 06 '14 at 18:31
  • 1
    Note: don't use the question Joseph mentions for Ruby 2.0 or higher! – Noah Gibbs Apr 14 '15 at 00:43
  • @Noah could you please elaborate? – Joseph Siefers Apr 17 '15 at 20:54
  • @JosephSiefers sure. Ruby-debug (original) is 1.8 and lower only. Rdebug19 is still only through 1.9, no 2.0+. You have to use byebug instead of rdebug or rdebug19 for Ruby 2.0 or higher. Debuggers are fairly finicky extensions, they tend to break when Ruby changes the internals. – Noah Gibbs Apr 19 '15 at 20:01
  • @Noah I see what you're getting at now, thanks for clarifying. Although that question references rdebug, it is still relevant and applies to byebug also under Ruby 2.0. The first response explains clearly that pow has multiple worker threads and it's possible that the remote debugger (in this case, byebug) will connect to the wrong thread (thus causing missed breakpoints). – Joseph Siefers Apr 20 '15 at 20:39
  • 2
    Note that you need to add `require 'byebug/core'` at the moment — see https://github.com/deivid-rodriguez/byebug/issues/185#issuecomment-155256967 – Whatcould Dec 08 '15 at 15:30
7

In your code

remote_byebug

Invoke your code (e.g., by refreshing the page). This will spin up a local byebug server on port 8989. Your code will then 'hang' waiting for a client connection.

Connect to it via the terminal:

byebug -R localhost:8989

Manually configuring the server is no longer necessary as of https://github.com/deivid-rodriguez/byebug/pull/406/files

DylanReile
  • 756
  • 7
  • 11