Installing and updating haproxy from source

One of the tools that I use for my personal websites is haproxy. Here are some distilled instructions for installing it from source. I primarily use Ubuntu.

The center of all this is a shell script that I use to automate the build and install:


err() {
  echo "E: $*" >>/dev/stderr

DIR=`dirname $0`/$1
if [ -e "$DIR" ]; then
  if [ -d "$DIR" ]; then
    cd $1
    make clean &&
    make TARGET=linux-glibc \
      USE_PCRE2_JIT=1 \
      USE_OPENSSL=1 \
      USE_ZLIB=1 \
      USE_SYSTEMD=1 \
    if [ "$RETVAL" -eq 0 ]; then
      make install
      if [ "$RETVAL" -ne 0 ]; then
        err Install failed\!
      make clean
      err Build failed\! skipping install\!
    err location \($DIR\) is not a directory.
  err location \($DIR\) does not exist.

I put this in /usr/local/src, make it executable, and name it “new-haproxy”. All of the following commands need to be executed as root:

To prepare for this on Ubuntu, I install the build-essential package:

apt install build-essential

The next commands requires uncommenting all the “deb-src” lines in /etc/apt/sources.list beforehand:

apt update
apt-get build-dep haproxy

Once that’s done, download the source code to /usr/local/src, extract it, and then run my script, giving it the newly extracted directory as the argument:

cd /usr/local/src
wget ""
tar zxf haproxy-2.4.15.tar.gz
./new-haproxy haproxy-2.4.15

The first time you install haproxy this way, you’ll need to install and activate the haproxy service in systemd. The following instructions should take care of it:

cd haproxy-2.4.15/admin/systemd
make haproxy.service
cp haproxy.service /etc/systemd/system/.
systemctl daemon-reload
systemctl enable haproxy
systemctl start haproxy

If you haven’t created /etc/haproxy/haproxy.cfg with a good config before running those commands, then starting the service will fail, and you’ll need to take care of that after installing a config. Also be aware that if you ask haproxy to listen on ports that another process is already listening on, the service start will also fail.

Installing the service with the commands above should work on an RPM-based distro as well as a DEB-based one. On an RPM-based distro, you will need this command before running my shell script:

yum groupinstall "Development Tools"

And you will need to install the development libraries for openssl, pcre2, zlib, and systemd.

Leave a Reply

Your email address will not be published.