How to install PHP on Ubuntu?

PHP is a very popular server-side scripting language. We can create dynamic and interactive Web pages using PHP. The world’s most popular CMS WordPress is written in PHP. Facebook started with PHP and still some part of Facebook uses PHP.

PHP is the widely-used programming language on the Web. In this post, we are going to install PHP 7.4 on Ubuntu. The steps will work on the ubuntu version: 20.04/19.04/18.04/16.04.

First of all run the following command to update apt-get itself, which ensures that we have access to the latest versions of anything we want to install.

$ sudo apt-get update

We will have to now install software-properties-common, which adds management for additional software sources:

$ sudo apt -y install software-properties-common
// -y flag will automatically agree to the installation.

Next, install the repository ppa:ondrej/php, which will give you all your versions of PHP:

$ sudo add-apt-repository ppa:ondrej/php

Now we need to update apt-get again so that the package manager can see the newly listed packages:

$ sudo apt-get update

Now we are ready to install PHP 7.4 using the following command:

$ sudo apt -y install php7.4

If the PHP installation goes successfully, we will be able to check the version using the following command:

$ php -v

// Output
PHP 7.4.0beta4 (cli) (built: Aug 28 2019 11:41:49) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.0beta4, Copyright (c), by Zend Technologies

PHP needs additional modules to meet different requirements. We can also install more than one module/package at a time.

$ sudo apt install -y php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl

The above command will install the most commonly used PHP modules.