Building ChordPro from source

11 March 2021

I (re)generate the songsheets for bahaisongproject.com on Netlify with ChordPro, each time I push to the bahai-songs repository. Netlify is limited in the types of dependencies it supports out of the box. Perl modules, such as ChordPro, are not included. But Netlify does support custom binaries.

The ChordPro releases on GitHub do not include pre-built binaries for Linux, but you can build the binary yourself.

The basic steps are:

  1. Install dependencies
    • Perl prerequisites
    • HarfBuzz, a text shaping library
    • HarfBuzz::Shaper, a Perl wrapper for a subset of HarfBuzz
  2. Download ChordPro
  3. Build ChordPro

Install dependencies and download ChordPro

The following Dockerfile describes how to do steps 1 and 2. For step 3, building ChordPro, we will build the Docker image, run a container and then build ChordPro.

FROM perl

RUN echo | cpan
RUN cpan PAR::Packer String::Interpolate::Named Text::Layout File::LoadLines PDF::API2 Devel::CheckLib App::Packager

RUN DEBIAN_FRONTEND="noninteractive" apt-get update \
  && apt-get install -y\
    wget \
    libharfbuzz-dev \
  && rm -rf /var/lib/apt/lists/*

RUN wget https://github.com/sciurius/perl-HarfBuzz-Shaper/archive/R0.016.tar.gz && tar -xzf R0.016.tar.gz
RUN wget https://github.com/ChordPro/chordpro/archive/R0.978.tar.gz && tar -xzf R0.978.tar.gz

WORKDIR /perl-HarfBuzz-Shaper-R0.016
RUN perl Makefile.PL && make && make test && make install
WORKDIR /

Let's go through it step by step.

  • Start with a perl image.
FROM perl
  • The first time you run cpan, you need to confirm a prompt with enter. You can achieve this by piping echo into cpan.
RUN echo | cpan
  • Install prerequisites with cpan.
RUN cpan PAR::Packer String::Interpolate::Named Text::Layout File::LoadLines PDF::API2 Devel::CheckLib App::Packager
  • Install wget (for downloading HarfBuzz::Shaper and ChordPro).
RUN DEBIAN_FRONTEND="noninteractive" apt-get update \
  && apt-get install -y\
    wget \
    libharfbuzz-dev \
  && rm -rf /var/lib/apt/lists/*
  • Download and extract the HarfBuzz::Shaper and ChordPro source code. I used the most recent releases at the time of writing, but you can of course try changing that.
RUN wget https://github.com/sciurius/perl-HarfBuzz-Shaper/archive/R0.016.tar.gz && tar -xzf R0.016.tar.gz
RUN wget https://github.com/ChordPro/chordpro/archive/R0.978.tar.gz && tar -xzf R0.978.tar.gz
  • Set the working directory to /perl-HarfBuzz-Shaper-R0.016 run the HarfBuzz::Shaper build commands and set the working directory back to /.
WORKDIR /perl-HarfBuzz-Shaper-R0.016
RUN perl Makefile.PL && make && make test && make install
WORKDIR /

Build ChordPro

If you're using Docker, you can now build the image.

docker build . -t daysm/chordpro-dev

Instead of building the image yourself, you can also pull my pre-built-image from Docker Hub (docker pull daysm/chordpro-dev).

Then run the container.

docker run -it daysm/chordpro-dev /bin/bash

To build ChordPro, cd into /chordpro-R0.978/pp/linux and run:

pp --output=chordpro @chordpro.pp ../../script/chordpro.pl

This will give you the linux command line binary. You can copy it to the host with docker cp (or run docker with a mounted directory).