PHP stands for Hypertext Preprocessor, which is remains a cornerstone of the web, powering a significant portion of websites and web applications across the internet. Majority of top websites and blogging tools are still running with PHP like WordPress, Facebook, Wikipedia etc. If you’re developing for the web on Ubuntu 24.04, installing PHP is likely one of the first tasks you’ll undertake.
Do you want to build websites or run web applications on your Ubuntu 24.04 computer? If so, you’ll need to install PHP! This guide will show you how to easily install different versions of PHP (from 8.3 down to 5.6) on your system. We’ll walk you through the steps and explain things clearly along the way.
Prerequisites
- A system running Ubuntu 24.04
- Sudo privileges
- Internet connection
Step 1: Update Your System
Before proceeding with the installation, it’s good practice to update your package list to ensure you’re getting the latest versions of software and dependencies.
sudo apt update
sudo apt upgrade -y
Step 2: Install Software Properties Common
Ubuntu 24.04 should come with software-properties-common installed, but in case it isn’t, you can install it using the following command. This package provides necessary scripts for adding repositories to your system.
sudo apt install software-properties-common -y
Step 3: Add PHP Repository
To ensure access to the latest PHP versions, including 8.3, 8.2, and 7.4, it’s recommended to add a third-party repository such as the Ondřej Surý PPA, which is widely trusted in the Ubuntu community. This repository contains updated PHP debian packages for the active LTS releases.
Use the following commands to add PHP repository to your system:
LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
Step 4: Install PHP
Once the repository is added to the system, you can proceed with the installation of PHP and required modules. You can choose to install PHP 8.3, 8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.1, or 5.6 depending on your application requirements.
- For PHP 8.3
sudo apt install php8.3 -y
- For PHP 8.2
sudo apt install php8.2 -y
- For PHP 7.4
sudo apt install php7.4 -y
You can also install multiple PHP versions on your system as required.
Step 5: Verify Installation
After the installation, you can verify that PHP is installed correctly by checking the version.
php -v
This command should output the PHP version you installed, along with some additional information. For multi php installation, You will see the default PHP version, To see other php versions use command like: php8.3 -v
, php8.2 -v
, etc.
Step 6: Install PHP Extensions
PHP extensions enhance or add functionalities to the PHP installation. You might need specific extensions for your projects. To install an extension, you can use the following command, replacing extension_name with the name of the extension and phpX.X with your PHP version (e.g., php8.3).
sudo apt install phpX.X-extension_name
Here’s an example of installing the MySQL extension for PHP 8.3:
sudo apt install php8.3-mysql
Step 7: Install PHP Composer (Optional)
Composer is an dependency management tool for the PHP applications. While Composer is not essential for every project, it’s a valuable asset for any developer looking to streamline their workflow and ensure project dependencies are properly installed and managed. If required, you can also install it by executing the following commands.
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Step 7: Configure PHP (Optional)
Optimizing PHP settings can enhance your project’s performance. The main configuration file, php.ini
, resides in specific locations based on your setup. You can find the correct php version as below:
- Apache:/etc/php/X.X/apache/php.ini
- CLI:/etc/php/X.X/cli/php.ini
- FPM:/etc/php/X.X/fpm/php.ini
Remember to replace ‘X.X’ with your PHP version. FPM is generally used by Nginx and sometime with Apache in case of application that using non-default PHP versions.
Conclusion
You’ve now successfully installed PHP on Ubuntu 24.04. Whether you’re developing a new project or running a server, having PHP properly installed and configured is essential. Always ensure to keep your PHP installation updated and secure by regularly checking for updates and applying security patches as they become available. Happy coding!
2 Comments
Clearly you never tested this as ondrej’s repo doesn’t actually support php 7.4 on Ubuntu 24.04 yet.
Hi Steve, PHP 7.4 packages are available for Ubuntu 24.04. You can check here: https://launchpad.net/~ondrej/+archive/ubuntu/php?field.series_filter=noble