Managing multiple PHP versions on a single server is essential when working on different projects requiring different PHP environments. In this article, we’ll walk you through a detailed step-by-step guide on how to install multiple PHP versions on Ubuntu 22.04. We’ll use the widely trusted Ondrej PPA repository to make this process straightforward and efficient.
By following this tutorial, you’ll be able to install, configure, and switch between PHP versions seamlessly on your system. This guide also works for Ubuntu 20.04 and 18.04.
How to Install Multiple PHP Versions on Ubuntu 22.04
Step 1: Update the System
Before installing any software, it’s a best practice to update your system to ensure that you’re working with the latest package versions. Open a terminal and run the following commands:
sudo apt-get update
sudo apt-get upgrade
Step 2: Add the Ondrej PHP Repository
The Ondrej PPA is a popular repository that contains various PHP versions and related packages. To enable it, execute these commands:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Step 3: Install Multiple PHP Versions
With the Ondrej repository enabled, you can now install different PHP versions. Below are the commands for installing PHP 8.1, PHP 8.0, and PHP 7.4 along with their common extensions.
Install PHP 8.1
To install PHP 8.1, run:
sudo apt-get install php8.1 php8.1-fpm
sudo apt-get install php8.1-mysql php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl
Install PHP 8.0
To install PHP 8.0, execute:
sudo apt-get install php8.0 php8.0-fpm
sudo apt-get install php8.0-mysql php8.0-mbstring php8.0-xml php8.0-gd php8.0-curl
Install PHP 7.4
For PHP 7.4, use:
sudo apt-get install php7.4 php7.4-fpm
sudo apt-get install php7.4-mysql php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl
You can repeat this process for any other PHP versions you need, such as PHP 7.3 or PHP 5.6, by replacing the version number in the above commands.
Step 4: Check the Active PHP Version
To check the currently active PHP version, use:
php -v
This will display the default PHP version set for the CLI (Command Line Interface).
Step 5: Switch Between PHP Versions
Ubuntu allows you to switch the default PHP version using the update-alternatives
command. Run the following:
sudo update-alternatives --config php
You’ll see a menu listing all installed PHP versions. Enter the number corresponding to the version you want to set as the default and press ENTER.
Alternatively, to switch directly to a specific version, use:
sudo update-alternatives --set php /usr/bin/php7.4
Step 6: Configure PHP-FPM
If you’re using a web server like Apache or Nginx, you’ll need to configure it to use the appropriate PHP-FPM version. For example, in Nginx, edit your virtual host configuration to point to the desired PHP-FPM socket:
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
Restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 7: Uninstall PHP Versions
If you need to remove a specific PHP version, use the following command:
sudo apt-get remove php7.4
To remove all modules associated with that version, run:
sudo apt-get remove php7.4-*
Finally, clean up unused dependencies:
sudo apt-get autoremove
Conclusion
Installing and managing multiple PHP versions on Ubuntu 22.04 is straightforward when using the Ondrej PPA repository. Whether you’re a developer juggling projects with different PHP requirements or someone setting up a robust web development environment, this guide equips you to handle PHP versions efficiently.
Now that you know how to install multiple PHP versions on Ubuntu 22.04, go ahead and set up your system to meet your project’s needs!
Have any questions or face any challenges?
Drop them in the comments below!
FAQs: How to Install Multiple PHP Versions on Ubuntu 22.04
Why should I install multiple PHP versions on Ubuntu 22.04?
Some projects may require older or newer PHP versions due to compatibility with specific frameworks, plugins, or codebases. Installing multiple PHP versions allows you to work on these projects without compromising your system or other applications.
What is the Ondrej PPA, and why is it used?
The Ondrej PPA is a widely trusted Personal Package Archive that provides multiple versions of PHP and related packages for Ubuntu. It’s maintained by a reputable developer and allows easy access to up-to-date PHP versions, including legacy ones.
How do I check which PHP versions are installed on my system?
To list all installed PHP versions, use:
ls /usr/bin/php*
To check the active version for CLI, use:
php -v
Can I run different PHP versions for different projects?
Yes! By configuring PHP-FPM with your web server (e.g., Nginx or Apache), you can assign different PHP versions to specific websites or virtual hosts.
For example, in Nginx, you can configure one site to use PHP 8.1 and another to use PHP 7.4 by updating the fastcgi_pass
directive in their respective configurations.
How do I switch the default PHP version for the CLI?
To switch the PHP version for CLI, run:
sudo update-alternatives --config php
Select the desired PHP version from the list by entering the corresponding number.
What happens if I install conflicting PHP modules?
Ubuntu uses the update-alternatives
system to manage multiple PHP installations. This ensures that conflicting modules (e.g., PHP extensions) are handled properly and don’t interfere with each other.
Can I install PHP versions older than 7.0 on Ubuntu 22.04?
Yes, the Ondrej PPA supports legacy PHP versions, including PHP 5.6. However, keep in mind that older versions like PHP 5.6 and PHP 7.0 are no longer supported by the PHP community and may pose security risks.
How do I uninstall a PHP version I no longer need?
To uninstall a PHP version, use:
sudo apt-get remove php7.4
sudo apt-get remove php7.4-*
sudo apt-get autoremove
This will remove the specified version and its extensions.
Can I automate switching between PHP versions for different projects?
Yes, you can use tools like phpenv or Docker containers for automation. These tools allow you to quickly set up and switch between different PHP environments without manually adjusting your system configurations.
Do I need to restart my web server after changing the PHP version?
Yes, if you’re using PHP-FPM with a web server like Nginx or Apache, you need to restart the server to apply the new configuration. For example:
sudo systemctl restart nginx
Or
sudo systemctl restart apache2
Are there any risks of using multiple PHP versions?
The primary risk is misconfiguration, which can lead to compatibility or performance issues. To mitigate this, always ensure your web server points to the correct PHP-FPM socket or binary for each project.
Can I use this guide for other Ubuntu versions like 20.04 or 18.04?
Yes, this guide is compatible with Ubuntu 20.04 and 18.04. The commands and processes remain the same.
How do I ensure PHP is properly configured after installation?
You can verify PHP configuration using:
php -i | grep "Loaded Configuration File"
Additionally, create a phpinfo()
file in your web server’s root directory to display all PHP settings in your browser.
How do I test the PHP version being used by my web server?
Create a file named info.php
in your web server’s document root with the following content:
<?php
phpinfo();
?>
Access it in your browser (e.g., http://your-server-ip/info.php
) to see the PHP version and other details.
Is this guide applicable to systems other than Ubuntu?
The specific commands and repository mentioned in this guide are tailored for Ubuntu-based distributions. For other Linux distributions like CentOS, Fedora, or Arch Linux, you’ll need to follow their respective methods to manage multiple PHP versions.