Install php alternatives

From www.deloptes.org
Jump to navigation Jump to search

How To Install PHP (8.2, 8.1 or 7.4) on Debian 12

By RahulDecember 1, 20233 Mins Read [[1]]

In this article, we will provide a step-by-step guide on how to install PHP on a Debian 12 system. We will specifically look into two different versions – PHP 8.2 and PHP 7.4, providing you with the flexibility to choose the version that suits your needs. PHP is a popular server-side scripting language often used for web development, which makes it an important installation for any web server.

Before you proceed, it’s crucial to ensure that your system is up-to-date with the latest security patches and software updates. Run the following commands:

sudo apt update 
sudo apt upgrade 

Add the SURY PHP PPA repository

The default Debian 12 repositories contains PHP 8.2 and PHP 7.4 packages, but not other versions like PHP 8.1, 7.3, 7.2 or 5.6. So we recommend to add a third-party repository, Ondřej Surý’s PHP repository, which provides up-to-date PHP packages. Run the following commands to add the repository:

sudo apt install -y apt-transport-https lsb-release ca-certificates wget 
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list 

Then update the system to reflect the addition of the new repository.

sudo apt update 

Installing PHP on Debian 12

As you have configured the required PPA on your system. Let’s install the required PHP version.

  • Install PHP 8.2

Now, install PHP 8.2 by running the following command:

   sudo apt install -y php8.2 
  • Install PHP 8.1

To install PHP 8.1, execute the following command:

   sudo apt install -y php8.1 
  • Install PHP 7.4

For some legacy applications, you may need an older version like PHP 7.4. Now you can install PHP 7.4 using the following command:

   sudo apt install -y php7.4 

Verify the Installation

You can check the installation using the same method as above:

php -v 

This should display the version of PHP that’s currently active on your system. If PHP 7.4 is correctly installed, it will reflect here. Switching Between PHP Versions

If you have multiple versions of PHP installed, you can switch between them using the update-alternatives command. For example, to switch to PHP 7.4, you would use:

sudo update-alternatives --set php /usr/bin/php7.4 

To switch back to PHP 8.2, replace php7.4 with php8.2.

Conclusion

That’s it! You now know how to install PHP 8.2 and PHP 7.4 on a Debian 12 system, and how to switch between the two versions. Remember, the version of PHP your server should run depends on your specific requirements and the compatibility of the web applications you plan to host. Always ensure to use supported and up-to-date versions of PHP for the best security and performance.