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 18.10. Once, you know how to install the newer version, you will also learn how to remove the older version.
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 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 PPA’s
Now, you can install version 7.4 and 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 php-apcu php-smbclient php7.4-ldap php-redis php7.4-gd php7.4-xml php7.4-intl php7.4-json php-imagick php7.4-mysql php7.4-cli php7.4-ldap php7.4-zip php7.4-curl php7.4-bcmath
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 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.
Switching PHP from older version 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 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 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 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-ldap 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
Now, you know how to installed 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.