How to create an install TGZ (*.tar.gz) for a compiled (eg. via autoconf) application

Let's assume you have an application/project that you've already compiled and would like to pack it into a TGZ (or TAR.GZ) archive with proper permissions ... which most probably involve the root user as well. If you use autoconf, you most probably applied some sort of --prefix. If not, the default --prefix=/usr/local was used. Anyway your Makefile most probably contains some sort of installation target that would copy all the necessary files into their proper destinations. The problem is that you don't want to install locally in the first place and you don't have root access either. How are you going to package the application into a TAR.GZ with proper pathes and owners/permissions?

It's quite easy actually. If we assume that the current working directory is the one where you compiled the application and the target prefix is set in $PREFIX (eg. PREFIX=/usr/local), then the following little shell script should do the job:
tmpdir="$(mktemp -d)"
mkdir -p "${tmpdir}${PREFIX}"
make install-strip prefix="${tmpdir}${PREFIX}"
dir="$(dirname "$(pwd)")"
packagefile="${dir}/$(basename "$(pwd)")-bin.tgz"
cd "${tmpdir}"
fakeroot -- sh -c "chmod -R a+rX,u+w,go-w data && chown -R root:root data && tar czf \"${packagefile}\" data"
cd "${dir}"
rm -rf "${tmpdir}"

The above commands make a few assumptions ...
  • The following commands are available: mktemp, dirname, basename, fakeroot.
    In any recent linux/unix operating system these should be available. Fakeroot might be a bit problematic on Unix boxes though.
  • The installation command is assumed to be make install-strip. If it's something else, just replace it and you're clear to go.
Of course you should finetune the script to your liking (eg. if you want to put some of the files/directories into a user's ownership other than root, then suit yourself). The two main points of the script are:
  1. Use the prefix option of make (in case you use the GNU make or something that supports a prefix) to install the application into a temporary directory instead of the root directory.
  2. Use fakeroot to create a TAR.GZ that has files/directories with root as the owner.
P.S.: this is nothing new or extreme ... various packaging systems (eg. dpkg-buildpackage) use things like this all the time. I used to use this all the time via dpkg-buildpackage. Smile I just put together this post because I never had to do this sort of packaging manually before.