Compiling from Source
Chapter 30: Compiling from Source
Section titled “Chapter 30: Compiling from Source”Overview
Section titled “Overview”This chapter covers compiling software from source code, which is essential for custom builds, security, and understanding how software works.
30.1 Build Environment Setup
Section titled “30.1 Build Environment Setup”Installing Build Tools
Section titled “Installing Build Tools”# Arch Linuxsudo pacman -S --needed base-devel
# Debian/Ubuntusudo apt install build-essential
# RHEL/CentOSsudo dnf groupinstall "Development Tools"
# Verify installationgcc --versionmake --versionAdditional Dependencies
Section titled “Additional Dependencies”# Arch Linux - common build dependenciessudo pacman -S \ cmake \ autoconf \ automake \ pkg-config \ libtool \ flex \ bison \ gettext \ ninja \ meson
# Install development libraries as neededsudo pacman -S openssl dev zlib libpng libjpeg30.2 Build Process
Section titled “30.2 Build Process”Standard Build Steps
Section titled “Standard Build Steps” Source Compilation Process+------------------------------------------------------------------+| || 1. Download Source || └─ wget/curl/git clone || || 2. Extract || └─ tar -xvf source.tar.gz || || 3. Configure || └─ ./configure --prefix=/usr/local || - Checks system capabilities || - Generates Makefile || || 4. Build || └─ make || - Compiles source into binaries || || 5. Install || └─ sudo make install || - Copies files to system || || 6. ldconfig (if needed) || └─ Updates library cache || |+------------------------------------------------------------------+Example: Building nginx from Source
Section titled “Example: Building nginx from Source”# Install dependenciessudo pacman -S openssl pcre zlib
# Download sourcewget https://nginx.org/download/nginx-1.24.0.tar.gztar -xvzf nginx-1.24.0.tar.gzcd nginx-1.24.0
# Configure with options./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_gzip_static_module \ --with-pcre \ --with-openssl=/usr/include/openssl
# Buildmake -j$(nproc)
# Installsudo make install
# Update library cachesudo ldconfig30.3 CMake Build System
Section titled “30.3 CMake Build System”CMake Basics
Section titled “CMake Basics”# Basic CMake workflowmkdir buildcd buildcmake ..makesudo make installCMake Examples
Section titled “CMake Examples”# Configure with optionscmake -DCMAKE_INSTALL_PREFIX=/usr/local \ -DCMAKE_BUILD_TYPE=Release \ -DENABLE_FEATURE=ON \ ..
# Out-of-source build (recommended)mkdir -p build/releasecd build/releasecmake -DCMAKE_BUILD_TYPE=Release ../..make30.4 Autotools (configure/make)
Section titled “30.4 Autotools (configure/make)”configure Options
Section titled “configure Options”# Common configure options./configure \ --prefix=/usr/local # Installation prefix --sysconfdir=/etc # Config files location --localstatedir=/var # Variable data --enable-shared # Build shared libraries --enable-static # Build static libraries --disable-debug # Disable debug symbols --with-package=option # Package-specific options --without-package # Disable package --help # Show all optionsmake Options
Section titled “make Options”# Build with multiple coresmake -j$(nproc)
# Build specific targetmake nginx
# Install to staging directorymake DESTDIR=/tmp/staging install
# Clean buildmake cleanmake distclean30.5 Meson/Ninja Build
Section titled “30.5 Meson/Ninja Build”Modern Build System
Section titled “Modern Build System”# Installsudo pacman -S meson ninja
# Configuremeson setup builddir
# Buildninja -C builddir
# Installsudo ninja -C builddir install30.6 Managing Multiple Versions
Section titled “30.6 Managing Multiple Versions”Using /usr/local
Section titled “Using /usr/local”# Install to /usr/local./configure --prefix=/usr/localsudo make install
# Update library cachesudo ldconfig
# Find installed binarieswhich nginxldd $(which nginx)Using checkinstall
Section titled “Using checkinstall”# Install checkinstallsudo pacman -S checkinstall
# Create package instead of make installsudo checkinstall --pkgname=nginx-custom --pkgversion=1.24.0 make install30.7 Build Troubleshooting
Section titled “30.7 Build Troubleshooting”Common Issues
Section titled “Common Issues”# Missing dependenciesconfigure: error: *** missing ***# Solution: Install required -dev packages
# Library not found# Solution: Set LDFLAGSexport LDFLAGS="-L/usr/local/lib"export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"
# Header not found# Solution: Set CPPFLAGSexport CPPFLAGS="-I/usr/local/include"
# Compiler not found# Solution: Set CCexport CC=gcc-13export CXX=g++-13Build Logs
Section titled “Build Logs”# Save configure output./configure > configure.log 2>&1
# Save build outputmake > build.log 2>&1
# Verbose buildmake V=130.8 Creating Packages
Section titled “30.8 Creating Packages”Arch Linux PKGBUILD
Section titled “Arch Linux PKGBUILD”# Example PKGBUILDpkgname=nginx-custompkgver=1.24.0pkgrel=1arch=('x86_64')depends=('openssl' 'pcre' 'zlib')
build() { cd $pkgname-$pkgver ./configure --prefix=/usr/local make}
package() { cd $pkgname-$pkgver make DESTDIR=$pkgdir install}RPM Spec File
Section titled “RPM Spec File”# Example nginx.specName: nginx-customVersion: 1.24.0Release: 1%{?dist}Summary: Custom nginx build
%build./configure --prefix=/usr/localmake %{?_smp_mflags}
%installmake install DESTDIR=%{buildroot}
%files%{_prefix}/local/nginxWhy This Matters in DevOps/SRE
Section titled “Why This Matters in DevOps/SRE”Compiling from source is essential for custom builds and security:
Source Compilation in DevOps+------------------------------------------------------------------+| || Custom Software: || +----------------------------------------------------------+ || | Custom nginx/Python with specific modules | || | Hardware-specific optimizations | || | Older versions not in package repos | || +----------------------------------------------------------+ || || Security: || +----------------------------------------------------------+ || | Patch vulnerabilities in custom builds | || | Compile with security flags (-D_FORTIFY_SOURCE) | || | Reproducible builds for verification | || +----------------------------------------------------------+ || || DevOps Tools: || +----------------------------------------------------------+ || | Packer → Build custom AMIs with compiled software | || | Docker → Multi-stage builds with compiled binaries | || | GitHub Actions → Compile in CI pipelines | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+Practical Impact:
- Custom software builds for specific requirements
- Security patching of outdated packages
- Optimized builds for specific hardware
Common Mistakes & Anti-Patterns
Section titled “Common Mistakes & Anti-Patterns”1. Not Checking Dependencies
Section titled “1. Not Checking Dependencies”# ❌ WRONG: Trying to build without dependencies./configure# Fails with "configure: error: ... not found"
# ✅ CORRECT: Install build dependencies firstsudo apt-get install build-essential libssl-dev# Or check README for required packages2. Not Using DESTDIR
Section titled “2. Not Using DESTDIR”# ❌ WRONG: Installing directly to systemmake install# Pollutes /usr/local, hard to remove
# ✅ CORRECT: Use DESTDIR for stagingmake install DESTDIR=/tmp/staging# Package the staging directory instead3. Not Documenting Build Steps
Section titled “3. Not Documenting Build Steps”# ❌ WRONG: No record of how software was built# Impossible to reproduce or update
# ✅ CORRECT: Document everything# save configure options, patches, build steps# Use Ansible/Chef to automate buildsInterview Questions
Section titled “Interview Questions”- What is the typical build process for autotools?
- How do you create an RPM from source?
- What are the advantages of CMake over autotools?
- How do you troubleshoot configure errors?
- What is DESTDIR and why is it important?
Summary
Section titled “Summary”In this chapter, you learned:
- ✅ Setting up build environment
- ✅ Autotools build process (configure/make)
- ✅ CMake build system
- ✅ Meson/Ninja builds
- ✅ Troubleshooting build issues
- ✅ Creating distribution packages
Next Chapter
Section titled “Next Chapter”Chapter 30: Repository Management
Last Updated: February 2026