Install PHP 8.1 alongside PHP 8.3 on your Ubuntu system

Install PHP 8.1 alongside PHP 8.3 on your Ubuntu system

To install PHP 8.1 alongside PHP 8.3 on your Ubuntu system, follow these steps:

1. Add the PHP PPA

If you haven't already, add the ondrej/php repository, which contains multiple PHP versions:

sudo add-apt-repository ppa:ondrej/php
sudo apt update

2. Install PHP 8.1 and Required Extensions

Now you can install PHP 8.1 along with its common extensions:

sudo apt install php8.1 php8.1-fpm php8.1-cli php8.1-mysql php8.1-xml php8.1-mbstring php8.1-curl php8.1-zip

You can modify the extensions list based on your needs.

3. Switch Between PHP Versions (Optional)

To switch between different PHP versions (e.g., between PHP 8.3 and PHP 8.1 for the CLI), you can use the update-alternatives tool:

sudo update-alternatives --set php /usr/bin/php8.1

If you want to choose between multiple PHP versions interactively:

sudo update-alternatives --config php

You'll be prompted to choose which PHP version should be the default for the command line.

4. Configure Nginx or Apache for PHP 8.1

If you're using Nginx, ensure PHP 8.1-FPM is running and update your Nginx configuration to use PHP 8.1:

sudo systemctl restart php8.1-fpm

For Apache, you would need to enable PHP 8.1 and disable PHP 8.3:

sudo a2dismod php8.3
sudo a2enmod php8.1
sudo systemctl restart apache2

5. Verify the Installation

Finally, verify that both PHP 8.1 and PHP 8.3 are installed:

php -v   # Check which PHP version is currently active
php8.1 -v   # Check PHP 8.1 specifically
php8.3 -v   # Check PHP 8.3 specifically

This will ensure both PHP versions are available on your system. Let me know if you run into any issues!

Did you find this article valuable?

Support Mandeep Singh Blog by becoming a sponsor. Any amount is appreciated!