In this tutorial, you will learn how to upgrade and switch PHP to a new version and make it work with 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.3), 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.3 and necessary modules as per your need using this command:

> sudo apt install php7.3 libapache2-mod-php7.3 php7.3-common php7.3-mbstring php7.3-xmlrpc php7.3-soap php-apcu php-smbclient php7.3-ldap php-redis php7.3-gd php7.3-xml php7.3-intl php7.3-json php-imagick php7.3-mysql php7.3-cli php7.3-ldap php7.3-zip php7.3-curl

If you want to configure PHP as per your need, then you will need to edit the configuration file which is location at “/etc/php/7.3/apache2/php.ini” for version 7.3. Also, you can find and open the configuration file for version 7.3 in nano editor using the command:

> sudo nano /etc/php/7.3/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.3 on your system. Currently, if 7.0 is set to default version on your system, then you can switch from version 7.0 to 7.3 using these commands:

Command Line:

> sudo update-alternatives --set php /usr/bin/php7.3
> sudo update-alternatives --set phar /usr/bin/phar7.3
> sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.3
> sudo update-alternatives --set phpize /usr/bin/phpize7.3
> sudo update-alternatives --set php-config /usr/bin/php-config7.3

Apache:

> sudo a2dismod php7.0
> sudo a2enmod php7.3

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.3, you can do the following commands:

Command Line:

> 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

Apache:

> sudo a2dismod php7.3
> 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 php-apcu php-smbclient php7.0-ldap php-redis php7.0-gd php7.0-xml php7.0-intl php7.0-json php-imagick php7.0-mysql php7.0-cli php7.0-ldap php7.0-zip php7.0-curl

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.

Leave a comment

Your email address will not be published. Required fields are marked *