How To Install PHP 7.4 on Ubuntu 20.04

Published on  

13 min read

How To Install PHP 7.4 on Ubuntu

Learn how to install PHP 7.4 on Ubuntu 20.04 with our comprehensive step-by-step guide. Discover the importance of PHP in web development and explore the features of PHP 7.4. Follow our instructions to update system packages, add the necessary repositories, and configure PHP to suit your needs.

Welcome to our comprehensive guide on installing PHP 7.4 on Ubuntu 20.04! If you're a web developer or someone interested in the world of web development, you've probably heard of PHP. PHP is a powerful scripting language that has played a significant role in the creation of dynamic and interactive websites.

In this article, we'll walk you through the step-by-step process of installing PHP 7.4 on your Ubuntu 20.04 server or virtual machine. PHP 7.4 comes with a range of exciting features and improvements, making it an excellent choice for your web development projects. So, whether you're starting a new project or upgrading your existing PHP version, this guide is here to help you.


Before we dive into the installation process, let's go over the prerequisites. To follow along with this guide, you'll need:

  1. Ubuntu 20.04 Server or Virtual Machine: Ensure that you have a server or virtual machine running Ubuntu 20.04. If you don't have one set up yet, you can refer to the official Ubuntu documentation for guidance.
  2. SSH Access to the Server: SSH access allows you to connect to your server remotely and execute commands. Make sure you have SSH access credentials handy.
  3. Basic Command-Line Knowledge: This guide assumes you have basic familiarity with the command-line interface (CLI) in Linux. Don't worry if you're new to it; we'll provide clear instructions to help you along the way.

Now that we have the prerequisites covered, let's move on to the step-by-step installation guide.

Note: Throughout this guide, we'll be using markdown formatting to enhance readability and provide better organization. We encourage you to click on the hyperlinks for additional resources and references as you follow along.

Let's get started with updating the system packages.

Step-by-Step Installation Guide

Update System Packages

Before we begin installing PHP 7.4, it's crucial to update the system packages on your Ubuntu 20.04 server. Updating the packages ensures that you have the latest bug fixes and security patches. To update the system packages, follow these steps:

Open your terminal or SSH client and connect to your Ubuntu 20.04 server using your SSH credentials.

Once connected, run the following command to update the package list:

sudo apt update

This command retrieves the latest information about available package updates.

After the package list is updated, you can upgrade the existing packages on your system using the following command:

sudo apt upgrade

The upgrade command installs the latest versions of the packages, including any necessary dependencies.

During the upgrade process, your system might prompt you to confirm any changes or ask for your permission to proceed. Enter Y or Yes when prompted to proceed with the upgrade.

Depending on the number of packages and their sizes, the upgrade process might take some time. Be patient and allow it to complete.

Congratulations! You have successfully updated the system packages on your Ubuntu 20.04 server. Now, let's move on to the next step: adding the PHP repository.

Add PHP Repository

Before we start, we can install software-properties-common, which adds management for additional software sources:

sudo apt -y install software-properties-common

To install PHP 7.4 on Ubuntu 20.04, we'll utilize a third-party repository called ondrej/php. This repository provides up-to-date PHP packages. Here's how you can add the PHP repository:

In your terminal or SSH client, run the following command to add the repository:

sudo add-apt-repository ppa:ondrej/php

This command adds the ondrej/php repository to your system's package sources.

After running the command, you may be prompted to confirm the action by pressing Enter. Simply press Enter to proceed. Next, your system will update the package list to include the newly added repository. This process might take a few moments.

Fantastic! You've successfully added the ondrej/php repository to your Ubuntu 20.04 system. In the next step, we'll install PHP 7.4 and its related packages.

Finally, you update apt-get again so your package manager can see the newly listed packages:

sudo apt-get update

Install PHP 7.4

With the PHP repository added to your Ubuntu 20.04 system, we can now proceed with the installation of PHP 7.4 and its associated packages. Follow the steps below to install PHP 7.4:

Open your terminal or SSH client, and enter the following command:

sudo apt install php7.4

This command tells Ubuntu's package manager to install PHP 7.4.

During the installation, your system will display a list of packages that will be installed along with PHP 7.4. Press 'Y' or 'Yes' when prompted to confirm and proceed with the installation.

The package manager will download and install PHP 7.4 and its dependencies. The process may take a few minutes, depending on your internet connection and system resources.

Once the installation is complete, you can verify the installation by running the following command:

php -v

This command will display the PHP version installed on your system. You should see the PHP 7.4 version information.

Congratulations! You have successfully installed PHP 7.4 on your Ubuntu 20.04 system. In the next step, we'll explore how to configure PHP 7.4 to suit your specific needs.

Install PHP 7.4 Modules and Packages

Besides PHP itself, you will likely want to install some additional PHP modules. You can use this command to install additional modules, replacing PACKAGE_NAME with the package you wish to install:

sudo apt-get install php7.4-PACKAGE_NAME

You can also install more than one package at a time. Here are a few suggestions of the most common modules you will most likely want to install:

sudo apt-get install -y php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath

