How to Switch PHP Version from 8.1 to 8.2 in Ubuntu
Learn how to upgrade and switch your PHP version from 8.1 to 8.2 in Ubuntu using simple commands.
Overview
If you have multiple PHP versions installed on Ubuntu, you can easily switch from PHP 8.1 to PHP 8.2 using update-alternatives and server configuration.
Step-by-Step Implementation
- Install PHP 8.2 (if not already installed).
- Switch PHP CLI version using update-alternatives.
- Update web server configuration (Apache/Nginx).
- Verify the PHP version.
Step 1: Install PHP 8.2
sudo apt update
sudo apt install php8.2 php8.2-cli php8.2-common php8.2-mysql php8.2-xml php8.2-curl php8.2-mbstring php8.2-zip
Step 2: Switch PHP CLI Version
sudo update-alternatives --config php
Select PHP 8.2 from the list.
Step 3: Switch PHP for Apache
sudo a2dismod php8.1
sudo a2enmod php8.2
sudo systemctl restart apache2
Step 4: Switch PHP for Nginx (PHP-FPM)
sudo systemctl stop php8.1-fpm
sudo systemctl start php8.2-fpm
Step 5: Verify PHP Version
php -v
Important Notes
- Ensure all required PHP extensions are installed for PHP 8.2.
- Restart your web server after switching versions.
- Check project compatibility (e.g., Magento 2 supports specific PHP versions).
- If using Nginx, update the socket path in config (e.g.,
php8.2-fpm.sock).