Linux, Web Hosting, and Everything Else in Between
Linux, Web Hosting, and Everything Else in Between
install php 7 or 7.2 ubuntu

How to install PHP (7.4, 8.0) on Ubuntu

Finally, the third part of our LAMP tutorial series: how to install PHP on Ubuntu. In this tutorial, we’ll show you how to install various versions of PHP, including PHP 7.4 and the latest PHP 8.0

This tutorial should work for any Ubuntu release and other Ubuntu-based releases. Ubuntu 16.04, Ubuntu 18.04, Ubuntu 20.04, Ubuntu 20.10, and should even work for Xubuntu, Kubuntu and similar distros.

Tutorials here:

 

For the first part of our LAMP series, go to our How to Install and Optimize Apache on Ubuntu tutorial.

And for the second part, go to How to Install MySQL/MariaDB on Ubuntu.


Before we begin installing PHP on Ubuntu

  • PHP has different versions and releases you can use. Starting from the oldest that is currently supported – PHP 7.3, and onto PHP 7.4 and the latest – PHP 8.0. We’ll include instructions for PHP XX, PHP XX. We recommend that you install PHP 8.0 as it’s stable and has lots of improvements and new features. If you still use PHP 7.3, you definitely need to upgrade ASAP because its security support ends at the end of 2021.
  • You’ll obviously need an Ubuntu server. You can get one from Vultr. Their servers start at $2.5 per month. Or you can go with any other cloud server provider where you have root access to the server.
  • You’ll also need root access to your server. Either use the root user or a user with sudo access. We’ll use the root user in our tutorial so there’s no need to execute each command with ‘sudo’, but if you’re not using the root user, you’ll need to do that.
  • You’ll need SSH enabled if you use Ubuntu or an SSH client like MobaXterm if you use Windows.
  • Check if PHP is already installed on your server. You can use the ‘which php’ command. If it gives you a result, it’s installed, if it doesn’t, PHP is not installed. You can also use the “php -v” command. If one version is installed, you can still upgrade to another.
  • Some shared hosts have already implemented PHP 8.0 in their shared servers, like Hawk Host and iWebFusion.

Now, onto our tutorial.

How to Install PHP 7.4 on Ubuntu 20.04

PHP 7.4 is included by default in Ubuntu 20.04’s repositories. So the instructions are pretty similar to other variations.

The instructions are also similar for Ubuntu 16.04, but Ubuntu 16.04 will use whatever the default PHP version it’s using.

Update Ubuntu

Again, before doing anything, you should update your server:

apt-get update && apt-get upgrade

Install PHP 7.4

Next, to install PHP 7.4 on Ubuntu 20.04, just run the following command:

apt-get install php

This command will install PHP 7.4, as well as some other dependencies.

To verify if PHP is installed, run the following command:

php -v

You should get a response similar to this:

PHP 7.4.3 (cli) (built: Oct 6 2020 15:47:56) ( NTS )

And that’s it. PHP 7.4 is installed on your Ubuntu 20.04 server.

Install PHP 7.4 modules (extensions)

These are the most common PHP 7.4 modules often used by php applications. You may need more or less, so check the requirements of the software you’re planning to use:

apt-get install php-pear php-fpm php-dev php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml libapache2-mod-php

To check all the PHP modules available in Ubuntu, run:

apt-cache search --names-only ^php

How to install PHP 8.0 on Ubuntu 20.04 or 18.04

PHP 8.0 is the newest PHP version released on 26th Nov 2020. You can start using it now. These are the instructions on how to install it on Ubuntu 20.04 or Ubuntu 18.04 (or any other Ubuntu)

Update Ubuntu

First, update your Ubuntu server:

apt-get update && apt-get upgrade

Add the PHP repository

To install PHP 8.0 you’ll need to use a third-party repository. We’ll use the repository by Ondřej Surý that we previously used.

First, make sure you have the following package installed so you can add repositories:

apt-get install software-properties-common

Next, add the PHP repository from Ondřej:

add-apt-repository ppa:ondrej/php

And finally, update your package list:

apt-get update

Install PHP 8.0

After you’ve added the repository, you can install PHP 8.0 with the following command:

apt-get install php8.0

This command will install additional packages:

  • libapache2-mod-php8.0
  • libaprutil1-dbd-sqlite3
  • php8.0-cli
  • php8.0-common
  • php8.0-json
  • php8.0-opcache
  • php8.0-readline
  • …and others.

And that’s it. To check if PHP 8.0 is installed on your server, run the following command:

php -v

Which should return something like this:

PHP 8.0.0 (cli) (built: Nov 27 2020 12:26:22) ( NTS )

