All Case Studies Design Development Interviews Our Way Project Management

What’s a Pair to Do? Pair programming using SSH + TMUX + VIM

We talk about pair programming a lot, but how do you actually do it? Rest easy, read on, and we’ll cover everything needed to pair program.

We talk about pair programming a lot, but how do you actually do it? Rest easy, read on, and we’ll cover everything needed to pair program.

What’s a Pair to Do?

  1. Collaborative Editing: This is a fundamental of pairing. Both people need to be able use the same editor and type together.
  2. Access to the Local Server: It’s pretty hard to develop an app you can’t see.
  3. Ease of Communication: The pair must hear each other, and ideally see each other as well (there’s a lot of non-verbal communication going on when pairing).
  4. Light Weight: The internet connections might be different in quality, so we require something that wouldn't be taxing for small pipelines.

Google Hangout/Skype fails to cover #1, 2 and 4. The lag from sending so much data over the network starts out as merely grating, but quickly crescendos into abject frustration. It tends to make the client of the pairing session little more than an observer.

Given that, the no-brainer was to use VIM inside a terminal-multiplexer that allowed multiple client connection. It could then be attached to the session over SSH. This covers #1, "Collaborative Editing". We can solve two and four by forwarding port 3000 of the host machine to the client by attaching via SSH. Since this was such a lightweight solution, we did eventually opt for Google Hangout for video chat.

If you are both in LAN, you don’t have to worry about allowing the client to gain access to a host machine over the internet. In all other cases, you will have open a port in the firewall and manually forward the port to the machine inside the firewall with static IP. The shortcoming here is that we do not want to have an open a port for every station—we want only the remote-pair to work. SSH tunneling or VPN usage to the rescue.

Pairing Down the Configuration

The configuration we use will make several assumptions:

  • Both machines (host and guest) are in the same LAN (or the host is available at a public IP)
  • Host machine has a dedicated account for pairing
  • Host machine is running ssh server
  • Host machine has git, ruby, rails, vim & tmux installed
  • Your machine will be host

Setup host

In this example, OS X will be used on both machines.

Why a new user account?

For safety of course! Personally I do not want just anyone to connect to my machine and review all my files that are on my disk. We will create a new user, dedicated for pairing.

Creating new user account

Follow this tutorial and create standard account type. Call it pair, or whatever you like. In this article we will be using pair account for the guest.

Be aware that a newly created account is part of staff group and your home directory has read permissions for that group. Remove staff access from your account! Macintosh HD -> Users -> Right click on your account -> Get info -> Change permissions for staff (and others) to write only (dropbox).

The next thing to do is login to the newly created account and setup rbenv/rvm, ruby, rails. I recommend using zsh over bash with oh-my-zsh!

Don't forget to generate ssh keys and add it to your github account!

As the admin user, simply type in su - pair into your terminal. You will have to enter the pair user’s password. If you are lazy like me, you can tweak sudo, and forget about entering a password each time you want to log in as pair. Here's how to do it by the way. (Assuming you are using sudo).

And finally, setup host machine to accept connections via ssh. Do not forget to allow the pair user to login via ssh.

Getting tmux

The easiest way to install tmux on your machine is to use homebrew. Personally, I recommend installing wemux. From github:

wemux enhances tmux to make multi-user terminal multiplexing both easier and more powerful. It allows users to host a wemux server and have clients join in either:

  • Mirror Mode - gives clients (another SSH user on your machine) read-only access to the session, allowing them to see you work, or

  • Pair Mode - allows the client and yourself to work in the same terminal (shared cursor)

  • Rogue Mode - allows the client to pair or work independently in another window (separate cursors) in the same tmux session.

It features multi-server support as well as user listing and notifications when users attach/detach.

The default setup will use Pair Mode.

To install it, open your terminal, and type:

<code>brew install tmux wemux </code>

tmux.conf

Put the following in ~/.tmux.conf, for both the admin and pair account:

<code>set -g mode-mouse on set -g mouse-resize-pane on set -g mouse-select-pane on set -g mouse-select-window on set -g default-terminal &quot;screen-256color&quot; set -g history-limit 1000 </code>

These settings create proper terminal handling and add support for a mouse in tmux. If you want to learn more, check out this article or just RTFM.

Getting VIM

This topic could have it’s own article, but to keep it simple, I recommend using vimified. Remember that you will have to setup vim with the pair account.

Setup ssh verification by key

This step is optional, but I strongly recommend using it. It allows the client to login without knowing the user password (comes in handy when you configure your sudo wrong). Get the client public key, add it to ~/.ssh/authorized_keys (pair user).

Pair FTW!

After the above steps, you should have your machine setup as host machine. If your machine is available at a public IP (or both host and client are inside the LAN) just login as a pair, type in wemux and fire up vim & rails. Give a colleague the necessary credentials (I strongly recommend using ssh verification by key) to log in, and when we connect via ssh, they just have to type in wemux.

Pair mode has been engaged. You’re ready to change the world together.

Host behind NAT

But what if your machine is behind NAT and you don’t have an access to the router?

The easiest solution is to set up tunneling to your machine. What's this?.

The simplest solution is to use localtunnel. Open tunnels to both ssh and rails server by opening two terminals and enter localtunnel 22 in one, and localtunnel 3000 in second one. Provide links to the client. Sadly, localtunnel is slow, but it’s the easiest solution that doesn’t require technical knowledge on the above solutions.

If you have your own server with a public IP (and a good up/down connection) you could make it yourself, by creating reverse ssh tunnel. Remember that the server needs some settings configured (AllowTcpForwarding Yes).

Personally, I use autossh for creating tunnels. When a tunnel goes down, autossh will restart it.

You can also use reverse ssh tunneling. Google it to learn more.

The third option is to use VPN that allows users to connect to one another. For example: OpenVPN in p2p mode, with the client-to-client setting on. The best for us was bridged mode (and yes, we tested all the options).

What about the client?

From a client perspective it’s a rather easy setup - connect to host via ssh, open tmux (wemux) and voila!

Are there any graphical options?

Yes, you can use screenhero, teamviewer or simply setup VNC. For VNC, you can tunnel the appropriate ports with ssh tunneling as described above.

Having a programming session with a potential recruit is only one element of the hiring puzzle, but it's often the most important one. Learn How Pair Programming Can Help You In Recruitment

Why it is impossible to hire quality engineers in London and how to deal with it
Scaling SaaS
READ ALSO FROM Dev
Read also
Need a successful project?
Estimate project or contact us