PHP 8.0, how to install this language in Ubuntu 20.04 | 18.04

about php 8.0

In the next article we are going to take a look at how can we install PHP 8.0 using Ubuntu 18.04 or 20.04 with Apache. This is a popular language for web development originally created in 1994 by rasmus lerdorf, a Danish-Canadian programmer. It is the language used to develop dynamic and responsive websites. In fact, platforms CMS Popular like WordPress, Drupal and Magento are based on PHP.

The created PHP files can be run on both Gnu / Linux, macOS, Windows and many other Unix systems, as long as PHP has been installed. In the following lines we are going to see how we can install PHP 8.0 in Ubuntu 20.04.

General features of PHP 8.0

The latest version to this day of PHP is PHP 8.0 and was released on November 26, 2020. In it you can find many new features. PHP 8.0 is a major update to the PHP language that contains new features and optimizations. Among them we can find:

  • This version only specifies the required parameters, skip the optionals. The arguments are independent of order and are automatically documented.
  • Attributes Instead of annotations in PHP Doc, we can use structured metadata.
  • We will need less code to define and initialize a property.
  • We can use a native union type declaration which will be validated at the time of execution.
  • Match expressions. The new match expressions are similar to switch and have the following characteristics like; Match is an expression, which means that it can be stored as variables or returned. It also makes strict comparisons.
  • Nullsafe operator. Instead of checking for null conditions, users can use a string with the new nullsafe operator. When the evaluation of an element fails, the execution of the chain is aborted and evaluated as null.
  • Smart comparisons between strings and numbers.
  • Most internal functions now provide an exception error if the parameter is not validated.

These are just some of the features of PHP 8.0. They can consult all of them in detail from the php.net.

Install PHP 8.0 on Ubuntu

Add the PPA

PHP 7.4 is the default version in the Ubuntu 20.04 repositories at the time of this writing. In order to install the latest version of PHP we will need to use the Ondrej PPA repository. It contains multiple versions and extensions of PHP.

Before proceeding with the installation we will have to open a terminal (Ctrl + Alt + T) and update system packages. We will also install some dependencies.

sudo apt update; sudo apt upgrade

install php 8 dependencies

sudo apt install ca-certificates apt-transport-https software-properties-common

After the installation of the dependencies, we can add the Ondrej PPA. In the same terminal, we will only need to use the command:

add repository for php 8.0

sudo add-apt-repository ppa:ondrej/php

Install PHP 8.0 on Apache

After adding the PPA in our team, it should occur updating packages available from repositories.

installed version of apache2

If you are running an Apache web server, you can proceed to install PHP 8.0 with the Apache module. To do this, you just have to open a terminal (Ctrl + Alt + T) and execute the command:

install php 8 on apache2

sudo apt install php8.0 libapache2-mod-php8.0

Once the installation is finished, we will have to restart web server Apache to enable the module.

sudo systemctl restart apache2

At this point, we can confirm the default PHP version on the server:

php apache version

php -v

If you are interested in using the Apache web server with PHP-FPM, run the following command to install the necessary packages:

install php fpm

sudo apt install php8.0-fpm libapache2-mod-fcgid

Given that PHP-FPM is not enabled by default, we will have to enable it with the following commands:

enable php fpm

sudo a2enmod proxy_fcgi setenvif

sudo a2enconf php8.0-fpm

Then we will have to go back to restart Apache web server for changes to take effect:

sudo systemctl restart apache2

Install PHP 8 extensions

PHP extensions are libraries that extend the functionality of PHP. These extensions exist as packages and can be installed as follows:

sudo apt install php8.0-[nombre-de-extension]

Verify the installation

To confirm the version of PHP installed, from the graphical environment we can create a php file in / Var / www / html called info.php:

sudo vim /var/www/html/info.php

Inside the file, we will only have to paste the following lines and save the file.

<?php

phpinfo();

?>

Finally, in our favorite web browser we are going to write the server's IP address in the URL and the name of the newly created file:

http://ip-de-servidor/info.php

When accessing this small file, if everything is correct we should see the following screen:

php8 in apache

And with this we can consider PHP 8.0 installed and integrated with the Apache web server running on Ubuntu 20.04.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Pedro Perez Pofenas said

    If you already had version 7 of php installed, in order for it to work with Apache, it is necessary to disable the php7-x module and enable php8.0 with the following commands:
    sudo a2dismod php7.x
    sudo a2enmod php8.0

  2.   MM21 said

    It won't let me create the folder 🙁
    I've tried with mkdir, but it doesn't load with php
    I want to install PHP to open it with Neatbeans, it took two days for it.

    Any help is appreciated.
    Thank you for all the information.
    ^^,

  3.   Benjamin said

    hello I have Ubuntu 16 installed, I had PHP 7.0 installed and I uninstalled it but now I uninstalled apache, mysql and php 7 and I have followed this manual but I have not been able to get it to work.

    Do you have any idea why it could be?

    1.    Damien A. said

      Hello. Your problem is given by your version of Ubuntu. Ubuntu 16 is no longer supported. I recommend that you update your version of Ubuntu to a more current version and reinstall php 8. Salu2.

  4.   Paul Perdomo said

    Thank you!!! They are great!