Install, DESTDIR & PREFIX
Building a program is half the job; users and package managers also need to install it: copy the binary, headers, man pages and data files into the right directories. GNU projects settled the conventions decades ago, and distribution packagers (Debian, Fedora, Homebrew, Nix) rely on them. If your Makefile follows them, packaging your project is trivial.
The conventional variables
| Variable | Default | Meaning |
|---|---|---|
PREFIX (GNU spelling: prefix) |
/usr/local |
root of the installation |
BINDIR (bindir) |
$(PREFIX)/bin |
programs |
LIBDIR (libdir) |
$(PREFIX)/lib |
libraries |
INCLUDEDIR (includedir) |
$(PREFIX)/include |
headers |
MANDIR (mandir) |
$(PREFIX)/share/man |
man pages |
DESTDIR |
empty | staging root prepended to every install path |
INSTALL |
install |
the copy command |
PREFIX is where the files will live at run time; it may be compiled into the program (for example to find data files). DESTDIR is a temporary directory used only while installing: a packager runs make install DESTDIR=/tmp/pkgroot PREFIX=/usr, the files land in /tmp/pkgroot/usr/bin/…, and the package tool archives that tree. The program still believes it lives in /usr. Never bake DESTDIR into the binary.
A Makefile with install and uninstall
mkdir -p hello && cd hello
cat > hello.c <<'SRC'
#include <stdio.h>
#ifndef DATADIR
#define DATADIR "."
#endif
int main(void) { printf("hello (data in %s)\n", DATADIR); return 0; }
SRC
printf '.TH HELLO 1\n.SH NAME\nhello \\- say hello\n' > hello.1
cat > Makefile <<'MK'
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
DATADIR ?= $(PREFIX)/share/hello
MANDIR ?= $(PREFIX)/share/man
INSTALL ?= install
CFLAGS ?= -O2
.PHONY: all install uninstall clean
all: hello
hello: hello.c
$(CC) $(CFLAGS) -DDATADIR='"$(DATADIR)"' lt; -o $@
install: all
$(INSTALL) -d $(DESTDIR)$(BINDIR) $(DESTDIR)$(MANDIR)/man1 $(DESTDIR)$(DATADIR)
$(INSTALL) -m 755 hello $(DESTDIR)$(BINDIR)/hello
$(INSTALL) -m 644 hello.1 $(DESTDIR)$(MANDIR)/man1/hello.1
uninstall:
rm -f $(DESTDIR)$(BINDIR)/hello $(DESTDIR)$(MANDIR)/man1/hello.1
clean:
rm -f hello
MK
make -j1 install PREFIX=/usr DESTDIR=$PWD/stage
find stage | sort
stage/usr/bin/hello
stat -c '%a %n' stage/usr/bin/hello stage/usr/share/man/man1/hello.1cc -O2 -DDATADIR='"/usr/share/hello"' hello.c -o hello
install -d /home/learner/hello/stage/usr/bin /home/learner/hello/stage/usr/share/man/man1 /home/learner/hello/stage/usr/share/hello
install -m 755 hello /home/learner/hello/stage/usr/bin/hello
install -m 644 hello.1 /home/learner/hello/stage/usr/share/man/man1/hello.1
stage
stage/usr
stage/usr/bin
stage/usr/bin/hello
stage/usr/share
stage/usr/share/hello
stage/usr/share/man
stage/usr/share/man/man1
stage/usr/share/man/man1/hello.1
hello (data in /usr/share/hello)
755 stage/usr/bin/hello
644 stage/usr/share/man/man1/hello.1
The binary was staged under stage/ but reports /usr/share/hello, its real home after packaging. Details:
install -dcreates directories (likemkdir -p);install -m 755copies and sets the mode in one step, unlikecp, which would keep whatever mode the file had.installdepends onall, somake installbuilds first. Many people runmakeas themselves andsudo make installafterwards; then the install step must not need to compile anything, or root-owned files end up in your build tree.- Write
$(DESTDIR)$(BINDIR)with no slash between them:BINDIRis absolute, andDESTDIRis empty for a normal install.
A gotcha: PREFIX at build time vs install time
Since PREFIX is compiled into this program, building with one prefix and installing with another produces a binary that looks in the wrong place:
mkdir -p hello && cd hello
printf '#include <stdio.h>\nint main(void) { printf("data in %%s\\n", DATADIR); return 0; }\n' > hello.c
cat > Makefile <<'MK'
PREFIX ?= /usr/local
hello: hello.c
$(CC) -DDATADIR='"$(PREFIX)/share/hello"' lt; -o $@
install: hello
install -D -m 755 hello $(DESTDIR)$(PREFIX)/bin/hello
MK
make -j1 # built for /usr/local
make -j1 install PREFIX=/opt/hello DESTDIR=$PWD/stage
stage/opt/hello/bin/hellocc -DDATADIR='"/usr/local/share/hello"' hello.c -o hello
install -D -m 755 hello /home/learner/hello/stage/opt/hello/bin/hello
data in /usr/local/share/hello
hello was up to date, so Make didn't rebuild it; Make doesn't know the compiled-in path depends on PREFIX. Pass the same PREFIX to make and make install (packaging tools do), or write the configuration into a generated header that depends on a stamp file containing the value. Ninja notices command-line changes like this automatically (The Build Log).
Release builds and source tarballs
A release configuration is usually just other flags in another build directory (Out-of-Tree Builds). A dist target produces the source tarball that people download; making it reproducible (same bytes from the same commit) needs fixed metadata. SOURCE_DATE_EPOCH is the standard variable for "the time of this release":
mkdir -p calc-1.0 && cd calc-1.0
printf 'int main(void) { return 0; }\n' > calc.c
printf 'calc 1.0\n' > README
cat > Makefile <<'MK'
NAME := calc
VERSION := 1.0
DIST := $(NAME)-$(VERSION)
DIST_FILES := Makefile README calc.c
.PHONY: dist
dist: $(DIST).tar.gz
$(DIST).tar.gz: $(DIST_FILES)
tar --sort=name --owner=0 --group=0 --numeric-owner \
--mtime=@$(SOURCE_DATE_EPOCH) --transform 's,^,$(DIST)/,' \
-cf - $^ | gzip -n > $@
MK
make -j1 dist | sed 's/ */ /g'
tar -tvzf calc-1.0.tar.gz | awk '{print $1, $2, $6}'
sha256sum calc-1.0.tar.gz > first.sum
rm calc-1.0.tar.gz && make -j1 dist >/dev/null
sha256sum -c first.sumtar --sort=name --owner=0 --group=0 --numeric-owner \
--mtime=@1767225600 --transform 's,^,calc-1.0/,' \
-cf - Makefile README calc.c | gzip -n > calc-1.0.tar.gz
-rw-r--r-- 0/0 calc-1.0/Makefile
-rw-r--r-- 0/0 calc-1.0/README
-rw-r--r-- 0/0 calc-1.0/calc.c
calc-1.0.tar.gz: OK
Sorted names, fixed owner and time, and gzip -n (no name or time stamp in the gzip header) make the second tarball byte-for-byte identical to the first. The Release Engineering course builds on this.
Test yourself
A packager runs make install DESTDIR=/tmp/root PREFIX=/usr. Where is the binary copied, and where does it think it lives?
- Copied to /usr/bin, thinks /tmp/root/usr/bin
- Copied to /tmp/root/usr/bin, thinks /usr/bin
- Both /tmp/root/bin
- Both /usr/bin
Why use install -m 755 instead of cp?
cpcan't copy binaries- It copies and sets the permissions in one step, and
install -dcreates directories - It strips debug info by default
- It's faster
You built with the default PREFIX and install with PREFIX=/opt/x. Why might the program look for data in the wrong place?
installrewrites paths- Make doesn't know the compiled-in path depends on PREFIX, so the binary wasn't rebuilt
- PREFIX is ignored by compilers
- DESTDIR overrides it