How to Upgrade/Install Nginx Stable/Mainline on Ubuntu 20.04 LTS


For those using Ubuntu 20.04 LTS, you might have noticed that installing Nginx directly from its repository does not install the latest stable or mainline version. This is a common trend in most distributions that focus on the stability of packages and provide only urgent bug or security updates until the subsequent major distribution.

For most, using the default Nginx that comes bundled with the repository will be preferred, but often many require and want the latest version of stable or mainline for updated features.

The following tutorial will cover installing the last stable or mainline versions of Nginx on Ubuntu 20.04 LTS Focal Fossa with two methods, both utilizing the APT package manager with importing the official repository from Nginx which is the most recommended option, or the alternative PPA model by Ondřej Surý.

Update Ubuntu

Before you begin, update your system to ensure all packages are up-to-date to avoid any conflicts.

sudo apt update && sudo apt upgrade -y

Remove Previous Nginx Installation

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


First, back up your Nginx configuration for safekeeping.

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

Stop Nginx using the systemctl command as follows:

sudo systemctl stop nginx

Next, remove Nginx using the following command:

sudo apt-get remove nginx*

NGINX Repository Installation – Nginx Repository

The first method is to install and update using Nginx’s repository, which will give you at all times the latest version available.

First, open your terminal (CTRL+ALT+T) and install the dependencies required for importing and installing Nginx using the official repository.

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

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


curl | 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 stable or mainline Nginx repository into your apt package manager list.

Import Nginx Stable Repository:

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

Import Nginx Mainline Repository:

echo "deb [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 other default Ubuntu repositories or PPA’s. 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

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

sudo apt update

Now proceed to install Nginx.


sudo apt install nginx

At this point, you have installed the latest version of Nginx from its official repository.

Option 2 – Ondřej Surý PPA Nginx Installation

An alternative method for users who prefer a more straightforward approach is installing Nginx mainline by Ondřej Surý, who many would know as the leading PHP developer and maintainer for Debian repositories. This PPA is safe and is usually maintained within hours to a few days after each update goes out by Nginx.

The PPA supports both stable and mainline however they are separate PPAs.

Import the Stable PPA:

sudo add-apt-repository ppa:ondrej/nginx -y

Import the Mainline PPA:

sudo add-apt-repository ppa:ondrej/nginx-mainline -y

Only import one repository, do not import both.

Next, update the APT cache.

sudo apt update

Once the chosen repository is added, proceed to install Nginx mainline in full by using the following command:


sudo apt install nginx-core nginx-common nginx nginx-full

And that is it! You have installed the latest version of Nginx using the PPA by Ondřej Surý.

Additionally, the PPA has several benefits of extra modules included. One of the main advantages is having the ability to add Brotli support. To install brotli, follow the steps below.

Open your nginx.conf configuration file.

sudo nano /etc/nginx/nginx.conf

Now add the additional lines before in the HTTP{} section:

brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types application/atom+xml application/javascript application/json application/rss+xml
   application/ application/x-font-opentype application/x-font-truetype
   application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
   font/eot font/opentype font/otf font/truetype image/svg+xml image/
   image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;

The brotli_comp_level can be set between 1 (lowest) and 11 (highest). Typically, most servers sit in the middle, but set it to 11 and monitor CPU usage levels if your server is a monster.

Next, test to make sure the changes are working correctly before making it live:


sudo nginx -t

If the changes are working correctly, you should see the following:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now make the changes live by restarting your server:

sudo systemctl restart nginx

Comments and Conclusion

The tutorial has shown you how to install the Nginx repository or PPA so you can pull either Nginx stable or mainline latest versions on your Ubuntu 20.04 LTS system.

Overall, using the latest stable Nginx or Mainline versions is relatively safe compared to other software where bugs and instability could be present. Nginx does a fantastic job in keeping its web application running smoothly.

Share on: