Check and update PHP in Ubuntu:

Install And Update PHP In Ubuntu

PHP is a necessary skill for web developers as it stands out as the most used server-scripting language at the present time. In this article, we will be covering how to check the currently installed version on your ubuntu machine and install another version.

Also read: How to Install and Secure phpMyAdmin on Ubuntu 18.04?

Check the current version on your Ubuntu machine

To check the currently installed version of PHP on your machine type in the following code in your terminal:

php -v

If PHP is installed on your machine then it’ll return a version and if not it’ll return a statement that there is no package installed named PHP. An alternative command will be the which command:

which php

If you don’t have PHP installed on your machine, follow along with us to install it!

Install PHP in Ubuntu

First and foremost it is a good practice to update the machine before any installation. Copy paste the following code to do so:

sudo apt update && upgrade

To install PHP on your Ubuntu machine, run the following code in your Ubuntu machine:

sudo apt install software-properties-common

The above code will enable the PHP repository. You may need to press the y key to give permission for the installation. Now we will be adding the PHP repository with the following command:

sudo add-apt-repository ppa:ondrej/php

Press enter if prompted and you’re good to go. Now to finally install PHP issue the following command. In this instance we will be installing php8:

sudo apt install php8.0

You may need to press the y key for confirmation. It takes about 28.1 MB of space at the time of writing this article. Once the installation is done you can check with the help of the which command as we did before:

which php

You can also check the version by issuing the following command:

php --version

Install another version of PHP

You can update to the latest or even downgrade to your desired version of PHP anytime. Normally you can’t use the apt command simply to update or downgrade, but since we have added the PHP repo in our Ubuntu machine we can mention any version number and simple install that version of PHP on our machine. For instance, to download PHP version 7.3 we run the following command after we have added the PHP repo (see install section in this article):

sudo apt install php7.3

Change to a different version of PHP installed on the system

You can change over to any version of PHP in your machine and set that as your version. To do this, first, we will need to know the location of all the php installations, to do so run the following command:

which php

The above command will return to you the location of all the PHP installations. Now check the currently used version by simply using the –version flag with the PHP command. Now you can use the following command to check out the PHP installation location, in our case, the installation location was /usr/bin/php:

ls -la /usr/bin/php

In our case it is simply a link to the /etc/alternatives/php, now let us check out the new location then:

ls -la /etc/alternatives/php

You will probably see that this is yet another redirection to the actual file to the version of PHP, in our case this is /usr/bin/php8.0. Now if we run the ls -la command on the /usr/bin/php* we will see all the installations of php. This way you will be able to confirm which versions of PHP are currently installed on your system and then you can choose one amongst them to switch to:

update-alternatives --config php

You will be able to see and select all the currently installed versions of PHP in your system. After the change again check the version by using the php --version command.

Summary

So that’s it for this article, we hope this has helped you guys. For more such awesome articles keep checking out LinuxForDevices! And as always, thanks for reading.