Install latest Nginx on various Ubuntu versions

Updated on December 15, 2017

We shall learn how to install latest Nginx on various Ubuntu versions. As of writing this article, the latest stable Nginx version is 1.12.2 released as on 17 Oct 2017. The changelog is here for your view.

Nginx from Ubuntu Software repository

Nginx is included in Ubuntu Software repository, but it might be out-of-date.

To install the latest version of Nginx, we need to add a official Nginx repository.

How to Install latest Nginx

Add official Nginx repository in Ubuntu

Open up the sources.list file and add the below code:

## Replace $release with your corresponding Ubuntu release.
deb http://nginx.org/packages/ubuntu/ $release nginx
deb-src http://nginx.org/packages/ubuntu/ $release nginx

For example on Ubuntu 16.04 (Xenial):

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

Replace the release names obtained from the below command:

# awk -v var=$(lsb_release -rs) '$3~var {print $4" "$5;exit;}' /usr/share/python-apt/templates/Ubuntu.info | cut -d"'" -f2
Xenial

To verify the integrity of packages downloaded from the above repository, we need to import Nginx public key using the commands below:

# wget http://nginx.org/keys/nginx_signing.key
# apt-key add nginx_signing.key
OK

Once the repository is setup, update the repository info using the below command:

apt-get update

Add PPA Nginx repository in Ubuntu

An another repository maintained by volunteers and is not distributed by nginx.org is PPA. You can get the latest stable version of Nginx from this Nginx PPA repository too. The advantage of this repository is that it has some additional compiled-in modules and may be more fitting to your environment.

Only following release of Ubuntu are supported in this PPA.

Trusty (14.04), Xenial (16.04) and Yakkety (16.10).

Add Nginx PPA to your repository list automatically

Add ppa:nginx/development to your system’s repository using the below command:

# add-apt-repository ppa:nginx/stable

If you encounter an error “add-apt-repository command not found“, then follow the below manual method of updating the repository.

Add Nginx PPA to your repository list manually

Copy the below lines and insert into the file: /etc/apt/sources.list

According to your Ubuntu version from the below command:

# awk -v var=$(lsb_release -rs) '$3~var {print $4" "$5;exit;}' /usr/share/python-apt/templates/Ubuntu.info | cut -d"'" -f2
Xenial

replace YOUR_UBUNTU_VERSION_HERE with the output of the above command in the below lines.

deb http://ppa.launchpad.net/nginx/development/ubuntu YOUR_UBUNTU_VERSION_HERE main 
deb-src http://ppa.launchpad.net/nginx/development/ubuntu YOUR_UBUNTU_VERSION_HERE main

Update the repository list

# apt-get update
Remove older Nginx

If you have installed Nginx from Ubuntu repository and it is an older version, you need to remove it before you proceed to the next step.

Remove older version of Nginx on Ubuntu

Before you remove the older version of Nginx, backup your main Nginx config file, as it will be replaced with a new nginx.conf file.

# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bk
# apt-get remove nginx nginx-common nginx-full nginx-core

Install Nginx

# apt-get install nginx

Start Nginx service

# systemctl start nginx

Add Nginx service to start at boot

# systemctl enable nginx

Check Nginx version

# nginx -v
nginx version: nginx/1.12.2

Was this article helpful?

Related Articles

Leave a Comment