How to Install Nginx Mainline on Ubuntu 22.04 | 20.04

For Ubuntu users, the default repository for Nginx might not install the latest version. However, for most users, this is satisfactory. Meanwhile, many users who seek performance and to keep up with the latest security, bug, and performance would look at installing the Nginx mainline. Nginx even recommends installing the mainline over the stable version. The main difference between stable and mainline is that stable has been tested while mainline has newer features and less testing. Depending on your needs, one or the other might be a better choice for you. If you need the latest and greatest, go with the mainline. If you need a more stable release that’s been thoroughly tested, go with the stable version.

As discussed in the introduction, the following tutorial will cover installing the mainline version of Nginx on Ubuntu 22.04 or 20.04 LTS with importing the official repository from Nginx, which is the most recommended option that provides the latest mainline or for users preferring to stay on stable the newest version of this branch as well.

Recommended Steps Before Installation

Before proceeding with the tutorial, ensuring your system is up-to-date with all existing packages is good.

sudo apt update

Optionally, you can list the updates for users who require review or are curious.

sudo apt --list upgradable

Proceed to upgrade any outdated packages using the following command.

sudo apt upgrade

Remove Previous Nginx Installation on Ubuntu Linux

First, to avoid conflict, you need to remove any previous Nginx installations before installing the new versions.

First, back up your Nginx configuration for safekeeping.

sudo mv /etc/nginx/ /etc/nginx.old/

Stop Nginx using the systemctl command as follows.

sudo systemctl stop nginx

Next, remove Nginx using the following command:

sudo apt autoremove nginx*

Install Nginx Mainline or Stable

The tutorial is for installing Nginx mainline, but since it is straightforward to import either mainline or stable, the tutorial will show both options, which will give you the latest up-to-date version of Nginx without waiting for the maintainers of Ubuntu or any other PPA you may be used to compile and push it to their repositories.

First, open your terminal (CTRL+ALT+T) for desktop users; server users would already be in the terminal and run the following installation command for the following packages.

sudo apt install curl gnupg2 ca-certificates lsb-release dirmngr software-properties-common apt-transport-https -y

Download and add the Nginx GPG key to verify the authenticity of the packages.

curl -fSsL | sudo gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

Verify the GPG key by using the following command.

gpg --dry-run --quiet --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

Example output if successful:

pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
uid                      nginx signing key <>

Next, use the following to add either the stable or mainline Nginx repository to your apt package manager list.

Import Nginx Mainline Repository:

echo "deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

Import Nginx Stable Repository:

echo "deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

Ideally, you should set APT pinning to prefer Nginx packages over any default Ubuntu repositories or PPAs. This can be done by using the following command.

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

Example output:

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

Once done, update the apt repositories to reflect the new additions.

sudo apt update

Now proceed to install Nginx.

sudo apt install nginx

Optionally, you can verify the installation on your server or desktop Nginx using the apt-cache policy command. Listing the version build and ensuring Nginx is installed directly from Nginx repositories is better.

apt-cache policy nginx

Example output:

example of apt-cache policy command checking on nginx mainline installation on ubuntu linuxPin

The above example image shows that the latest Nginx mainline is installed directly from Nginx and has a priority set of 900, higher than any other repository you configured earlier. At this point, you have installed the latest version of Nginx from its official repository.

Share to...