Remote Debugging with Byebug, Rails and Pow

Byebug is a simple to use, feature rich debugger for Ruby 2.x. In this post, we'll discuss how to set up remote debugging with byebug so that you can debug code running in Pow, Unicorn or other application servers.

If you haven't seen byebug before, I recommend that you check it out. It's a great debugger for Ruby 2.x. In the words of its authors:

Byebug is a simple to use, feature rich debugger for Ruby 2. It uses the new TracePoint API for execution control and the new Debug Inspector API for call stack navigation, so it doesn't depend on internal core sources. It's developed as a C extension, so it's fast. And it has a full test suite so it's reliable.

The basic setup is pretty simple. Just install the gem. If you use the byebug method anywhere in your code, execution will stop at that point and you'll be dropped into a debug console. You can ever set it up to use pry.

For example, I could invoke the debugger like so:

require 'byebug'

def my_method
  a = 1
  byebug
end

my_method()

If I run that file, I get dropped into the debugger.

When the debugger is triggered, it drops you into an interactive shell. When the debugger is triggered, it drops you into an interactive shell.

The problem with Pow

That's fine if you can run you app on the command line. But what if you're developing a Rails app using pow to serve it locally?

Pow, like most app servers, runs in the background. So even if you did manage to stop execution and run the debugger, you wouldn't be able to interact with it!

Fortunately, byebug provides a mechanism for remote debugging.

If you're not familiar with remote debugging, it's a simple concept. When the byebug method is called, instead of dumping you to an interactive shell, the debugger fires up its own special server. You can then connect to this debug server using a command-line client.

What does remote debugging look like?

First, I call the byebug method to a controller action

class PagesController < ApplicationController
  def index
    if user_has_never_signed_in? && request.subdomain == "www"
      @hero_bg = ab_test("hero_bg", "control", "variable")
    else
      @hero_bg = "control"
    end

    byebug

  end
end

Second, I make a request to that action. The connection appears to hang.

A web request that triggers the debugger doesn't load A web request that triggers the debugger doesn't load

Finally, I switch over to my byebug client, which is showing a debugger console at exactly the point I specified.

The byebug remote debugger in action The byebug remote debugger in action

Setting up remote debugging with byebug

If you don't already have the gem installed, go ahead and install it.

# Gemfile

gem "byebug", group: "development"

Next, we need to add an initializer to our rails app that starts up the byebug server whenever pow starts our rails app. The only real option you need to provide is a port number. The code below lets you configure that via an environment variable.

Once you add this initializer, you'll probably need to to restart the app make sure that it's been loaded.

# config/initializers/byebug.rb

if Rails.env.development?
  Byebug.start_server 'localhost', ENV.fetch("BYEBUG_SERVER_PORT", 1048).to_i
end

Finally, you'll need to run the byebug client and tell it where to find our server. The client will then sit and wait for a debug trigger to occur.

If you used a different port, you'll need to substitute that here.

bundle exec byebug -R localhost:1048

That's it! Time to start debuggin'.

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like: