How To Install & Uninstall PHP on Ubuntu: 2 Easy Methods

install php on ubuntu

PHP (short for Hypertext Preprocessor) is a server-side scripting language widely used for task automation and building dynamic web applications and websites. Its versatility, ease of use, and community support make it a popular choice in web development.

Ubuntu 20.04 and 22.04 are long-term support (LTS) versions of the open-source Ubuntu operating system. Ubuntu is a popular choice for server OS because of its consistent performance and extended vendor support.

Installing PHP on Ubuntu 20.04 and 22.04 is straightforward and allows you to set up PHP as part of your server software stack. 

In this tutorial, we will discuss how to install and uninstall PHP on Ubuntu. We will use the command-line interface (CLI) to install PHP because of the efficiency and convenience of the approach.

Table Of Contents

  1. A Note on PHP Versions in Ubuntu
  2. How to Install PHP on Ubuntu
  3. Install PHP Modules on Ubuntu
  4. Uninstall PHP on Ubuntu
  5. Conclusion
  6. FAQs

A Note on PHP Versions in Ubuntu

PHP offers two current versions, 7.4 and 8.1. These versions are well-supported and developers install it as a critical component of LAMP stacks that power business applications and operations. 

In an Ubuntu environment, we’ll use the following command to install PHP using the APT package manager:

# sudo apt install php

Depending upon the Ubuntu version, this command installs the following PHP versions:

  • In Ubuntu 20.04, this command installs PHP 7.4. 
  • In Ubuntu 22.04, it installs PHP version 8.1.

How to Install PHP on Ubuntu

Installing PHP on Ubuntu is a critical step in setting up business applications on your server. We’ll discuss two methods of installing PHP and then go into the details of setting up additional PHP modules. We’ll round off by showing you how to uninstall PHP from your Ubuntu server. 

Let’s start with the prerequisites.


Before proceeding with the methods outlined below, ensure you have the following:

  • A Linux environment running Ubuntu 20.04 or 22.04
  • A user account with root or sudo privileges
  • Access to the command line or terminal
  • A properly configured web server, such as Apache or NGINX, on the server

Method #1: Install PHP with Apache on Ubuntu

Start by upgrading the server’s package index with the following command: 

# sudo apt update && sudo apt upgrade

Next, install the prerequisites for working with PPA packages.

# sudo apt install software-properties-common

Now that your server is ready, add the PHP PPA with the following command:

# sudo add-apt-repository ppa:ondrej/php

Remember to update the package index again to ensure you’re working with the latest package versions:

# sudo apt update

We are now ready to install Apache and PHP on the Ubuntu server. 

Install Apache and PHP 7.4

Run the following command to install Apache and PHP 7.4:

# sudo apt install apache2 php7.4 libapache2-mod-php7.4

Install Apache and PHP 8.1

Run the following command to install Apache and PHP 8.1:

# sudo apt install apache2 php8.1 libapache2-mod-php8.1

Once the process finishes, remember to restart Apache:

# sudo systemctl restart apache2

Verify PHP Installation

You can easily verify PHP installation by printing out the version information in the terminal:

# php -v

If you have installed version 7.4, you’ll see something like the following:

php -v

Alternatively, if you have installed version 8.1, the version information will show:

php -v 2

We recommend using the phpinfo() function to show more information about the PHP installation. For this, run the following statement in the terminal:

# echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php

Open your browser and navigate to http://<your_server_ip>/phpinfo.php to see PHP information.

Method #2: Install PHP with NGINX on Ubuntu

Unlike Apache, NGINX does not inherently support PHP processing, and thus, the installation process will include additional steps.

Start by upgrading the system package index:

# sudo apt update && sudo apt upgrade

Next, install the prerequisites for working with PPA packages:

#sudo apt install software-properties-common

Import the PHP PPA into the system’s package index:

# sudo add-apt-repository ppa:ondrej/php

Update the package list with the following command:

# sudo apt update

Install NGINX and PHP 7.4

Run the following command to install NGINX and PHP 7.4 on Ubuntu: 

# sudo apt install nginx php7.4-fpm

Install NGINX and PHP 8.1

Run the following command to install NGINX and PHP 8.1:

# sudo apt install nginx php8.1-fpm

apt install php7

Configure NGINX to use PHP-FPM

When the installation finishes, you need to edit the default NGINX configuration file in your preferred editor. We’ll run the following command to edit this file in nano:

# sudo nano /etc/nginx/sites-available/default

In this file, find the ~ \.php$ block and uncomment it to enable the use of PHP-FPM with NGINX.

Exit the editor and restart the NGINX service:

# sudo systemctl restart nginx

Next, restart the PHP-FPM service with the following command:

For PHP 7.4:

# sudo systemctl restart php7.4-fpm  

For PHP 8.1:

# sudo systemctl restart php8.1-fpm  

