instalar-nginx-ultima-version-ubuntu

How to install the latest version of nginx on ubuntu

  • 2 min

If we install Nginx on a distribution like Ubuntu, using apt, we will find that the version we will install is not the latest*.

This is due to the way Ubuntu (and other distributions) prioritize stable and secure versions, over the more recent versions.

This is especially noticeable in LTS (Long Term Support) versions, which are specifically intended to be stable.

However, many times this causes us to miss out on the latest features of Nginx (such as performance improvements or new functionalities)

Generally, this does not affect security improvements or bug fixes, which are incorporated even in LTS versions

Fortunately, we can install the latest version directly from the official NGINX repositories. Let’s see how to do it step by step for an Ubuntu distro.

Install the required packages

First (as is customary), we ensure that our system is updated

sudo apt update
sudo apt upgrade

Now, we check that we have the necessary tools to add new repositories and manage authentication keys:

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

Import the Nginx signing key

In order for apt to verify the authenticity of the Nginx packages, we will import the official Nginx signing key:

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

If you want, you can check that the key you have downloaded is correct. To do this, run the following command:

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

The output should show the fingerprint, for example,

pub   rsa2048 2011-08-19 [SC] [expires: 2027-05-24]
      573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid                      nginx signing key <[email protected]>

Postdata, I generally never do this step

Configure the NGINX repository

Now we need to add the official Nginx repositories. We can choose between the stable NGINX repository or the mainline version (mainline is the latest version with new features, but less tested in production).

To install the stable version of NGINX, let’s run:

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

If we prefer the mainline version of NGINX, let’s run:

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

Configure repository pinning

To ensure that the NGINX packages we install come from the official NGINX repository and not from the Ubuntu repositories, we will configure repository pinning:

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

Install NGINX

Finally, let’s update the package list and install NGINX:

sudo apt update
sudo apt install nginx