Compiling_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.
29.1 Build Environment Setup
Section titled “29.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 libjpeg29.2 Build Process
Section titled “29.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 ldconfig29.3 CMake Build System
Section titled “29.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 ../..make29.4 Autotools (configure/make)
Section titled “29.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 distclean29.5 Meson/Ninja Build
Section titled “29.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 install29.6 Managing Multiple Versions
Section titled “29.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 install29.7 Build Troubleshooting
Section titled “29.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=129.8 Creating Packages
Section titled “29.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/nginxSummary
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