Why build from source
A package manager is the preferred way to install software, because it tracks files, versions, and dependencies. Building from source is what you do when no package exists, when you need a patched version, or when you want a build with particular options. You take on the work the package maintainer normally does: finding the dependencies, choosing the options, and placing the files. The trade-off is that the package manager no longer knows about the files you install, so you must keep track of them yourself.
The three steps
Most projects that use the GNU build system follow the same three steps. The first is ./configure, a script generated by autoconf. It inspects the compiler, the libraries, and the headers on the machine, and it writes a file called Makefile that records what it found. The second is make, which reads the Makefile and runs the compiler on each source file in the right order. The third is make install, which copies the built programs, libraries, and documentation to the install location.
A common shape for the whole process is shown below. It is a sketch for a real project, not a command to run on this machine, which has no compiler and no source tree to build.
./configure --prefix="$HOME/.local"
make
make installThe --prefix option sets the top of the install tree. With a prefix inside your home directory, the files go into $HOME/.local/bin and the other standard subdirectories, and you need no administrative rights. Without a prefix, the default is usually /usr/local, which requires root.
Reading configure output
Configure prints a long list of checks, such as whether a C compiler works, whether a header exists, and which version of a library it found. The last lines are the most useful. A failed check that the project requires ends the run with an error message that names the missing item. The fix is almost always to install the named development package and run configure again.
To see the options a project accepts, read its help text rather than its documentation. The option list is generated from the same script that performs the checks, so it is the authoritative list for that version.
./configure --help | head -n 20Make and parallel builds
The make program decides what to rebuild by comparing the time stamps of the source files with the time stamps of the outputs. A file is rebuilt only when its source is newer than the object file that came from it. This is why a second run of make after a small change is fast. The -j option runs several compile steps at once, which speeds up large builds on machines with several cores. The number after -j is the count of jobs, and -j alone asks for as many as the make program allows.
A dry run prints the commands that make would run without running them. The -n option is the safe way to inspect a build you did not write.
make -n | head -n 5Install, uninstall, and tracking
The install step copies files, and it does not record them anywhere that the package manager can read. If you want to remove the software later, you must either run an uninstall target that the project provides, or keep a list of the files it installed. A simple habit is to install into a dedicated prefix directory, so that removing the software means removing that one directory. Another useful habit is to keep the build directory around until the install is verified.
The DESTDIR variable makes make install copy into a staging directory instead of the real prefix. Package builders use it so that they can capture the exact files before they move them into place.
Do not run make install as root for software you have not read. The install step writes to system directories, and a bad install script can overwrite files that the package manager owns.
Source builds also depend on the environment in ways that a package hides from you. The configure script records the compiler flags, the library paths, and the optional features it found, and it stores them in the generated files. If you change the machine, such as by installing a new library, run configure again so the recipe matches the machine. A stale Makefile is a common cause of a build that succeeds but produces a program that fails at run time.
Before a build, it is worth reading the README and the install notes that ship with the source. Many projects list the required libraries and the minimum versions, and those notes are usually more accurate than the error messages from a failed configure run. Keep the source tarball and its checksum next to the build, so that you can prove later which source produced which program.
This lab machine
The lab does not have make, a C compiler, or a source tree, so you cannot run the build in it. The lab does have the tools that the build steps rely on, such as the shell, the file utilities, and the library inspection tools that the next unit covers. The hands-on part of this unit asks you to write the build plan as a file and to check whether make is installed, so you can see the answer for yourself.
Hands-on: write a build plan
Write the three build commands into a plan file, in the order they must run. Then test whether make exists on this machine and save the answer.
printf "%s\n" "./configure --prefix=$HOME/.local" make "make install" > plan.txt
if command -v make >/dev/null 2>&1; then echo yes; else echo no; fi > make.txtThe file plan.txt lists the three steps in order, and make.txt holds yes or no. The answer for this lab is no, which is the reason you cannot run the build here.