Verify PHP Installation

To verify that the PHP is working on the NGINX server, create a PHP test file:

# echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php

Navigate to http://<your_server_ip>/phpinfo.php in your browser to see PHP information.

Install PHP Modules on Ubuntu

PHP modules are extensions that extend the capabilities of the core PHP-FPM package. You can use the following command syntax to add these PHP modules to your Ubuntu system:

# sudo apt install php<version>-<module_name>

For instance, to install the POSIX module for PHP 8.1, you would use:

# sudo apt install php8.1-posix

If you need to install several modules simultaneously, you can do so in a single command. For instance, if you wish to install the mysql, zip, and bcmath modules for PHP 7.4, execute the following command:

# sudo apt install php7.4-mysql php7.4-zip php7.4-bcmath

You can view all active PHP modules on the system by running: 

# php -m

php -m

Uninstall PHP on Ubuntu

Follow these steps to uninstall PHP on Ubuntu.

Uninstall PHP 7.4 on Ubuntu

Start by running the following command in the terminal to remove the core PHP package:

# sudo apt remove --purge php7.4

Next, if you installed PHP-FPM, run the following command to uninstall it:

# sudo apt remove --purge php7.4-fpm

Finally, remove the PHP configuration files with the following command: 

#sudo rm -rf /etc/php/7.4

Initiate the uninstallation process by executing this command in the terminal to remove PHP packages.

Uninstall PHP 8.1 on Ubuntu

The process of uninstalling PHP 8.1 is similar to uninstalling PHP 7.4.

Start by removing the core PHP 8.1 package with the following command:

# sudo apt remove --purge php8.1

Next, remove PHP-FPM with the following command:

# sudo apt remove --purge php8.1-fpm

Finally, remove the PHP configuration files:

# sudo rm -rf /etc/php/8.1

Restart the Web Server

Once you have removed PHP files, you need to restart the web server to reflect these changes: 

If you’re using Apache, run the following command:

# sudo systemctl restart apache2


# sudo systemctl restart nginx

Verify PHP Removal

Verify that PHP is no longer available on the server by printing out the version information:

# php -v

This command should now return a command not found message, indicating that PHP has been successfully uninstalled.


You can install different PHP versions, including PHP 7.4, PHP 8.1, or any other specific version. We recommended two methods that utilize a good package manager for a quick and hassle-free installation. Additionally, we covered installing a web server, like Apache or Nginx, to serve PHP scripts on your Ubuntu machine.

RedSwitches helps customers by offering customizable bare metal servers

If you’re searching for a reliable server infrastructure for your projects, we provide competitive, dedicated server pricing and ensure swift delivery, typically on the same day of order approval. 

Whether you require a dedicated server, a traffic-friendly 10Gbps dedicated server, or a high-performance instant dedicated server, we are your trusted hosting partner!


Q. How can you install multiple PHP versions on Ubuntu 22.04?

The installation process involves adding the PHP repository, installing the desired PHP versions, updating PHP alternatives, configuring PHP extensions, and checking the current PHP version in use.

Q. How do I add the PHP repository for Ubuntu 22.04?

Utilize the command line to add the PHP repository, refresh the package list, and employ the apt package manager to install the preferred PHP version.

Q. What are the dependency packages required for installing multiple PHP versions?

You need to install the necessary dependency packages to support the installation of multiple PHP versions. This includes packages related to Apache, PHP modules, and other required dependencies.

Q: Is it possible to install the newest PHP version alongside the default PHP version?

Yes, you can install the latest PHP version alongside the default version by configuring PHP alternatives and selecting the preferred PHP version for your system.

Q. How can I configure the default PHP version on Ubuntu 22.04?

To set the default PHP version on Ubuntu 22.04, use the command sudo update-alternatives --set php /usr/bin/phpX.Y, replacing phpX.Y with your preferred PHP version, like php8.1. Verify the change with php -v. If the chosen version isn’t installed, install it using the package manager first.

Q. What are the standard PHP 7.4 extensions I might need to install?

Standard PHP 7.4 extensions include packages related to MySQL, XML, JSON, GD, and other commonly used PHP extensions required for web development and application deployment.

Q. What are the preferred PHP versions for Ubuntu 22.04?

The preferred PHP versions for Ubuntu 22.04 are PHP 7.4 and PHP 8.1. You can select the PHP version that best suits your application and development requirements.

Q. How do I install and configure PHP with Apache on Ubuntu 22.04?

You can install and configure PHP with Apache by installing the necessary Apache and PHP modules, configuring the PHP settings, and enabling PHP support in Apache.

Q. Can I run multiple PHP applications with different PHP versions on Ubuntu 22.04?

Yes, once you have installed multiple PHP versions and configured PHP alternatives, you can run multiple PHP applications and specify the PHP version for each application as needed.