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

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.



  • Recommended OS: Ubuntu 20.04
  • User account: A user account with sudo or root access.

Update Operating System

Update your Ubuntu operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade -y

The tutorial will be using the sudo command and assuming you have sudo status.

To verify sudo status on your account:

sudo whoami

Example output showing sudo status:

[joshua@ubuntu ~]$ sudo whoami

 To set up an existing or new sudo account, visit our tutorial on adding a User to Sudoers on Ubuntu.

To use the root account, use the following command with the root password to log in.


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*

Option 1. NGINX Repository Installation

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 PPA’s.

To import the stable PPA:

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

To import the mainline PPA:

sudo add-apt-repository ppa:ondrej/nginx-mainline
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 if your server is a monster, set to 11 and monitor CPU usage levels.

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

Conclusion & Comments:

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.

Notify of
Inline Feedbacks
View all comments
Wednesday, November 3, 2021 5:18 am

Thank you! Very helpful article!

Sunday, November 7, 2021 8:58 am

Thank you very much. This worked perfectly for me with Ubuntu 20.04. I successfully updated my Nginx. Thank you again.

Would love your thoughts, please comment.x