Guides 11792 Published by

The article explains how to compile NGINX from source on Ubuntu 20.04 or 22.04, beginning by installing the essential build tools and libraries such as GCC, PCRE, zlib, and OpenSSL. It walks through downloading the desired tarball, selecting modules during configuration—like adding geoip support—and setting a custom installation prefix to keep system defaults untouched. The guide then covers building with make, installing the binary, creating a convenient symlink, configuring a minimal nginx.conf, testing that configuration, and finally enabling or starting the service. Finally, it lists common pitfalls such as missing dependencies, incorrect prefixes, and the need to reload after changes, while encouraging readers that recompiling provides fine control over modules and versions without being overly complex on recent Ubuntu releases.



Build NGINX From Source on Ubuntu 22.04 (or 20.04) – No Pre‑Built Packages Needed

If you’re chasing a specific module, need the latest TLS support, or just want to see how the guts work, compiling NGINX from source on Ubuntu is faster than hunting for a matching binary in apt’s repositories.

Prerequisites: Why You’ll Need These Tools

You’ll have to get your build environment ready. The compiler, make tool, and a few libraries are mandatory; without them the ./configure script will bail out before even touching the source code.

sudo apt update
sudo apt install -y \
    build-essential \
    libpcre3-dev \
    zlib1g-dev \
    libssl-dev

Why it matters:

  • build‑essential pulls in GCC, G++, and make.
  • The PCRE library lets NGINX do regex‑heavy routing.
  • Zlib powers the gzip module.
  • OpenSSL gives you HTTPS.

I’ve seen systems where people omitted zlib1g-dev, then got a cryptic “module ngx_http_gzip_module not found” error when compiling. Don’t let that happen to you.

Download the Source: Get the Right Version

Grab the tarball from the official site or use wget. Pick the version you want—maybe the latest stable 1.26.x, or a specific patch release.

cd ~/Downloads
wget https://nginx.org/download/nginx-1.26.0.tar.gz
tar xf nginx-1.26.0.tar.gz

If you need a development snapshot, switch to https://github.com/nginx/nginx/archive/refs/tags/release-1.26.x.zip instead.

Configure With Custom Modules

Before building, decide which modules you want. The default configuration includes the essentials, but if you plan on adding ngx_http_geoip_module, for instance, point the config to its source directory:

cd nginx-1.26.0
./configure \
    --prefix=/opt/nginx \
    --with-http_ssl_module \
    --add-module=../nginx-mod-http-geoip

Why it matters:

The --prefix tells NGINX where to install itself—keeping the default /etc/nginx/ untouched.

Adding modules at compile time ensures they’re compiled into the binary rather than loaded as shared objects later, which can save a handful of bytes and avoid runtime load errors.

Compile and Install

With configuration settled, let GCC do its thing. The -j$(nproc) flag tells make to use all available CPU cores, shaving a few minutes off large builds.

make -j$(nproc)
sudo make install

If the compile stalls or you see “error: undefined reference” messages, double‑check that all required libraries are present. Missing libssl headers often cause those headaches.

Post‑Build Tweaks

Create a symlink so that /usr/sbin/nginx points to your new binary—this keeps the usual commands working without rewriting scripts:

sudo ln -s /opt/nginx/sbin/nginx /usr/sbin/nginx

Set up a minimal nginx.conf in /opt/nginx/conf/ or copy the one from /etc/nginx/ and adjust paths.

sudo cp /etc/nginx/nginx.conf /opt/nginx/conf/
# Edit the file to point worker_processes and logs to your new prefix if needed

Finally, test the config:

nginx -t

If it passes, start NGINX:

sudo systemctl enable --now nginx

Or simply run:

sudo /opt/nginx/sbin/nginx
Common Pitfalls to Avoid

1. Missing libpcre3-dev – you’ll hit a “no PCRE support” error.
2. Wrong prefix – if you forget it, the binary installs into /usr/local, and your system’s default /etc/nginx/ remains untouched, which can be confusing when debugging.
3. Forgetting to reload after config changes – NGINX doesn’t auto‑reload; use nginx -s reload.
I’ve also run into cases where a recent openssl update broke the default binary because it was compiled against an older library. Recompiling solves that instantly.

Final Thoughts

Compiling NGINX gives you fine control over modules and versions, and if you’re comfortable with build tools, it’s pretty painless on Ubuntu 20.04 or 22.04. Just remember to keep your dependencies tidy and test the config before trusting the server in production.

Happy hacking—keep those requests humming!