Practice Use drills for recall and labs for real operating judgment.

LAB-PKG-04 - Compiling from Source

Demystify the classic 'configure, make, make install' workflow to understand how Linux software is built from human-readable human source code into machine-executable binaries.

PKG Package & Software Management

Compiling from Source

Demystify the classic 'configure, make, make install' workflow to understand how Linux software is built from human-readable human source code into machine-executable binaries.

40 min ADVANCED LINUX Curriculum-reviewed
Success criteria
  • Demystify the classic 'configure, make, make install' workflow to understand how Linux software is built from human-readable human source code into machine-executable binaries.
  • Repeat the workflow without copy-paste or step-by-step prompting.
Safety notes
  • Review package names and commands before installation or removal to avoid changing the wrong system state.

Part A: The Field Guide


🎯 What & Why

What happens when you need a program that isn’t in apt or dnf? What if it’s a cutting-edge beta released today on GitHub?

You will have to Compile from Source.

Linux programs are written by humans in languages like C or C++. A computer CPU cannot read C code. A Compiler (like gcc) translates the human text files into 1s and 0s (a Binary executable) that the Linux kernel can actually run.

apt install htop downloads a pre-compiled binary. Compiling from source means downloading the raw text code and running the translation engine on your own CPU locally.


🧠 Mental Model: The Auto Factory

Compiling is like building a car.

  1. The Blueprint (./configure): You walk into the factory and hand the manager your blueprint. The manager runs a checklist. “Do we have enough steel? Do we have rubber for tires? Do we have a robotic welding arm?” If your Linux machine is missing a required library, ./configure throws an error and halts the assembly line.
  2. The Assembly (make): The checklist passed. You yell “Make!” The robots spark to life, melt the steel, and bolt the engine together. This creates the final car (the executable binary) sitting on the factory floor.
  3. The Showroom (make install): The car is built, but it’s stuck in the factory. You tell the delivery truck to drive it to the showroom (/usr/local/bin) so the public (your user account) can actually drive it.

📖 Command Reference

The Holy Trinity

For 30 years, 90% of Linux software has been compiled using these exact three steps.

The Build Process

$ # STEP 1: The Blueprint (Check dependencies and prepare Makefile) $ ./configure

$ # STEP 2: The Assembly (Compile the C code into binaries) $ make

$ # STEP 3: The Showroom (Move the binary into the system path) $ sudo make install

Preparing the Factory

A fresh Linux server does not come with a compiler installed! You must install the “Build Tools” metapackage first.

Installing the Compiler

$ # On Ubuntu/Debian: Installs gcc, make, and core libraries $ sudo apt update && sudo apt install build-essential

$ # On RHEL/Alma/Fedora: Installs the exact same toolset $ sudo dnf groupinstall “Development Tools”


🌍 Real Scenarios

Scenario 1: Fixing a “Configure” Error You download the source code for a custom Web Server. You run ./configure. It spits out 50 lines of green text, but then halts with a red error: configure: error: OpenSSL development headers not found. The factory is missing steel! To fix this, you must use your package manager to install the development package for OpenSSL. (Ubuntu: sudo apt install libssl-dev. RHEL: sudo dnf install openssl-devel). Once installed, you run ./configure again, and it passes!

Scenario 2: Utilizing Multi-Core CPUs Compiling the Linux Kernel itself can take 2 hours on a single CPU core. But you have an 8-core server! You can tell make to use parallel assembly lines by passing the “Jobs” flag -j. make -j 8 This forces make to use 8 CPU cores simultaneously, reducing the compile time from 2 hours to 15 minutes.


⚠️ Gotchas & Pitfalls

  1. Where does it install? apt installs binaries to /usr/bin/. make install almost always installs binaries to /usr/local/bin/. This separation is intentional so manually compiled software doesn’t overwrite your package manager’s software!
  2. How do I uninstall it? This is the nightmare of compiling. apt remove handles cleanly deleting software. But if you make install, the system has no official record of it. To uninstall, you must keep the original source code folder where you compiled it! You cd back into that folder and run sudo make uninstall (assuming the developer actually built an uninstall script in the Makefile).

Part B: The Drill Deck

Terminal Required: Open a Linux terminal to practice compiling a massive 2-line C program. Keep this confined to a lab directory.


G
Guided Step by step - type exactly this and compare the result
>

Exercise G1: Building the Factory

Before we compile, we need the robotic arms (the gcc compiler program).

  1. Ubuntu/Debian learners: sudo apt update && sudo apt install build-essential -y
  2. RHEL/CentOS learners: sudo dnf groupinstall "Development Tools" -y
  3. Verify the factory exists: gcc --version. You should see version details.

Exercise G2: Writing Source Code

We aren’t going to download millions of lines of code. We will write 5 lines of C code ourselves.

  1. Create a workspace: mkdir ~/compile_lab && cd ~/compile_lab
  2. Create the raw C source code file. Paste this entire multi-line block directly into your terminal:
cat << 'EOF' > hello.c
#include <stdio.h>
int main() {'{'}
    printf("I compiled this myself!\\n");
    return 0;
{'}'}
EOF
  1. Run ls. You have a text file named hello.c.
  2. Try to run it: ./hello.c. It fails, producing a “Permission denied” or “Command not found” error because it’s just text.

Exercise G3: The One-Step Assembly

Because our program is so tiny, we don’t need ./configure or make. We can just feed it directly to the gcc compiler engine!

  1. Tell gcc to read hello.c, compile it into binary, and Output (-o) the binary file as my_first_app. gcc hello.c -o my_first_app
  2. Run ls. You now have a new file named my_first_app. Notice it is highlighted green in your terminal, marking it as an executable binary!
  3. Run your custom software: ./my_first_app
S
Solo Task described, hints available - figure it out
>

Exercise S1: The Object File Bloat

When you compile large programs, the compiler generates temporary “Object” files (ending in .o or .out depending on the system). If you don’t use the -o flag to name your output, the compiler spits out a default binary.

  1. Delete your beautiful app: rm my_first_app
  2. Run the compiler completely “naked” without the -o flag: gcc hello.c
  3. Run ls. Look at the file gcc created. It defaulted to the classic C-compiler output name a.out.
  4. Run it to verify it works: ./a.out

Exercise S2: Source Code vs Binary Size

Humans read words. Computers read zeros and ones. Let’s look at the size difference.

  1. Check the file size of the human text code: ls -lh hello.c (It should be around 80 bytes).
  2. Check the file size of the compiled binary: ls -lh a.out
  3. Notice the binary is massively larger (~16 Kilobytes). The compiler packed in thousands of bytes of kernel-level instructions and memory-allocation logic just to allow the program to print text to your screen!
M
Mission Real scenario - no hints, combine multiple skills
>

Mission M1: The Showroom Installation

You have compiled a.out (or my_first_app). Right now, it only exists in your ~/compile_lab directory. If you switch to /var/log and type a.out, the system will say “command not found”.

We want this software to be a globally recognized command, just like ls or cd.

Your Mission: Move your compiled binary into the global binary “showroom” folder.

  1. Rename a.out to greet_me.
  2. Move it to the /usr/local/bin/ directory. (You will need sudo because regular users cannot modify global system folders).
  3. Give the binary execute permissions if it lost them during the move (using chmod +x).
  4. Change directories to /tmp/ (to prove you are no longer in your lab).
  5. Type greet_me and press Enter. It should instantly execute your custom C code from anywhere on the system!