Install Latest Version of Nginx on Linux (Debian/Ubuntu)
The Nginx installed by the 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 to stay on the bleeding edge is never the goal of the Debian/Ubuntu release schedule, there are still some ways to obtain the latest version of Ngnix.

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
Import Nginx Official Key
Import the signing key from Nginx officials:
$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
Verify the key exists and contains the full fingerprint 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62:
$ gpg --dry-run --quiet --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
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.listdeb [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.listdeb [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.listdeb [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.listdeb [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
Package: *
Pin: origin nginx.org
Pin: release o=nginx
Pin-Priority: 900
Output Installation
Update the package lists and install Nginx from its official repository:
$ sudo apt update
$ sudo apt install nginx
Check version:
$ nginx -v
nginx version: nginx/1.20.2
Output