Skip to content
Tutorials Center
My learning

Topics

Lesson 91 of 95 ยท System Administration

Compiling Software from Source and Building Packages

Build Linux software from source with gcc, make and the configure/make/make install routine, understand shared libraries with ldd, and package your program as a .deb or .rpm.

Most of the time you install software with a package manager. But sometimes the package does not exist, is too old, or you need a custom build option, a patch, or your own program installed on several servers. Then you build from source code: you download the sources, turn them into executables with a compiler, and install the result. Understanding this process also explains a lot about how Linux software fits together: headers, libraries, linking, install prefixes.

This lesson walks through the classic routine (./configure, make, make install), shows what gcc does in each stage, explains shared libraries and ldd, and ends with turning your build into a proper .deb package (and what an .rpm needs). The sandbox has gcc, make, ldd and dpkg-deb, so most of this runs for real; only steps that need root or the network are read-only. It builds on Packages, Services and systemd, Archives and Permissions.

The big picture

flowchart LR
  T["Source tarball<br/>(tar.gz)"] --> X["tar xf"]
  X --> C["./configure<br/>checks your system"]
  C --> M["make<br/>compile + link"]
  M --> I["make install<br/>copy to PREFIX"]
  I --> R["Program in<br/>/usr/local/bin"]

A typical build: unpack, configure for this machine, compile, then install into a prefix.

First install the toolchain. On Debian and Ubuntu one meta-package covers it, and on Fedora/RHEL it is a group:

CONSOLE
$ sudo apt install build-essential pkg-config        # gcc, g++, make, libc headers
$ sudo dnf groupinstall "Development Tools"          # Fedora / RHEL

A typical session for a program that uses GNU Autotools (a configure script):

CONSOLE
$ wget https://example.org/downloads/tool-2.4.tar.gz
$ sha256sum tool-2.4.tar.gz                  # compare with the published checksum!
$ tar xf tool-2.4.tar.gz && cd tool-2.4
$ less README INSTALL                        # read them: options differ per project
$ ./configure --prefix=/usr/local            # check dependencies, write Makefiles
$ make -j"$(nproc)"                          # build using all CPU cores
$ make check                                 # run the test suite, if there is one
$ sudo make install                          # copy files into place

Always verify the checksum or signature of what you download, and read the project's INSTALL file: some projects use cmake (cmake -S . -B build && cmake --build build) or meson instead of configure, but the flow is the same: configure, build, install.

If ./configure stops with "C compiler cannot create executables" you lack the toolchain; with "Package foo was not found" or "foo.h: No such file or directory" you lack a development package, normally named libfoo-dev (Debian) or foo-devel (RHEL). The runtime library alone (libfoo) has no headers.

What gcc does

Compiling a C file is four steps, and gcc runs all of them unless you stop it early:

Step Option Result What happens
Preprocess -E Expanded source #include and #define are processed
Compile -S Assembly (.s) C becomes assembly instructions
Assemble -c Object file (.o) Machine code, not yet runnable
Link (none) Executable Object files and libraries are joined
Bash The four stages of compiling

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Useful flags: -Wall -Wextra (warnings you should read), -O2 (optimise), -g (debug symbols), -I dir (where to look for headers), -L dir and -l name (where to look for libraries, and which one: -lm links libm), -std=c17 (language standard). The C++ compiler is g++.

Shared libraries and ldd

Few programs are self-contained. Most are dynamically linked: the executable contains a list of libraries (libc.so.6, libm.so.6, ...) that the dynamic loader finds at start-up. This keeps programs small and lets a security fix in one library help every program that uses it.

ldd lists the libraries a program needs and where they were found. not found next to a name is the classic reason a program refuses to start with "error while loading shared libraries":

CONSOLE
$ ldd /usr/local/bin/tool
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6
        libmytool.so.2 => not found