Install PHP 8.0 modules (extensions)

You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with the following command:

apt-get install libapache2-mod-php8.0 php8.0-fpm libapache2-mod-fcgid php8.0-curl php8.0-dev php8.0-gd php8.0-mbstring php8.0-zip php8.0-mysql php8.0-xml

And that’s all. You can now start using PHP on your Ubuntu server.

If you want to further tweak and configure your PHP, read our instructions below.

How to Install PHP 7.4 on Ubuntu 18.04

Instead of using the older PHP version that’s included by default in Ubuntu 18.04, you should use PHP 7.4 or PHP 8.0. Here are the instructions on how to install PHP 7.4 on Ubuntu 18.04

Update Ubuntu

Of course, as always, first update Ubuntu:

apt-get update && apt-get upgrade

Add the PHP repository

You can use a third-party repository to install the latest version of PHP. We’ll use the repository by Ondřej Surý.

First, make sure you have the following package installed so you can add repositories:

apt-get install software-properties-common

Next, add the PHP repository from Ondřej:

add-apt-repository ppa:ondrej/php

And finally, update your package list:

apt-get update

Install PHP 7.4

After you’ve added the repository, you can install PHP 7.4 with the following command:

apt-get install php7.4

This command will install additional packages:

  • libapache2-mod-php7.4
  • libargon2-0
  • libsodium23
  • libssl1.1
  • php7.4-cli
  • php7.4-common
  • php7.4-json
  • php7.4-opcache
  • php7.4-readline

And that’s it. To check if PHP 7.4 is installed on your server, run the following command:

php -v

Install PHP 7.4 modules

You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with the following command:

apt-get install libapache2-mod-php7.4 php7.4-fpm libapache2-mod-fcgid php7.4-curl php7.4-dev php7.4-gd php7.4-mbstring php7.4-zip php7.4-mysql php7.4-xml

And that’s all. You can now start using PHP on your Ubuntu server.

If you want to further tweak and configure your PHP, read our instructions below.

How to change the PHP version you’re using

If you have multiple PHP versions installed on your Ubuntu server, you can change what version is the default one.

To set PHP 7.4 as the default, run:

update-alternatives --set php /usr/bin/php7.4

To set PHP 8.0 as the default, run:

update-alternatives --set php /usr/bin/php8.0

You can verify what version of PHP you’re using with the following command:

php -v

If you’re following our LAMP tutorials and you’re using Apache, you can configure Apache to use PHP 8.0 with the following command:

a2enmod php8.0

And then restart Apache for the changes to take effect:

systemctl restart apache2

How to upgrade to PHP 7.4 or 8.0 on Ubuntu

If you’re already using an older version of PHP with some of your applications, you can upgrade by:

  1. Backup everything.
  2. Install the newest PHP and required modules.
  3. Change the default version you’re using.
  4. (Optionally) Remove the older PHP
  5. (Required) Configure your software to use the new PHP version. You’ll most likely need to configure Nginx/Apache, and many other services/applications. If you’re not sure what you need to do, contact professionals and let them do it for you.

Speed up PHP by using an opcode cache

You can improve the performance of your PHP by using a caching method. We’ll use APCu, but there are other alternatives available.

If you have the ‘php-pear’ module installed (we included it in our instructions above), you can install APCu with the following command:

pecl install apcu

There are also other ways you can install APCu, including using a package.

To start using APCu, you should run the following command for PHP 7.4:

echo "extension=apcu.so" | tee -a /etc/php/7.4/mods-available/cache.ini

Or this command for PHP 8.0:

echo "extension=apcu.so" | tee -a /etc/php/8.0/mods-available/cache.ini

If you’re following our LAMP tutorials and you’re using Apache, create a symlink for the file you’ve just created.

For PHP 7.4:

ln -s /etc/php/7.4/mods-available/cache.ini /etc/php/7.4/apache2/conf.d/30-cache.ini

For PHP 8.0:

ln -s /etc/php/8.0/mods-available/cache.ini /etc/php/8.0/apache2/conf.d/30-cache.ini

And finally, reload Apache for the changes to take effect:

systemctl restart apache2

To further configure APCu and how it works, you can add some additional lines to the cache.ini file you previously created. The best configuration depends on what kind of server you’re using, what applications you are using etc. Either google it and find a configuration that works for you, or contact professionals and let them do it for you.

That’s it for our basic setup. Of course, there are much more options and configurations you can do, but we’ll leave them for another tutorial.

Leave a Reply to Val Cancel reply

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

13 thoughts on “How to install PHP (7.4, 8.0) on Ubuntu”