Dynamic website and web application development starts from PHP, which serves as a fundamental technology, while Ubuntu stands as a leading platform for executing PHP-based projects. The guide provides essential information for users who need to install PHP on Ubuntu for local development purposes, configure PHP with Apache on Ubuntu or Nginx, manage different PHP versions, or uninstall PHP from their system.
This guide covers installing PHP on Ubuntu 24.04, 22.04 or 20.04, switching versions, restarting services and uninstalling PHP without breaking the rest of the server. The commands come first and explanations are kept short.
The article provides a comprehensive guide to PHP installation and configuration, along with version management and system cleanup procedures, which work on Ubuntu 20.04, 22.04, and 24.04 through the execution of practical commands, best practices, and troubleshooting techniques, enabling users to control PHP installations across all Ubuntu systems from their first stack setup to their advanced production environments. If you're new to the platform or want a clearer understanding of the operating system itself, refer to What is Ubuntu to learn how Ubuntu works and why it's widely used for server and development environments.
🧰 Prerequisites
Before starting the PHP setup process, it's important to make sure your system is properly prepared. Meeting these basic requirements helps avoid permission errors, package conflicts, and version mismatches during installation.
| Requirement | Description |
| Ubuntu OS | Ubuntu 20.04, 22.04, or 24.04 installed on a local machine or server |
| Sudo Access | A user account with sudo privileges to install and remove packages |
| Terminal | Access to the command line (Terminal or SSH) |
| Internet | Required to download packages from Ubuntu repositories or PPAs |
Since PHP package availability depends on your OS release, it's worth confirming which version of Ubuntu you're on before running any install commands — check it with lsb_release -a or cat /etc/os-release.
⚡ Quick Start: Install PHP on Ubuntu in 3 Commands
For most users, the default repo version is the right choice:
sudo apt update
sudo apt install php
php -vHere's a gotcha I've seen catch people on fresh VPSes. The php metapackage depends on a web server backend, so it may pull in Apache without you asking for it. If you're on Nginx, install the FPM stack instead:
# Apache path
sudo apt install php libapache2-mod-php
# Nginx path
sudo apt install php-fpm php-cli
📊 Default PHP Versions by Ubuntu Release
First, check your Ubuntu version with lsb_release -a. Each LTS release ships one PHP branch:
| Ubuntu release | Default PHP | Recommended path |
| Ubuntu 20.04 (Focal) | PHP 7.4 | Upgrade the OS, or use the PPA for PHP 8.x |
| Ubuntu 22.04 (Jammy) | PHP 8.1 | PPA if your app needs 8.2 or newer |
| Ubuntu 24.04 (Noble) | PHP 8.3 | Default repo works for most apps |
Launchpad lists Focal's php package as a dependency package that tracks the "latest stable PHP version (currently 7.4)". On packages.ubuntu.com, Jammy's default is 8.1 and Noble's is 8.3.
⚠️ Supported PHP Versions and End-of-Life Warning
Upstream PHP has already dropped the defaults on 20.04 and 22.04. As of March 2026, "only four PHP versions are actively supported: 8.2, 8.3, 8.4, and 8.5." PHP 8.2 reaches end of life on December 31, 2026. (Ubuntu still backports some security fixes to its own packages, but that's a different kind of support.) The official supported versions page always has the current dates.
So don't install PHP 7.x for a new project. If an old app really needs it, run it on an isolated server and plan the migration. If you need to verify what version you're actually running before upgrading, our guide on checking your PHP version walks through the WordPress and CLI methods. If you're setting up PHP for backend work and want the concepts first, our explainer on what PHP is covers its role in modern web development. If you're working across multiple environments or need PHP on a different platform, you can also follow How To Install PHP on Windows for a complete Windows-based setup guide.
🎯 When to Use Ubuntu Repos vs Ondřej PPA
Use the Ubuntu repo when its version meets your app's needs. You get updates through the normal apt upgrade cycle with nothing extra to manage. Use the PPA when your framework needs a newer branch, or when you need two versions installed side by side:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.4 php8.4-cli php8.4-fpmIs it safe? The PPA is run by a Debian PHP maintainer and is very widely used. It's still a third-party source, though, so only add it when you actually need it.
🧩 Install Common PHP Extensions for Real Projects
Here's a general web-app bundle that covers most CMSs and frameworks:
sudo apt install php-cli php-fpm php-mysql php-curl php-xml php-mbstring php-zip php-gdFor WordPress, add php-intl and php-imagick. If you're planning to host WordPress on the same server, our step-by-step guide on installing WordPress on Ubuntu picks up right where the PHP setup ends. For Laravel, add php-bcmath and php-intl, then install Composer. If you use PostgreSQL instead of MySQL, swap php-mysql for php-pgsql.
With PPA installs, put the version in the package name: php8.4-mbstring, php8.4-curl. Then check what's loaded:
php -mInstalling only the extensions your application needs helps keep the system lightweight and secure. After installing new extensions, restarting the web server or PHP-FPM ensures the changes are applied correctly.
🔍 CLI vs Web Server PHP: Why Versions Don't Match
A lot of people get confused here, and it's easy to see why. Ubuntu can run three separate PHP runtimes, and each one has its own php.ini:
- CLI:
/etc/php/8.3/cli/php.ini, used byphp -v, cron and Composer - Apache module:
/etc/php/8.3/apache2/php.ini - PHP-FPM:
/etc/php/8.3/fpm/php.ini, used by Nginx through FastCGI
If your terminal shows 8.4 and your site shows 8.3, nothing is broken. You've switched one runtime and not the other. The settings people usually need to change are memory_limit = 256M, upload_max_filesize = 64M, post_max_size = 64M and max_execution_time = 120. Edit the file for the right runtime, then restart that service.
🔄 How to Switch PHP for Apache vs Nginx
Switch the CLI with update-alternatives
sudo update-alternatives --config phpThis changes the terminal version only. It doesn't touch your website.
Switch the Apache module version
sudo a2dismod php8.3
sudo a2enmod php8.4
sudo systemctl restart apache2If you run Apache with PHP-FPM instead of mod_php, use a2disconf php8.3-fpm and a2enconf php8.4-fpm.
Switch the PHP-FPM socket for Nginx
Point your server block at the new socket:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.4-fpm.sock;
}Confirm the socket exists with ls /run/php/, then test and reload:
sudo nginx -t
sudo systemctl restart php8.4-fpm nginx🛠️ How to Restart PHP, Apache, and Nginx
How you restart PHP on Ubuntu depends on your setup. With mod_php, PHP runs inside Apache, so restarting Apache restarts PHP. With FPM, PHP runs as its own versioned service:
sudo systemctl restart php8.3-fpm
sudo systemctl restart apache2
sudo systemctl restart nginx
systemctl status php8.3-fpmNot sure what the service is called? Run systemctl list-units | grep fpm.
🌐 Setting Up PHP with Apache or Nginx
After installing PHP, it must be properly connected to a web server to process dynamic requests. Apache and Nginx handle PHP differently, so configuration steps vary based on the server you choose. Correct integration ensures stable performance, security, and predictable behavior in production.
PHP + Apache Integration
Apache supports PHP through a dedicated module, making integration straightforward and beginner-friendly. Once enabled, Apache can directly interpret PHP files without extra process managers. Install the matching libapache2-mod-php package, enable the module, and restart Apache. To confirm Apache is loading the correct PHP version, check with a quick phpinfo() test.
PHP + Nginx + PHP-FPM
Nginx does not execute PHP directly and relies on PHP-FPM for processing requests. This setup is highly efficient and commonly used in high-traffic or performance-sensitive environments. Install the matching phpX.X-fpm package, configure Nginx to pass .php requests to the FPM socket or TCP port, and restart both services after configuration. If you're building the full stack from scratch, our guide on installing LEMP on Ubuntu covers the complete Nginx + MySQL + PHP setup end to end.
✅ Verifying Installation
Verification confirms that PHP is installed, configured, and working as expected. Skipping this step can leave hidden configuration issues unnoticed. A simple test file is enough to validate the entire setup.
Create /var/www/html/info.php containing <?php phpinfo(); ?> and load it in the browser. It provides a complete overview of your PHP environment, confirms PHP execution, and shows the web server integration. Then delete it immediately — that page shows paths, module versions and environment details, which is exactly what an attacker wants to see.
sudo rm /var/www/html/info.phpAlso, stick to supported branches, install only the extensions you use, and keep web root files owned by a deploy user rather than www-data wherever you can.
🧹 How to Remove Only One PHP Version Without Breaking Others
This is the safest way to uninstall PHP on Ubuntu when other versions are installed:
sudo apt purge 'php8.2*'
sudo apt autoremoveremove leaves config files behind. purge deletes them too. Afterwards, confirm your other versions are still there with dpkg -l | grep php and systemctl list-units | grep fpm. If /etc/php/8.2/ is still around, remove it.
🗑️ How to Uninstall PHP on Ubuntu Completely
Only do this if you're getting rid of PHP entirely. The wildcard matches every package starting with "php", so read the list apt shows you before you confirm:
sudo apt purge 'php*' libapache2-mod-php*
sudo apt autoremove
sudo rm -rf /etc/php
php -vA "command not found" message means PHP is gone. Also run dpkg -l | grep php, which should return nothing.
🔧 How to Reinstall PHP on Ubuntu
If packages are broken (half-configured states, dpkg errors), this repair sequence usually fixes them:
sudo dpkg --configure -a
sudo apt purge 'php8.3*'
sudo apt clean && sudo apt update
sudo apt install php8.3 php8.3-cli php8.3-fpm php8.3-mysql🧰 Common Issues and Troubleshooting
| Issue | Likely cause | Fix |
| Browser downloads .php files | Apache module not enabled | a2enmod php8.3, restart Apache |
| 502 Bad Gateway on Nginx | Socket path mismatch | Match fastcgi_pass to /run/php/ |
| Wrong version in browser | Only the CLI was switched | Switch the Apache module or FPM socket |
| Call to undefined function | Missing extension | Install php8.x-name, restart |
| PPA 404 errors | Stale cache or unsupported release | apt clean && apt update, upgrade Ubuntu |
| Command not found: php | PHP CLI not installed | Install php-cli for the active PHP version |
By addressing these common issues methodically, you can keep your PHP environment stable and avoid unnecessary downtime.
🚀 Next Steps: Composer, Database Drivers, and App Setup
Once PHP works, install Composer for dependencies, add php-mysql or php-pgsql for your database, and finish the rest of your LAMP or LEMP stack. The Ubuntu Server PHP docs are worth bookmarking.
🏁 Conclusion
The process of handling PHP on Ubuntu becomes simple for users who learn the system's installation, configuration, version management, and cleanup procedures. The guide delivers a complete practical solution that covers all aspects of initial setup, web server integration, PHP version switching, and complete PHP uninstallation.
The steps help developers create a secure, clean environment that meets their needs for deploying production servers and maintaining legacy applications while they prepare for PHP installation on Ubuntu 24.04. For production deployments or scalable server setups, consider using an Ubuntu VPS from MonoVm to run PHP applications with full root access, better performance, and long-term stability. If your workflow needs a graphical interface on the same server — for example, to run GUI-driven development tools alongside your PHP stack — an Ubuntu RDP setup gives you that desktop layer without leaving the Linux environment.
People also read:
Hello, everyone, my name is Lisa. I'm a passionate electrical engineering student with a keen interest in technology. I'm fascinated by the intersection of engineering principles and technological advancements, and I'm eager to contribute to the field by applying my knowledge and skills to solve real-world problems.