/ Tutorials

QR codes without leaving Terminal (almost)

At CamioCam we have one heck of a team — 7 here in the Bay Area, 3 in Brazil (our mobile team at Cheesecake Labs), 1 in Chicago, and 1 in Australia — spanning 3 countries and 4 timezones. This make co-ordination, particularly with our mobile team, a challenge. When we want to test a new dev build or release candidate for our Android app it's generally a pain in the ass to get it installed.

I've got 3 Android devices I test with and to update each I need to type in an URL, download the APK, and install it. The worst part for me is the typing. I've got rather large hands, and you know what they say about guys with large hands: they be sayin' "damn you make a lot of typos".

I'm not personally a fan of QR codes — in products at the very least — but they can save you a bunch of time typing URLs into the mobile devices you test with. With a little work you can shorten a link, turn it into a QR code, and display it, all in one simple command.


What's Required

There are 3 things we need to generate QR codes from the command line and quickly display them:

  • A command line URL shortener. We'll use mush, a Ruby gem with support for bit.ly, is.gd, ow.ly, and zae.bz. This step is optional if you use don't mind limiting your URLs to 27 characters with standard QR codes (25×25), but having an URL shorter from your shell is worth it.

  • A QR code generator. Your best bet is libqrencode. It's straightforward to use and there's a package for it in Homebrew.

  • A program to display the QR code from STDIN. ImageMagick will do this for you, and it's a great Swiss Army Chainsaw for image manipulation, even if it's arcane at the best of times.

Installing the Prereqs.

  1. Download and install Homebrew. You probably already know about Homebrew or have it installed, but if not get on it. It's a huge timesaver (when it's not misbehaving), and while it ain't apt, it serves a similar purpose. Best of all it won't overwrite any system libraries, instead suppling and managing its packages in its own folder. If you ever try to install a duplicate package that might cause problems it'll warn you too, most of the time.

  2. Go to bit.ly and sign up for an account. We need an API key to use the URL shortener from the command line. In the settings, go to the Advanced tab and scroll to the bottom to find out what your login and API key are. Add the following line to your ~/.bashrc file replacing the appropriate fields:

alias bitly='bitly -l <BITLY_LOGIN> -k <BITLY_API_KEY> -u'

Now if you ever want to shorten any URL, you can just type:

bitly https://my.url.com

Notice I used an absolute URL there - bit.ly doesn't like it when you don't include the protocol with your URL.

  1. Next we install libqrencode:
brew install libqrencode

If you want to generate a QR code it's as easy as:

qrencode -o <FILE_NAME>.png <YOUR TEXT HERE>

The command line interface for libqrencode is really rich, so I won't go into it more here but linux in the shell has a nice article on it.

  1. Get XQuartz, Apple's implementation of the X window system, better known as X11. You can find the .dmg and instructions on how to install it here. A lot of legacy apps require X11 to run so — like Homebrew — it's worth the time getting it installed.

  2. Install ImageMagick. Installing a fresh copy is easy:

brew install imagemagick --with-x11
gem install rmagick

If you've already installed ImageMagick in the past but are getting errors along the lines of display': delegate library support not built-in image-name.png' then you need to re-install it with the proper compilation flags by running:

brew reinstall imagemagick --with-x11

which will install the binary that plays nice with X11.

Wiring it All Together

I use a couple functions together so I can use individual steps in the pipeline: generate a QR code, shorten an URL, etc. but this is naïve endproduct you can paste into your ~/.bashrc file:

function qru()
  bitly -l <BITLY_LOGIN> -k <BITLY_API_KEY> -u $1 | \
  qrencode -s 10 -o - | \

Pretty straightforward stuff, right? Pipe a shortened URL from the first line into libqrencode to generate a basic QR code with 10×10 pixels (-s 10) and pipe the output to STDOUT (-o -) so ImageMagick can display it.

Oh, is that all?

It seems a bit heavy handed, I'll admit, but if you have Homebrew, X11, and ImageMagick installed it's a piece of cake!

What if I don't want to use a windowing system?

After writing this post I found this nifty npm package called qrcode-terminal which lets you render the QR code in terminal, and can be installed in one command:

npm install -g qrcode-terminal

So much for 5 steps.