In this tutorial, you will learn how to upgrade and switch PHP to a new version and make it work with the Apache server in Ubuntu 20.04. Once, you know how to install the newer version, you will also learn how to remove the older version.
Also, these same steps can be followed to upgrade PHP from any older version to a newer one by simply replacing the actual version mentioned with the version you wish to upgrade to. The process remains mostly similar across different versions of PHP.
First of all, you can check your current PHP version using this command:
> php -v
This will return your current version installed in your system. Now, to install a newer version (7.4), you will need to add a PPA repository, using the following commands:
> sudo apt-get install software-properties-common
> sudo add-apt-repository ppa:ondrej/php
> sudo apt update
The above commands will:
- Installs the “software-properties-common” package
- Adds the PPA repository
- Update the list of packages from all repositories and PPAs
Now, you can install version 7.4 and the necessary modules as per your need using this command:
> sudo apt install php7.4 libapache2-mod-php7.4 php7.4-common php7.4-mbstring php7.4-xmlrpc php7.4-soap php7.4-apcu php7.4-ldap php7.4-redis php7.4-gd php7.4-xml php7.4-intl php7.4-json php7.4-mysql php7.4-cli php7.4-ldap php7.4-zip php7.4-curl php7.4-bcmath php7.4-igbinary php7.4-imagick php7.4-opcache php7.4-readline
If you want to configure PHP as per your need, then you will need to edit the configuration file which is located at “/etc/php/7.4/apache2/php.ini” for version 7.4. Also, you can find and open the configuration file for version 7.4 in the nano editor using the command:
> sudo nano /etc/php/7.4/apache2/php.ini
Press Ctrl + X then Y (if using nano editor) to save the changes and exit from it.
Upgrading to PHP 8.0
If you want to upgrade to PHP 8.0 instead of PHP 7.4, then you can adjust the commands accordingly.
To install version 8.0 and the necessary modules, you can use this command:
> sudo apt install php8.0 libapache2-mod-php8.0 php8.0-common php8.0-mbstring php8.0-xmlrpc php8.0-soap php8.0-apcu php8.0-ldap php8.0-redis php8.0-gd php8.0-xml php8.0-intl php8.0-mysql php8.0-cli php8.0-ldap php8.0-zip php8.0-curl php8.0-bcmath php8.0-igbinary php8.0-imagick php8.0-opcache php8.0-readline
Note: You don’t need to install the “php8.0-json” module as it’s included by default in PHP 8.
Now, to access the configuration file for version 8.0, you can use the following command:
> sudo nano /etc/php/8.0/apache2/php.ini
In a similar way, you can install the later PHP versions like PHP 8.1, PHP 8.2, or PHP 8.3.
Switching PHP from an older to a newer version
Suppose, you also have PHP 7.0 along with PHP 7.4 on your system. Currently, if 7.0 is set to the default version on your system, then you can switch from version 7.0 to 7.4 using these commands:
To activate the newer version of PHP for your command line:
> sudo update-alternatives --set php /usr/bin/php7.4
> sudo update-alternatives --set phar /usr/bin/phar7.4
> sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4
> sudo update-alternatives --set phpize /usr/bin/phpize7.4
> sudo update-alternatives --set php-config /usr/bin/php-config7.4
Now, you can check if the newer version is installed properly for your command line:
> php -v
To activate the newer version of PHP for the Apache server:
> sudo a2dismod php7.0
> sudo a2enmod php7.4
Now, you need to restart the apache2 server for changes to take an effect.
> sudo service apache2 restart
If you want to switch back to version 7.0 from 7.4, you can do the following commands:
> sudo update-alternatives --set php /usr/bin/php7.0
> sudo update-alternatives --set phar /usr/bin/phar7.0
> sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.0
> sudo update-alternatives --set phpize /usr/bin/phpize7.0
> sudo update-alternatives --set php-config /usr/bin/php-config7.0
> sudo a2dismod php7.4
> sudo a2enmod php7.0
> sudo service apache2 restart
Removing the old PHP version
If you are sure that you have installed the newer version and it works correctly, then you may want to remove the older version. To remove version 7.0, you can use the following command:
> sudo apt-get purge php7.0-common
Also, you can remove it along with its modules using the command:
> sudo apt-get purge php7.0 libapache2-mod-php7.0 php7.0-common php7.0-mbstring php7.0-xmlrpc php7.0-soap php7.0-apcu php7.0-ldap php7.0-redis php7.0-gd php7.0-xml php7.0-intl php7.0-json php7.0-mysql php7.0-cli php7.0-ldap php7.0-zip php7.0-curl php7.0-bcmath php7.0-igbinary php7.0-imagick php7.0-opcache php7.0-readline
Now, you know how to install and switch to the latest version. It’s better to have the latest version of PHP installed on your server with new improvements and features. To find out the latest release, you can refer to the official website.