How to Install PHP on Ubuntu 24.04

Install PHP on Ubuntu

PHP (Hypertext Preprocessor) is a widely-used open-source scripting language primarily designed for web development and can be embedded into HTML. It’s versatile and suitable for a range of tasks, from server-side scripting to command-line scripting and even writing desktop applications.

Originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994, PHP has evolved into a powerful language, thanks to contributions from a large community of developers worldwide. PHP is particularly well-suited for web development because it can be easily embedded into HTML code and executed on the server, generating dynamic web pages.

PHP can interact with databases, handle forms, manage session tracking, and even create custom content management systems (CMS) and e-commerce websites. It’s compatible with various web servers and operating systems, making it a versatile choice for developers.

This tutorial focuses on installing PHP on Ubuntu 24.04 Noble Numbat.

Prerequisites

An Ubuntu 24.04 instance

Root/sudo privileges

Install PHP on Ubuntu 24.04

Step 1. Update package repository

To get started log in to your system. Launch the terminal (by clicking on Show Apps or by using the shortcut key CTRL + Alt + T) and issue the following command to update package repositories. This updates all system packages to their latest versions. Therefore, it is an important step and one must not ignore it.

sudo apt update

Wait for the command to finish it.

Step 2. Install required dependencies

The second step is to install dependencies that are required for smooth functioning of PHP. Run the following:

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

When prompted, press y and then Enter key to continue.

Step 3. Install PHP

Finally, you can install PHP on your Ubuntu machine. Please note that there are several ways to install PHP. We will be installing it from Ubuntu’s official repository using apt. Invoke the following:

sudo apt install php

Verify the PHP Installation

There are two methods you can use to verify the PHP installation. First method is by checking the version as follows.

php --version

The command will return you the version installed on your machine as shown in the below screenshot.

The second method invloves checking PHP version from the browser. For that you need to create a info.php file in the root directory of Apache Webs server. Let’s create an empty file.

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

Paste the following code in the newly created info.php file.

<?php
phpinfo();
?>

Save and quit the file.

Open localhost/php.info in your favorite browser. The webpage will open which lists you all PHP-related information.





Remove PHP from Ubuntu 24.04

You can remove the PHP from your system whenever required with just a single command.

sudo apt remove php

Final Thoughts

Thank you for learning with us. If you have any questions or feedback, send us. We will review it and get back to you. Keep visiting Linux World.