Gerard Altamirano

GitHub
~2 min read

Debugging a rails app with Byebug (and remote byebug)

TL; Won't Read

  1. Use byebug for code you can run in an interactive console
  2. Use remote_byebug for code you can't run in an interactive console.

Introduction

If you worked before with web apps, you might had the desideratum (fancy word to sound cool 😎 ) or the need to debug something. Depending on the language and framework you've worked, you may have used the classic <> , echo for ancient PHP, puts for our dear Ruby, console.log() for JS, color: red; for CSS, and so on. Well with rails a simple "puts" might work, and if the app has better_errors will be easier and handy. But unfortunately not all apps have this gem. So let's see another gem that will help us to debug our apps.

Byebug

In words of its author "Byebug is a simple to use and feature rich debugger for Ruby. It uses the TracePoint API for execution control and the Debug Inspector API for call stack navigation". And it's easy to use, you just need to call byebug like this:

and then we run our method, test, or whatever we're trying to debug (I'm running a test that assert the response should be HTTP 200); and we'll have an output like this:

As you can see byebug gives us a many details regarding the code we're running, including the line the code stopped (21 in this case). To avoid confusions just need to clarify that the pointed line wasn't executed.

I'll type c (you can see all the available commands here) just to see the test fail; this command will continue the execution of the code:

And then you can play around with your code. Happy debugging! 🐞

Remote Byebug

Ok, Byebug seems to work ok. But, what if we work with containerized apps and we can see the byebug output in the container logs, but I CAN'T INTERACT WITH IT!!!!

Chill, there're many workarounds to accomplish that. Unfortunately we're not using any workaround here. We'll be using a byebug built-in functionality called remote_byebug.

So let's understand the situation. You're trying to debug a functionality that doesn't fit with the regular byebug , for example, you're working with a method that's being called in a controller that renders something and there's no test that fits your method. So let's make a test; just kidding. We'll need to use remote_byebug instead.

Using remote_byebug

This is not a good example but I think you can get the idea, note how in the line #3 we're using remote_byebug instead of the regular byebug.

Now to get the same output we need to open a bash of our container:

$ docker exec -it yout_container_name bash

And once we're in the container bash, we'll execute this:

$ bundle exec byebug -R localhost:8989

The port is configured by the gem, it has the 8989 by default but you can change it. If you read the documentation of the gem (and many overflows answers) you'll see that there're many ways to configure remote_byebug just in case you need it.

Important: You should run the buggy code before executing the command. No worries the code will wait until you execute the byebug command in the bash.

And that's it, you should be able to debug your code like you would with the regular byebug

NOTE: Once you've used remote_byebugand you want to add more breakpoints, I recommend using byebug so we don't have problems with the remote server.