Packaging binaries for Linux

I had the pleasure to prepare a binary (including dependencies) for Linux a few months ago. It’s been a while since I had to do that, and I forgot how difficult it is. Here’s some advice:

Compile libraries yourself

This is probably the most important point. You can either link statically or link dynamically and ship your shared objects, the latter usually being a lot less problematic. If you ship the shared objects prepared by your distribution, you will introduce lots of unnecessary direct and transitive dependencies, forcing you to add tons of additional libraries. For instance, here goes an ldd of the core SDL library on Ubuntu 10.10:

linux-vdso.so.1 =>  (0x00007fff64bce000)
libm.so.6 => /lib/libm.so.6 (0x00007f6888cfe000)
libdl.so.2 => /lib/libdl.so.2 (0x00007f6888afa000)
libpulse-simple.so.0 => /usr/lib/libpulse-simple.so.0 (0x00007f68888f5000)
libpulse.so.0 => /usr/lib/libpulse.so.0 (0x00007f68886b4000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00007f6888497000)
libc.so.6 => /lib/libc.so.6 (0x00007f6888113000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6889236000)
libpulsecommon-0.9.21.so => /usr/lib/libpulsecommon-0.9.21.so (0x00007f6887ec7000)
librt.so.1 => /lib/librt.so.1 (0x00007f6887cbf000)
libX11-xcb.so.1 => /usr/lib/libX11-xcb.so.1 (0x00007f6887abc000)
libX11.so.6 => /usr/lib/libX11.so.6 (0x00007f6887786000)
libICE.so.6 => /usr/lib/libICE.so.6 (0x00007f688756b000)
libSM.so.6 => /usr/lib/libSM.so.6 (0x00007f6887361000)
libXtst.so.6 => /usr/lib/libXtst.so.6 (0x00007f688715a000)
libxcb-atom.so.1 => /usr/lib/libxcb-atom.so.1 (0x00007f6886f55000)
libxcb.so.1 => /usr/lib/libxcb.so.1 (0x00007f6886d37000)
libwrap.so.0 => /lib/libwrap.so.0 (0x00007f6886b2c000)
libsndfile.so.1 => /usr/lib/libsndfile.so.1 (0x00007f68868c7000)
libdbus-1.so.3 => /lib/libdbus-1.so.3 (0x00007f6886684000)
libuuid.so.1 => /lib/libuuid.so.1 (0x00007f688647f000)
libXext.so.6 => /usr/lib/libXext.so.6 (0x00007f688626c000)
libXi.so.6 => /usr/lib/libXi.so.6 (0x00007f688605c000)
libXau.so.6 => /usr/lib/libXau.so.6 (0x00007f6885e59000)
libXdmcp.so.6 => /usr/lib/libXdmcp.so.6 (0x00007f6885c52000)
libnsl.so.1 => /lib/libnsl.so.1 (0x00007f6885a38000)
libFLAC.so.8 => /usr/lib/libFLAC.so.8 (0x00007f68857ee000)
libvorbisenc.so.2 => /usr/lib/libvorbisenc.so.2 (0x00007f688531e000)
libvorbis.so.0 => /usr/lib/libvorbis.so.0 (0x00007f68850f2000)
libogg.so.0 => /usr/lib/libogg.so.0 (0x00007f6884eeb000)

That’s quite a lot of libraries, some of which you can’t even reasonably ship (I’m looking at you, pulseaudio). The solution? Build your required libraries yourself, and do it on the oldest and humblest distribution you want your software to work on. I’m building our current C++ game project and its dependencies on Debian Lenny, this makes it work on all reasonably current popular distributions. Furthermore, if you build the libraries yourself, you can usually configure it to compile only the features you need, reducing size and dependencies even further. This is an ldd of the core SDL library I built:

linux-gate.so.1 =>  (0xf77a7000)
libm.so.6 => /lib32/libm.so.6 (0xf76d3000)
libdl.so.2 => /lib32/libdl.so.2 (0xf76cf000)
libpthread.so.0 => /lib32/libpthread.so.0 (0xf76b5000)
libc.so.6 => /lib32/libc.so.6 (0xf755a000)
/lib/ld-linux.so.2 (0xf77a8000)

See the difference? Also make sure to ship a 32 bit version, preferably both 32 and 64 bit.

Check distribution compatibility

There are lots of Linux distributions out there, and you’ll probably want to support as many as possible. If you compile libraries yourself, you’re definitely on the right track, but you should also check for compatibility issues and see what you can do about them. You can either get dozens of distributions and check if it works, or you can use the excellent Linux Application Checker tool:

Reduce dependencies

Reducing your dependencies is good, but do you know what’s even better? Get rid of them. You really shouldn’t overdo this, but look at each of your dependencies and think about whether you really need them. Should you include a large library if you need just one function? Is there a more coherent alternative? Would it be possible and perhaps faster or easier if you just wrote the required functionality yourself?

4 Responses

  1. Cool post, I always wondered how to package a binary that is not part of the distribution’s package management. Thanks for the info!

Leave a Reply