How to install PHP on Ubuntu 22.04 | Step-by-Step

May 15th, 2023
How to install PHP on Ubuntu 22.04 | Step-by-Step

PHP is an open-source server-side scripting language used in web development. It performs a wide range of functions, including handling forms, database functions, gathering and saving data to files, and sending data via email.

In this step-by-step tutorial, you will learn how to install PHP, including associated PHP modules as well as how to configure the default version and uninstall it.

What is PHP?

PHP, short for Hypertext Preprocessor, is a free and open-source server-side scripting language that plays a crucial role in the development of both static and dynamic websites. It was created in 1993 and officially released in 1995 by Danish-Canadian Rasmus Lerdorf, initially referred to as Personal Home Page.

PHP is still popular and widely used in web development despite the emergence of other server-side scripting languages such as Ruby, Perl, and Lua. According to W3techs, PHP is used by close to 77.4% of all websites that use a server-side scripting language. In fact, it powers WordPress, a popular CMS (Content Management System) that powers nearly 43% of all websites on the internet.

What is PHP used for?

PHP is widely used to develop interactive and dynamic websites. It is also the last layer of the LAMP stack (Linux, Apache, MySQL, PHP), popular open source components for web development. It runs on a browser and is embedded in HTML pages to add functionality to a website without the need of calling external files. PHP code is first executed on the server, and the result is finally rendered on a web browser.

Popular sites that run PHP include Facebook, Pinterest, Wikipedia, WordPress, Slack, and many others. Currently, PHP 8.2 is the latest release. It ships with numerous features and improvements. Check out the release page for a comprehensive list of new features and enhancements.

In this tutorial, you will learn how to install PHP 8.2 on Ubuntu 22.04.

Prerequisites

Before you set sail, ensure that you have the following requirements in check.

How to install PHP on Ubuntu 22.04 in 5 steps

Once you've reviewed the prerequisites section and set up the requirements for this installation guide, let's dive right in. Follow the below steps to learn how to install PHP on Ubuntu 22.04 system.

Step 1: Install Dependencies on Ubuntu

To get started, log into your server instance via SSH and update the system as follows.

sudo apt update

Next, install the required dependencies, which are essential in the installation of PHP.

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

Step 2: Install PHP on Ubuntu

There are two main ways of installing PHP on Ubuntu. You can install PHP from Ubuntu repositories using the APT package manager, as shown.

sudo apt install php

However, this does not install the latest release of PHP. Currently, the repository only offers PHP 8.1. From the following output, you can see that PHP 8.1 is going to be installed alongside other PHP modules and the Apache web server.

If you don’t mind installing this release, go ahead and press ‘Y’ on the keyboard and hit ENTER.

apt install php

To confirm that PHP is installed, run the command:

php --version

php --version

To install the latest PHP release, you need to add the Sury OndreJ PPA (Personal Package Archive ). This is a PPA maintained by Ondrej Sury, a Debian developer who has been packing a collection of software into Ubuntu and Debian, most notably PHP. The PPA provides the latest versions of PHP, which have yet to be available on the main Ubuntu / Debian repositories.

Therefore, to install PHP 8.2, first, add the OndreJ PPA by running the following command:

sudo add-apt-repository ppa:ondrej/php

add-apt-repository ppa:ondrej/php

Hit ‘ENTER’ on your keyboard when prompted to continue.

Add php repository to apt

Once the installation is complete, update the local package index. This notifies your system of the newly added PPA and enables it to install packages from the PPA.

sudo apt update

Finally, install PHP 8.2 as follows.

sudo apt install php8.2 -y

The command installs PHP 8.2 and other PHP extensions, including php8.2-cli, php8.2-common, and php8.2-opcache.

Once the installation is complete, verify that you have installed the desired version of PHP.

php --version

php --version

The output below confirms that we have installed PHP 8.2.

Step 3: Confirm PHP installation

There are two ways of checking the version of PHP installed on your system. First, as previously seen, you can verify on the command line as shown.

php --version

php --version

Alternatively, you can confirm this from a web browser. But first, you need to create an info.php file that will display detailed information about the PHP version installed.

First, create the file in Apache’s Document Root directory as follows.

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

Paste the following lines.

<?php
phpinfo();
?>

Save the changes and exit the file. Then restart the Apache web server for the changes to come into effect.

sudo systemctl restart apache2

Next, open your browser and browse the following URL http://server-ip/info.php

This displays the PHP information page with information about the PHP version and other finer details.

PHP information page

Step 4: Install PHP Modules on Ubuntu

PHP Modules, also known as PHP extensions, are compiled PHP libraries that extend the functionality of PHP. For example, to enable PHP to work with MySQL databases, you need to install the php8.2-mysql extension.

The syntax for installing PHP modules is as shown.

sudo apt install php8.2-module_name

For example, the following command installs php8.2-mysql, php8.2-cli, php8.2-gd and php8.2-zip PHP modules.

sudo apt install php8.2-mysql php8.2-cli php8.2-gd php8.2-zip

Alternatively, you can use the following shorthand notation to avoid repetitive commands.

sudo apt install php8.2-{mysql,cli,gd,zip}

To view the list of all loaded PHP modules, run the command:

$ php -m

Step 5: Set the Current PHP Version ( Optional )

You can have multiple PHP versions installed on your system. By default, the recently installed version automatically becomes the default PHP version. In our setup, we have PHP 8.1 and PHP 8.2 installed, the former of which is the current PHP version.

The update-alternatives command allows you to set your preferred PHP version as shown.

$ sudo update-alternatives --config php

Once the command is executed, a list of PHP versions will be populated with the default version prefixed with an asterisk. You will be prompted to select your current PHP version. Type in a number from the Selection column that corresponds to your preferred PHP version and press ‘ENTER’.

In the example below, PHP 8.1 was initially the default version. To set PHP 8.2 as the current version, we have selected option ‘2’ and hit ENTER.

update-alternatives --config php

To verify your current version, once again run the command:

$ php --version

How To Uninstall PHP

If you no longer require PHP on your system, you can remove it as follows.

sudo apt remove php8.2

Additionally, you can uninstall all the modules as shown.

sudo apt remove php8.2-*

Conclusion

In this guide, we have shown you how to install PHP on Ubuntu 22.04. To recap, you can install it directory from the default Ubuntu repositories or from the Ondrej Sury PPA, which provides the latest version.

Thanks for learning with us! For more reading and tutorials, check out Cherry Servers blog.

Winnie is a seasoned Linux Systems administrator, currently specializing in writing technical Linux tutorials. With over seven years of experience in deploying and working with major Linux distributions such as Ubuntu, Debian, RHEL, OpenSUSE, and ArchLinux, she has written detailed and well-written "How to" Linux guides and tutorials. Winnie holds a Bachelor's Degree in Computer Science from Masinde Muliro University, Kenya and resides in Nairobi, Kenya. She is an expert in authoring Linux and DevOps topics involving Docker, Ansible, and Kubernetes. She currently works as a freelance technical writer and consultant. In her previous roles, she worked in the capacity of an IT support specialist and Linux administrator. Her key roles included offering level 1 and 2 support to both in-house and remote staff and managing and monitoring Linux servers.

Start Building Now

Deploy your new Cloud VPS server in 3 minutes starting from $5.83 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 5bc831c3.737