Selfmade Techie

Selfmade Techie Logo

How to install PHP on Ubuntu

How to install PHP on Ubuntu

How to install PHP 8.3 on Ubuntu

How to install PHP 8.3 on Ubuntu

How to install PHP 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, PHP 8.0, PHP 8.1 and the latest PHP 8.3.

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

Tutorials here:

  • How to install PHP 8.3 on Ubuntu 24.04
  • How to upgrade to PHP 7.4, 8.0, 8.1 or 8.3 on Ubuntu
  • How to change the PHP version you’re using
  • Speed up PHP by using an opcode cache

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.3. We’ll include instructions for PHP 8.3. We recommend that you install PHP 8.3 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 8.3 on Ubuntu 22.04 or 24.04

PHP 8.3 is the newest PHP version. You can start using it now. These are the instructions on how to install it on Ubuntu 24.04 or Ubuntu 22.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.3

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

apt-get install php8.3
apt-get install php (FOR LATEST VERSION INSTALLATION)

This command will install additional packages:

  • libapache2-mod-php8.3
  • libpcre2-8-0
  • php8.3-cli
  • php8.3-common
  • php8.3-opcache
  • php8.3-readline
  • …and others.

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

php -v

Which should return something like this:

PHP 8.3.x (cli) 

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.3 as the default, run:

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

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, 8.1 or 8.3 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 8.3:

echo "extension=apcu.so" | tee -a /etc/php/8.3/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.3:

ln -s /etc/php/8.3/mods-available/cache.ini /etc/php/8.3/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.

Install WordPress on Ubuntu 24.04

Leave a Comment

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

Scroll to Top