Install Latest Version of Nginx on Linux (Debian/Ubuntu)

mail
Cyrus Kao
Last modified

The Nginx installed from package manager (apt) is usually pretty outdated. Currently v1.20.2 is the stable version of Nginx and Ubuntu LTS 20.04 (Focal) comes with version v1.18.0. Although bleeding edge is never the goal of Debian/Ubuntu release schedule, there is still some ways to obtain the latest version of Ngnix.

Nginx
Nginx from Ubuntu Focal

In this guide we'll be using apt to install Nginx from the package maintenance by Nginx core team on Debian/Ubuntu.

Prerequisites

Install the required tools and dependencies:

sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
Bash

Import Nginx Official Key

Import signing key from Nginx official:

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
Bash

Verify the key exist and contain the full fingerprint 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62:

gpg --dry-run --quiet --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
Bash
pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
      573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid                      nginx signing key <signing-key@nginx.com>
Output

Set Up Repository

Add the official repository to your apt package lists:

  • Debian

    • Stable

      echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
      http://nginx.org/packages/debian `lsb_release -cs` nginx" \
      		| sudo tee /etc/apt/sources.list.d/nginx.list
      Bash
      deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian focal nginx .
      Output
    • Mainline (newer and more unstable)

      echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
      http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
      		| sudo tee /etc/apt/sources.list.d/nginx.list
      Bash
      deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/mainline/debian focal nginx .
      Output
  • Ubuntu

    • Stable

      echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
      http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
      		| sudo tee /etc/apt/sources.list.d/nginx.list
      Bash
      deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu focal nginx .
      Output
    • Mainline (newer and more unstable)

      echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
      http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \
      		| sudo tee /etc/apt/sources.list.d/nginx.list
      Bash
      deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/mainline/ubuntu focal nginx .
      Output

Tell apt to install from nginx.org instead of upstream from Debian/Ubuntu:

echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
    | sudo tee /etc/apt/preferences.d/99nginx
Bash
Package: *
Pin: origin nginx.org
Pin: release o=nginx
Pin-Priority: 900
Output

Installation

Update package lists and install Nginx from its official repository:

sudo apt update
sudo apt install nginx
Bash

Check version:

nginx -v
Bash
nginx version: nginx/1.20.2
Output

See Also

Comments

0500