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, PHP 8.0 and the latest PHP 8.1.
This tutorial should work for any Ubuntu release and other Ubuntu-based releases. Ubuntu 18.04, Ubuntu 20.04, Ubuntu 22.04, Ubuntu 21.10, and should even work for Xubuntu, Kubuntu and similar distros.
Tutorials here:
- Before we begin
- How to Install PHP 7.4 on Ubuntu 20.04 (not recommended)
- How to install PHP 8.1 on Ubuntu 22.04 or 20.04
- How to install PHP 8.0 on Ubuntu 22.04 or 20.04
- How to change the PHP version you’re using
- How to upgrade to PHP 8.1 (or 8.0) on Ubuntu
- Speed up PHP by using an opcode cache
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.4, and onto PHP 8.0 and the latest – PHP 8.1. We’ll include instructions for PHP 8.0, PHP 8.1. We recommend that you install PHP 8.1 as it’s stable and has lots of improvements and new features. If you still use PHP 7.4, you definitely need to upgrade ASAP because its security support ends at the end of 2022.
- 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.1 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 22.04, but Ubuntu 22.04 will use whatever the default PHP version it’s using.
We don’t recommend using PHP 7.4, so you should skip to some of the other installation methods below. Use 8.0 or 8.1 instead.
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: Nov 25 2021 23:16:22) ( 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.1 on Ubuntu 22.04 or 20.04
PHP 8.1 is the newest PHP version released on 25 Nov 2021. You can start using it now. These are the instructions on how to install it on Ubuntu 22.04 or Ubuntu 20.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.1
After you’ve added the repository, you can install PHP 8.1 with the following command:
apt-get install php8.1
This command will install additional packages:
- libapache2-mod-php8.1
- libpcre2-8-0
- php8.1-cli
- php8.1-common
- php8.1-opcache
- php8.1-readline
- …and others.
And that’s it. To check if PHP 8.1 is installed on your server, run the following command:
php -v
Which should return something like this:
PHP 8.1.1 (cli) (built: Dec 31 2021 07:26:20) (NTS)
Install PHP 8.1 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.1 php8.1-fpm libapache2-mod-fcgid php8.1-curl php8.1-dev php8.1-gd php8.1-mbstring php8.1-zip php8.1-mysql php8.1-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 8.0 on Ubuntu 22.04 or 20.04
Instead of using the older PHP version that’s included by default in Ubuntu 20.04, you should use PHP 8.0 or 8.1. Here are the instructions on how to install PHP 8.0 on Ubuntu 20.04/22.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 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
- libargon2-0
- libsodium23
- libssl1.1
- php8.0-cli
- php8.0-common
- php8.0-json
- php8.0-opcache
- php8.0-readline
And that’s it. To check if PHP 8.0 is installed on your server, run the following command:
php -v
Install PHP 8.0 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-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 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 8.1 as the default, run:
update-alternatives --set php /usr/bin/php8.1
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.1
And then restart Apache for the changes to take effect:
systemctl restart apache2
How to upgrade to PHP 7.4, 8.0 or 8.1 on Ubuntu
If you’re already using an older version of PHP with some of your applications, you can upgrade by:
- Backup everything.
- Install the newest PHP and required modules.
- Change the default version you’re using.
- (Optionally) Remove the older PHP
- (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 8.1:
echo "extension=apcu.so" | tee -a /etc/php/8.1/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 8.1:
ln -s /etc/php/8.1/mods-available/cache.ini /etc/php/8.1/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.
15 thoughts on “How to install PHP (8.1, 8.0, 7.4) on Ubuntu”
PHP 7.x comes with an opcode cache. A very good one. Unless you specifically need functions from APCu (in which case you likely need PHP5.3 as that was the last time APCu was a good idea) you should keep using the Zend OpCache – it’s about five times faster than APCu and not likely to break anything. If you do think you need APCu functionality outside of opcode caching, look at the myriad of very fast alternatives.
How to set up with Nginx with php 7.2 Are there any important parts in this setup?
Our tutorial is for Apache, but for Nginx it’s quite a different setup. In short, you’ll need:
That’s basically it. It may differ depending on the software you plan on using. Make sure you have all the extensions installed and make sure you have the right nginx configuration (server block). There are plenty of other tutorials on google that will help you with all of this.
This seem to only install prefork and not FPM on apache2. I have been trying to install PHP7.2-FPM on apache2 with no success. Any idea?
Issue Solved when installing php7.2-mcrypt
I was also facing the same issue. Check this link https://stackoverflow.com/q/48275494/7713811 to get the right solution for installing it in PHP
thanks for the info but on kubuntu 18.04 I still a blank screen with phpinfo() why ?
Nice tutorial. You were help a lot.
Thanks for this tutorial. Most helpful.
LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php
very good tutorial.
Thanks a lot.
Install php7.2 WITHOUT apache
apt install php7.2 php7.2-common php7.2-cli php7.2-fpm
I upgraded on my ubuntu 16.04 server from php 7.0 to php 7.2. Installation succeed, and the command php -v says PHP 7.2.13. But my joomla site still says i run PHP 7.0. How can i activate the new installed php 7.2? Th siteruns on a virtual host. Sorry for my bad english…
to change the version in the apache2 I had to execute the command a2dismod php7.2
a2enmod php8.0 Does not work how come
Did you
apt-get install libapache2-mod-php8.0
?