Language: EN

instalar-nginx-ultima-version-ubuntu

How to install the latest version of nginx on ubuntu

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 <signing-key@nginx.com>

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

To install the latest version of Nginx on Ubuntu, you can follow these steps. These steps are to ensure that you are installing the most recent version from the official Nginx repository, instead of the version that comes by default in the Ubuntu repositories.

1. Add the official Nginx repository

  1. Import the repository key:

    Open a terminal and run the following command to add the GPG key for Nginx:

    sudo wget https://nginx.org/keys/nginx_signing.key
    sudo apt-key add nginx_signing.key
  2. Add the repository to the sources list:

    Open the file /etc/apt/sources.list in a text editor (you can use nano to edit it):

    sudo nano /etc/apt/sources.list

    Add the following lines at the end of the file, according to the version of Ubuntu you are using:

    • For Ubuntu 22.04 (Jammy Jellyfish):

      deb http://nginx.org/packages/ubuntu/ jammy nginx
      deb-src http://nginx.org/packages/ubuntu/ jammy nginx
    • For Ubuntu 20.04 (Focal Fossa):

      deb http://nginx.org/packages/ubuntu/ focal nginx
      deb-src http://nginx.org/packages/ubuntu/ focal nginx
    • For Ubuntu 18.04 (Bionic Beaver):

      deb http://nginx.org/packages/ubuntu/ bionic nginx
      deb-src http://nginx.org/packages/ubuntu/ bionic nginx

    Save the file and exit the editor (in nano, press Ctrl+O to save and Ctrl+X to exit).

2. Install Nginx

  1. Update the package list:

    sudo apt update
  2. Install Nginx:

    sudo apt install nginx

3. Verify the installation

To ensure that Nginx has been installed correctly and is using the latest version, you can check the installed version:

nginx -v

This command should show you the version of Nginx installed.

4. Manage Nginx

  • Start Nginx:

    sudo systemctl start nginx
  • Enable Nginx to start automatically on boot:

    sudo systemctl enable nginx
  • Check the status of Nginx:

    sudo systemctl status nginx

With these steps, you should have the latest version of Nginx installed and running on your Ubuntu system.