How to install PHP on Ubuntu

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to install PHP on Ubuntu.

PHP is one of the most popular programming languages that is used and being used currently by a lot of companies and developers.

Even though there are a lot of programming languages that you can use to make web apps, and build backend sites, PHP is still one of the best options.

Installing PHP on your Ubuntu server is easy, as PHP is uploaded on Ubuntu repositories and it can be installed easily without having to add a repository on your system.

Step 1 - Update your server

Before installing PHP on your Ubuntu server, make sure that your server's packages are up to date. To check for updates, run the command below

sudo apt update

Once the update has been performed, on the output you will see if your system needs to update packages, to update packages that might be outdated, run the command below

sudo apt upgrade

Step 2 - Install PHP

To install the latest version of PHP on your Ubuntu server, you can do so by running the command below.

sudo apt install php

💡
Note: The command above will install only PHP on your server, which means if you are planning to use PHP with a webserver like Nginx or Apache you will also need to install the webserver you need after installing PHP.

Step 3 - Verify PHP Installation

Once PHP has been installed on your server, to check if PHP was installed correctly, you can check the version.

To do this, run the following command on your terminal

php -v

The command above will return php version that was installed.

Output

PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies

How to uninstall PHP

To uninstall PHP from your Ubuntu 22.04 machine, run the following command

sudo apt remove php --purge

The command above will remove and purge all config files from your system

Conclusion

By now you should know how to install PHP on Ubuntu.

In this tutorial, we installed PHP on Ubuntu via the latest package of PHP that is available on the Ubuntu repository. The ubuntu version that we used for this tutorial, is Ubuntu 22.04.