How to Install and Uninstall PHP on Ubuntu (20.04/22.04/24.04)
- by Lisa P
- in Ubuntu
- View 6018
- Date 01 Jun, 24
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.
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, make sure to Check Ubuntu Version before installation to avoid compatibility issues.
๐ Installing PHP on Ubuntu
Installing PHP on Ubuntu can be done using the default repositories or by adding external sources for more control over versions. The approach you choose depends on whether you need the latest stable release, an older compatible version, or multiple PHP versions side by side.
Ubuntu’s package manager makes it straightforward to install, update, and maintain PHP packages. In this section, we’ll walk through installing the default PHP version, adding a PPA for more flexibility, and installing a specific PHP release such as PHP 8.1.
Before diving into installation and configuration steps, take a moment to review What is PHP to understand its role in backend development and why it remains essential for modern web applications.
๐ฆ Install Default PHP Version
Ubuntu ships with a default PHP version that is tested and supported for that specific release. Installing it is the fastest and simplest option, especially for beginners or production servers that prioritize stability.
After updating the package index, a single command installs PHP along with the PHP CLI. This method is commonly used for quick setups and basic development environments. It’s also the recommended approach if you don’t need to switch PHP versions Ubuntu later.
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.
โ Add PPA for Other PHP Versions
If the default PHP version doesn’t meet your project requirements, adding a PPA is the best solution. The widely used ppa:ondrej/php repository provides access to multiple PHP versions, including older and newer releases.
This is essential when working on legacy applications or testing compatibility across versions. After adding the PPA and updating package lists, Ubuntu can recognize and install multiple PHP packages. Using a PPA gives you long-term flexibility and finer control over PHP version management.
๐ฏ Install Specific PHP Version (e.g., PHP 8.1)
Installing a specific PHP version allows you to match your application’s exact requirements. This is especially useful when deploying frameworks or CMS platforms that depend on a particular PHP release. You can install PHP 8.1 (or any supported version) alongside others without conflicts.
This approach is common in professional environments where developers need consistent behavior across staging and production. It also lays the groundwork for safely testing upgrades and rolling back if needed.
๐ Switching Between PHP Versions
When multiple PHP versions are installed on the same system, you need a reliable way to choose which one is active. Ubuntu provides built-in tools that let you switch PHP versions Ubuntu safely without uninstalling any packages.
- Multiple PHP versions can coexist on one Ubuntu server
- Version switching is useful for testing, development, and legacy applications
- CLI and web server PHP versions can be managed independently
After installing or switching PHP versions, it’s recommended to verify the active version by following this guide on Checking PHP Version to ensure the correct PHP release is in use.
๐งญ Using update-alternatives
The update-alternatives system allows you to select which PHP version is used by default in the command line. This is essential when you have installed PHP through the official repositories or via the ondrej/php PPA.
- Lists all installed PHP versions available for selection
- Let you choose the default PHP binary for CLI usage
- Helps avoid conflicts when running composer, artisan, or custom scripts
- Works seamlessly alongside Apache or PHP-FPM setups
Once configured, update-alternatives ensures your Ubuntu system consistently uses the PHP version you intend.
๐งฉ Installing PHP Extensions
PHP extensions add essential functionality such as database connectivity, image processing, caching, and API communication. Ubuntu makes it easy to install extensions using the package manager, with each module matched to a specific PHP version.
Installing 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.
โ๏ธ Configuring PHP Settings
After installing PHP, fine-tuning its configuration is crucial for performance, security, and application stability. PHP behavior is controlled through configuration files that differ based on the PHP version and execution mode. Proper configuration helps prevent common issues such as memory limits, upload errors, and slow execution.
๐ Locate and Edit php.ini
PHP uses different php.ini files depending on whether it runs via CLI, Apache, or PHP-FPM. Knowing the correct file location ensures your changes apply to the intended environment.
- Apache: /etc/php/8.x/apache2/php.ini
- PHP-FPM: /etc/php/8.x/fpm/php.ini
- CLI: /etc/php/8.x/cli/php.ini
- Version number (8.x) changes based on the installed PHP release
Editing the correct php.ini file prevents configuration conflicts between web and command-line usage.
๐ ๏ธ Common Directives to Change
Several PHP directives are frequently adjusted to match application requirements and server capacity. These settings directly impact file uploads, memory usage, and script execution time.
- memory_limit to control maximum RAM usage per script
- upload_max_filesize to allow larger file uploads
- post_max_size to handle large form submissions
- max_execution_time to prevent long-running scripts
Carefully adjusting these directives helps optimize PHP performance without compromising system stability.
๐ 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 Apache PHP module that matches your PHP version
- Enable the PHP module and required dependencies
- Restart Apache to apply configuration changes
- Confirm Apache is loading the correct PHP version
With Apache correctly configured, PHP scripts are processed natively and reliably for most use cases.
๐ 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 PHP-FPM package for your PHP version
- Configure Nginx to pass .php requests to PHP-FPM
- Ensure the socket or TCP port matches the PHP-FPM service
- Restart both Nginx and PHP-FPM after configuration
When properly configured, Nginx with PHP-FPM delivers excellent performance and scalability.
โ 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 and Test info.php File
The phpinfo() function provides a complete overview of your PHP environment. Viewing this output in a browser confirms both PHP execution and web server integration.
- Create a PHP file in the web root directory
- Add the phpinfo() function to the file
- Access the file through your browser
- Confirm PHP version, modules, and configuration details
If the page loads correctly, your PHP installation is functioning properly.
๐งน How to Uninstall PHP
Removing PHP is sometimes necessary when switching stacks, troubleshooting issues, or cleaning unused software. Ubuntu allows safe removal while keeping the system stable. A clean uninstall prevents leftover files from causing conflicts later.
๐๏ธ Remove PHP Packages
The first step is removing PHP packages installed via the package manager. Purging ensures both binaries and system-level configuration files are deleted.
- Remove the core PHP package for the installed version
- Uninstall PHP-FPM or Apache PHP modules if present
- Use purge options to remove package configurations
- Restart the web server after removal
Proper package removal ensures PHP is no longer active on the system.
๐งผ Clean Residual Files
Even after uninstalling packages, some configuration files may remain on disk. Cleaning these files helps avoid issues if PHP is reinstalled later.
- Delete version-specific directories under /etc/php/
- Run autoremove to clear unused dependencies
- Verify PHP is no longer available via the CLI
- Check web server configs for leftover PHP references
Once cleanup is complete, your Ubuntu system is fully cleared of PHP-related components.
๐ ๏ธ Common Issues and Troubleshooting
Even with a correct setup, PHP installations on Ubuntu can run into issues related to versions, modules, or web server integration. Understanding the most common problems and their fixes helps you resolve errors quickly without reinstalling the entire stack.
|
Issue |
Cause |
Solution |
|
PHP version mismatch |
Multiple PHP versions installed |
Use update-alternatives to select the correct version |
|
PHP is not executing in the browser |
Web server not linked to PHP |
Verify PHP for Apache Ubuntu or PHP-FPM configuration |
|
Missing PHP modules |
Extension not installed |
Install the required phpX.X-module package |
|
Command not found: php |
PHP CLI not installed |
Install php-cli for the active PHP version |
|
PPA errors during install |
Outdated package cache |
Run sudo apt update after adding the PPA |
By addressing these common issues methodically, you can keep your PHP environment stable and avoid unnecessary downtime.
๐ 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.
Category: Ubuntu