The loader searches the paths cached by ldconfig (configured in /etc/ld.so.conf and /etc/ld.so.conf.d/*.conf), plus any directory in the environment variable LD_LIBRARY_PATH. After installing a library into /usr/local/lib, run sudo ldconfig to refresh the cache. Never use ldd on a program you do not trust: on some systems it can execute it.

Bash Build a shared library and watch the loader find it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Setting LD_LIBRARY_PATH is fine for tests. For a permanent install, put the library in a directory the loader already knows (/usr/local/lib plus ldconfig), or embed the location with -Wl,-rpath,/opt/tool/lib at link time.

Makefiles and the install prefix

make reads a Makefile made of rules: target: dependencies, followed by a command line that starts with a tab character (not spaces). It compares file modification times and rebuilds only targets whose dependencies are newer. That is why a second make finishes instantly and why editing one file recompiles only what depends on it.

Good Makefiles also respect two conventions that package builders rely on:

  • PREFIX (or --prefix= for configure): where the program will live when installed. Defaults to /usr/local for software you build yourself, so it cannot clash with files owned by your package manager (/usr).
  • DESTDIR: a staging root prepended to every install path, so make install DESTDIR=/tmp/stage copies the files to /tmp/stage/usr/local/... without touching the real system. Packaging tools use it.
Bash A small project with make, PREFIX and DESTDIR

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

make -n ("dry run") prints the commands without running them; always use it when a Makefile is unfamiliar. Some projects also offer make uninstall, but many do not, which is a strong reason to install self-built software under its own prefix (/opt/tool-2.4) or, better, in a package.

Avoid sudo make install into /usr: it mixes files into locations owned by the package manager, which then cannot upgrade or remove them cleanly. Use /usr/local, /opt/NAME or your home directory (--prefix=$HOME/.local). Never run make itself as root; only the final install step needs it.

Packaging: .deb and .rpm

A package is an archive of files plus metadata (name, version, dependencies, install scripts) that the package manager can install, verify, upgrade and remove. Building one for your own program means every server gets it with apt install ./myprog.deb and removal is clean.

A minimal .deb is a directory tree that mirrors the target filesystem, plus DEBIAN/control. You can build one with dpkg-deb, and check it with dpkg-deb -I (info) and -c (contents).

Bash Build a tiny .deb package

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Installing it on a real system is sudo apt install ./hello-pkg_1.0-1_all.deb (read-only here, as it needs root), and dpkg -L hello-pkg lists its files afterwards. Serious Debian packaging uses the debhelper toolchain and a debian/ directory with rules, changelog and control so packages can be built reproducibly in clean environments; dpkg-buildpackage and sbuild automate that.

On the Red Hat side the equivalent is an RPM, built with rpmbuild from a spec file:

TEXT
Name:     hello-pkg
Version:  1.0
Release:  1%{?dist}
Summary:  A tiny demonstration package
License:  MIT
Source0:  %{name}-%{version}.tar.gz

%description
A tiny demonstration package.

%prep
%setup -q

%build
make %{?_smp_mflags}

%install
make install DESTDIR=%{buildroot} PREFIX=/usr

%files
/usr/bin/hello

%changelog
* Mon Mar 02 2026 Ada Example <[email protected]> - 1.0-1
- First package

The spec file's sections mirror the manual build: %prep unpacks, %build compiles, %install stages the files with DESTDIR, and %files lists what belongs to the package. Because both formats lean on DESTDIR, the Makefile habits above carry straight over. For a quick way to wrap an already-installed or staged tree without learning either system, fpm (a Ruby tool) and checkinstall exist, though a hand-written package is easier to trust.

Troubleshooting builds

Symptom Likely cause and fix
foo.h: No such file or directory Install the -dev/-devel package for foo, or pass -I
cannot find -lfoo Library or its dev symlink missing; install it or use -L
configure: error: ... not found Missing dependency; pkg-config --list-all shows what is known
error while loading shared libraries at run time ldd the program, run ldconfig, or set LD_LIBRARY_PATH/rpath
make: *** missing separator A recipe line starts with spaces instead of a tab
Works as you, fails after sudo make install Check ownership and permissions of the installed files

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the correct order of the classic source-install routine?

    configure inspects the system and writes the Makefiles, make compiles, and make install copies the results into the prefix.
  2. 2. What does make install DESTDIR=/tmp/stage do?

    DESTDIR is prepended to every install path. Package builders use it to collect the files that go into a package.
  3. 3. A program fails with 'error while loading shared libraries: libfoo.so.2'. What is the best first step?

    ldd lists each needed library and where it was found. Then install the missing package, run ldconfig, or set the library path.
  4. 4. Why is /usr/local (or /opt/NAME) preferred over /usr for software you built yourself?

    Files in /usr belong to packages. Mixing in your own build can be overwritten or leave orphan files on upgrade.
  5. 5. Which gcc option stops after producing an object file (.o) without linking?

    -c compiles and assembles but does not link. -E stops after preprocessing, -S after producing assembly, and -o only names the output.

Summary

  • Build from source when no suitable package exists: verify the download, then ./configure --prefix=..., make -j"$(nproc)", make check, make install. Some projects use cmake or meson with the same flow.
  • gcc compiles in four stages (preprocess -E, compile -S, assemble -c, link); -Wall -Wextra -O2 -g, -I, -L and -l are the everyday flags.
  • Missing headers mean a missing -dev/-devel package. At run time, ldd shows not found libraries; fix with ldconfig, LD_LIBRARY_PATH or an rpath.
  • make rebuilds only what changed; recipe lines need tabs. Respect PREFIX and DESTDIR, and install under /usr/local or /opt instead of /usr.
  • A .deb is a staged file tree plus DEBIAN/control built with dpkg-deb; an RPM is built from a spec file with %prep, %build, %install and %files.
  • Installing a package beats make install because the package manager can verify, upgrade and remove it.