This command will install the following modules:

  • php7.4-cli - command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasks
  • php7.4-json - for working with JSON data
  • php7.4-common - documentation, examples, and common modules for PHP
  • php7.4-mysql - for working with MySQL databases
  • php7.4-zip - for working with compressed files
  • php7.4-gd - for working with images
  • php7.4-mbstring - used to manage non-ASCII strings
  • php7.4-curl - lets you make HTTP requests in PHP
  • php7.4-xml - for working with XML data
  • php7.4-bcmath - used when working with precision floats

PHP configurations related to Apache are stored in /etc/php/7.4/apache2/php.ini. You can list all loaded PHP modules with the following command:

php -m

You have installed PHP and verified the version you have running. You also installed any required PHP modules and were able to list the modules that you have loaded.

Configure PHP 7.4

Configuring PHP 7.4 allows you to customize various settings and options based on your requirements. The configuration file for PHP is typically located at /etc/php/7.4/cli/php.ini for the command-line interface (CLI) version of PHP. Follow these steps to edit the PHP configuration file:

Open the PHP configuration file using your preferred text editor. For example, you can use the nano editor by running the following command:

sudo nano /etc/php/7.4/cli/php.ini

This command opens the PHP configuration file in the nano editor with administrative privileges.

Within the configuration file, you'll find various settings and options that control the behavior of PHP. Modify these settings according to your requirements. Some common settings you may want to adjust include max_execution_time, memory_limit, and error_reporting. Refer to the PHP documentation or online resources for more information on each setting.

After making the necessary changes, save the file and exit the text editor. In nano, you can press Ctrl + X, followed by Y to save the changes.

Great job! You've now configured PHP 7.4 to suit your specific needs. In the next step, we'll test the PHP installation to ensure everything is working correctly.

Test PHP Installation

Testing the PHP installation is an essential step to verify that PHP 7.4 is functioning as expected on your Ubuntu 20.04 system. Here's a simple way to test PHP:

Create a new PHP test file using your preferred text editor. For example:

sudo nano /var/www/html/test.php

In the newly created file, add the following line of code:

<?php phpinfo(); ?>

This code will display the PHP configuration information when accessed through a web browser. Save the file and exit the text editor.

Open a web browser and navigate to http://<your-server-IP>/test.php, replacing <your-server-IP> with the actual IP address or domain name of your server.

If you're running Ubuntu 20.04 locally on your machine, you can use http://localhost/test.php or as the URL.

If PHP is correctly installed and configured, you will see a page containing detailed information about your PHP installation. This confirms that PHP 7.4 is working correctly on your Ubuntu 20.04 system.

Congratulations! You have successfully installed and tested PHP 7.4 on your Ubuntu 20.04 system. In the next section, we will address common issues that readers may encounter during the installation process and provide possible solutions or workarounds.


During the installation process, you may encounter certain issues or face challenges. Don't worry; we're here to help! In this section, we'll address some common problems that readers may encounter while installing PHP 7.4 on Ubuntu 20.04. We'll also provide possible solutions or workarounds to help you overcome these hurdles. Let's dive in:

  1. Package Not Found: If you receive an error stating that the PHP 7.4 package is not found, it's likely that the ondrej/php repository was not added correctly. Double-check the steps in Section B to ensure you followed them accurately. You can also try running the command sudo apt update again to refresh the package list.
  2. Dependency Errors: Sometimes, the installation process may fail due to unmet dependencies. In such cases, you can try running sudo apt --fix-broken install to resolve the dependency issues and resume the installation.
  3. PHP Not Working: If you encounter issues where PHP scripts are not executing or displaying correctly in the web browser, ensure that you have restarted the necessary services after the installation. You can restart the Apache or Nginx web server using commands like sudo service apache2 restart or sudo service nginx restart.
  4. Incorrect PHP Version: If you find that you have a different PHP version installed or you're unable to switch to PHP 7.4, ensure that the ondrej/php repository is prioritized in your package sources. You can check the priorities by running the command apt-cache policy php7.4. If needed, you can modify the priorities using the apt_preferences file.

Remember, these are just a few common troubleshooting scenarios, and there may be other unique issues you might encounter. It's always a good idea to search for specific error messages or consult online forums and communities for additional support.

In the next section, we'll wrap up the article with a concise summary of what we've covered and encourage readers to explore the capabilities of PHP 7.4 in their web development projects.


Congratulations! You've successfully installed PHP 7.4 on your Ubuntu 20.04 system. Throughout this guide, we've covered the step-by-step installation process, starting from updating the system packages to configuring PHP and testing the installation. By following these instructions, you've equipped yourself with a powerful web development tool that can handle dynamic and interactive web applications.

PHP 7.4 brings with it a range of features and enhancements that improve performance, security, and overall development experience. Now, it's up to you to explore and harness the power of PHP 7.4 in your web projects. From building dynamic websites to creating robust web applications, PHP 7.4 opens up a world of possibilities.

If you encountered any issues during the installation process or have any questions, please feel free to reach out to us. We're here to assist you and ensure your PHP 7.4 installation journey is a smooth one.

Thank you for choosing our guide on how to install PHP 7.4 on Ubuntu 20.04. We hope this article has been informative and helpful. Now, go forth and unleash your creativity with PHP 7.4!

About the Author

Rexposed Staff

Was this helpful?