How to Install PHP on Ubuntu 22.04


PHP is a scripting language that remains one of the leading choices for web developers. With its compatibility features, one can run PHP on different operating systems including Ubuntu. PHP has the capability to work with databases such as MySQL and SQLite, as well as popular frameworks like Laravel. Furthermore, one of its notable benefits is that it is freely available.

In this article, we aim to enhance your understanding and skill in installing the latest available version of PHP on Ubuntu 22.04.

How to Install PHP on Ubuntu 22.04?

Following the instructions presented below will guide you through the process of installing PHP on your Ubuntu 22.04 system.

Step 1: Install PHP via “apt”

The command presented below will be used to install the PHP package and its associated files such as libraries, dependencies, etc:

sudo apt install php -y

As shown above, the PHP package and its required libraries have been successfully installed on your Ubuntu 22.04 system.

Step 2: Update Ubuntu Machine Packages

Once you have completed the PHP installation, run the below command to update your system repository with the newest installed packages:

sudo apt update

Step 3: Upgrade System Packages

You can also run the below-given command if you want to upgrade the system packages with the latest one:

sudo apt upgrade -y

Based on the screenshots attached above shows that your Ubuntu is updated to the latest versions.

Step 4: Display PHP Version

The following command is the most common for confirming and verifying the installed package and its version:

php --version

Step 5: PHP Testing

To test the PHP, you can either write a script in your desired code editor and save it with “.php” extension or you can try the terminal directly such as a simple script given below:

php -a

Started your PHP shell with “php -a” and now you’re allowed to type your PHP script to run line by line:

To display the PHP version:

echo PHP_VERSION;

You can also display any text message such as a greeting message:

echo "Welcome to PHP!";

Type “exit” to end the PHP shell:

exit

How to Uninstall PHP from Ubuntu 22.04?

The following instructions will guide you in removing the PHP and its associated files from your operating system.

Step 1: Delete PHP

You can remove PHP from your Ubuntu 22.04 at any time by using the command provided below:

sudo apt purge php* -y

It can be seen in the above snapshot, the command removed the PHP package and its associated files from your Ubuntu machine.

Step 2: Remove PHP Directories

To remove the other remaining empty directories associated with PHP, run the command from your terminal:

sudo rm -rf /etc/php*

The above empty output indicates that the PHP-related files and directories no longer exist in your Ubuntu 22.04.

Conclusion

One can install and run PHP on Ubuntu 22.04. To install the PHP package the “sudo apt install php -y” will be used in the terminal. You can run the PHP script from both “*.php” or directly from the terminal. PHP is a scripting language popular for its speed and flexibility, used by developers worldwide. This post has enhanced your understanding to install, run and remove PHP from your Ubuntu 22.04 system.

Print Friendly, PDF & Email
Categories