How To Install PHP 7.4 and Set Up a Local Development Environment on Ubuntu 20.04
PHP is a popular server scripting language known for creating dynamic and interactive web pages. Getting up and running with your language of choice is the first step in learning to program.
This tutorial will guide you through installing PHP 7.4 on Ubuntu and setting up a local programming environment via the command line. You will also install a dependency manager, Composer, and test your installation by running a script.
Prerequisites
To complete this tutorial, you will need a local or virtual machine with Ubuntu 20.04 installed and have administrative access and an internet connection to that machine. You can download this operating system via the Ubuntu releases page.
Step 1 — Setting Up PHP 7.4
You’ll be completing your installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.
The command line, also known as a shell or terminal, can help you modify and automate many of the tasks you do on a computer every day and is an essential tool for software developers. There are many terminal commands to learn that can enable you to do more powerful things. The article An Introduction to the Linux Terminal can get you better oriented with the terminal.
On Ubuntu, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left-hand corner of your screen and typing terminal into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL , ALT , and T keys on your keyboard at the same time to open the Terminal application automatically.
Note: Ubuntu 20.04 ships with PHP 7.4 in its upstream repositories. This means that if you attempt to install PHP without a specified version, it will use 7.4.
You will want to avoid relying on the default version of PHP because that default version could change depending on where you are running your code. You may also wish to install a different version to match an application you are using or to upgrade to a newer version, such as PHP 8.
Run the following command to update apt-get itself, which ensures that you have access to the latest versions of anything you want to install:
Next, install software-properties-common , which adds management for additional software sources:
The -y flag will automatically agree to the installation. Without that, you would receive a prompt in your terminal window for each installation.
Next, install the repository ppa:ondrej/php , which will give you all your versions of PHP:
Finally, you update apt-get again so your package manager can see the newly listed packages:
Now you’re ready to install PHP 7.4 using the following command:
Check the version installed:
You will receive something similar to the following:
OutputPHP 7.4.0beta4 (cli) (built: Aug 28 2019 11:41:49) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies with Zend OPcache v7.4.0beta4, Copyright (c), by Zend Technologies
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:
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:
-
sudoapt-getinstall-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:
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.
You could start using PHP right now, but you will likely want to use various libraries to build PHP applications quickly. Before you test your PHP environment, first set up a dependency manager for your projects.
Step 2 — Setting Up Composer for Dependency Management (Optional)
Libraries are a collection of code that can help you solve common problems without needing to write everything yourself. Since there are many libraries available, using a dependency manager will help you manage multiple libraries as you become more experienced in writing PHP.
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and will manage installing and updating these packages.
Although similar, Composer is not a package manager in the same sense as yum or apt . It deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor ) inside your project. By default, it does not install anything globally. Thus, it is a dependency manager. It does, however, support a global project for convenience via the global command.
This idea is not new, and Composer is strongly inspired by Node’s npm and Ruby’s bundler .
- You have a project that depends on several libraries.
- Some of those libraries depend on other libraries.
- Enables you to declare the libraries you depend on.
- Finds out which versions of which packages can and need to be installed and installs them by downloading them into your project.
- Enables you to update all your dependencies in one command.
- Enables you to see the Basic Usage chapter for more details on declaring dependencies.
There are, in short, two ways to install Composer: locally as part of your project or globally as a system-wide executable. Either way, you will start with the local install.
Locally
To quickly install Composer in the current directory, run this script in your terminal:
-
php -r«copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);»php -r«if (hash_file(‘sha384’, ‘composer-setup.php’) === ‘756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3’) < echo 'Installer verified'; >else < echo 'Installer corrupt'; unlink('composer-setup.php'); >echo PHP_EOL;»php composer-setup.php php -r«unlink(‘composer-setup.php’);»
This installer script will check some php.ini settings, warn you if they are set incorrectly, and then download the latest composer.phar in the current directory. The four lines will, in order:
- Download the installer to the current directory
- Verify the installer SHA-384, which you can also cross-check here
- Run the installer
- Remove the installer
The installer will check a few PHP settings and then download composer.phar to your working directory. This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP that can be run on the command line, amongst other things.
In order to run Composer, you use php composer.phar . As an example, run this command to see the version of Composer you have installed:
To use Composer locally, you will want your composer.phar file to be in your project’s root directory. You can start in your project directory before installing Composer. You can also move the file after installation. You can also install Composer to a specific directory by using the —install-dir option and additionally (re)name it using the —filename option.
Since Composer is something used across projects, it’s recommended that you continue to the next portion and set Composer to run globally.
Globally
You can place the Composer PHAR anywhere you wish. If you put it in a directory that is part of your $PATH , you can access it globally. You can even make it executable on Ubuntu (and other Unix systems) and invoke it without directly using the PHP interpreter.
After installing locally, run this command to move composer.phar to a directory that is in your path:
If you’d like to install it only for your user and avoid requiring root permissions, you can use ~/.local/bin instead, which is available by default on some Linux distributions:
Now to run Composer, use composer instead of php composer.phar . To check for your Composer version, run:
As a final step, you may optionally initialize your project with composer init . This will create the composer.json file that will manage your project dependencies. Initializing the project will also let you define project details such as Author and License, and use Composer’s autoload functionality. You can define dependencies now or add them later.
Run this command to initialize a project:
Running this command will start the setup wizard. Details that you enter in the wizard can be updated later, so feel free to leave the defaults and just press ENTER . If you aren’t ready to install any dependencies, you can choose no . Enter in your details at each prompt:
OutputThis command will guide you through creating your composer.json config. Package name (sammy/php_install): sammy/project1 Description []: Author [Sammy , n to skip]: Minimum Stability []: Package Type (e.g. library, project, metapackage, composer-plugin) []: project License []: Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? no Would you like to define your dev dependencies (require-dev) interactively [yes]? no < "name": "sammy/project1", "type": "project", "authors": [ < "name": "Sammy", "email": "sammy@digitalocean.com" >], "require": <> > Do you confirm generation [yes]? yes
Before you confirm the generation, you will see a sample of the composer.json file that the wizard will create. If it all looks good, you can confirm the default of yes . If you need to start over, choose no .
The first time you define any dependency, Composer will create a vendor folder. All dependencies install into this vendor folder. Composer also creates a composer.lock file. This file specifies the exact version of each dependency and subdependency used in your project. This assures that any machine on which your program is run, will be using the exact same version of each packages.
Note: The vendor folder should never be committed to your version control system (VCS). The vendor folder only contains packages you have installed from other vendors. Those individual vendors will maintain their own code in their own version control systems. You should only be tracking the code you write. Instead of committing the vendor folder, you only need to commit your composer.json and composer.lock files. You can learn more about ignoring specific files in How To Use Git: A Reference Guide.
Now that you have PHP installed and a way to manage your project dependencies using Composer, you’re ready to test your environment.
Step 3 — Testing the PHP Environment
To test that your system is configured correctly for PHP, you can create and run a basic PHP script. Call this script hello.php :
This will open a blank file. Put the following text, which is valid PHP code, inside the file:
echo 'Hello World!'; ?>
Once you’ve added the text, save and close the file. You can do this by holding down the CTRL key and pressing the x key. Then choose y and press ENTER .
Now you can test to make sure that PHP processes your script correctly. Type php to tell PHP to process the file, followed by the name of the file:
If the PHP is processed properly, you will see only the characters within the quotes:
OutputHello World!
PHP has successfully processed the script, meaning that your PHP environment is successfully installed and you’re ready to continue your programming journey.
Conclusion
At this point, you have a PHP 7.4 programming environment set up on your local Ubuntu machine and can begin a coding project.
Before you start coding, you may want to set up an Integrated Development Environment (IDE). While there are many IDEs to choose from, VS Code is a popular choice as it offers many powerful features such as a graphical interface, syntax highlighting, and debugging.
With your local machine ready for software development, you can continue to learn more about coding in PHP by following How To Work With Strings in PHP.
Get Ubuntu on a hosted virtual machine in seconds with DigitalOcean Droplets! Simple enough for any user, powerful enough for fast-growing applications or businesses.
Tutorial Series: How To Code in PHP

PHP is a popular server scripting language known for creating dynamic and interactive web pages.
How to install PHP 7.4 on Ubuntu 22.04 LTS Jammy Linux
By default system repository Php 8.1 is available to install on Jammy JellyFish, hence in this tutorial, we learn the commands to install PHP 7.4 on Ubuntu 22.04 LTS server or desktop using the terminal.
PHP is a scripting language, so it is translated line by line into machine commands by an interpreter. On the Internet, PHP is behind over 80% of all websites on whose web servers it runs and thus controls the behavior of the website. It has been used by many web servers such as Apache to power thousands of websites and web platforms. Also, PHP makes it easy for programmers to develop and control a website functioning because PHP has numerous function libraries and connections to Internet protocols and databases.
Further, PHP can be used to process user input. If, for example, you want to include a contact form on your website, the entries must be further processed. Only with HTML, this is not possible. With PHP, for example, you can check whether all fields have been filled in and, if successful, send an e-mail. Also, when it comes to retrieving content from a database, it makes sense to use PHP. For example, if you want to program a news website, you can save your texts in a database and then output them with PHP at the desired places in your HTML code. You do not have to create a single HTML file for each article and can implement changes much faster. That’s why PHP has been used for creating Dynamic websites.
On the Page hide
Steps to install PHP 7.2 on Ubuntu 22.04 LTS server or desktop
The steps given here can also be used for other Linux distros based on Ubuntu 22.04 LTS such as POP OS Linux, Linux Mint, Elementary OS, and more…
1. Run system update
Our system must be in its latest state to avoid any package conflict. Hence, before going further, let’s first run the system update command to ensure all the available security updates are on our Ubuntu.
sudo apt update && sudo apt upgrade
2. Add Ondrej PPA Repository on Ubuntu 22.04
We cannot install the PHP7.4 packages using the default system repository of Ubuntu 22.04 because the default version of PHP present to install in this Ubuntu version is PHP 8.1. Hence, to get the older version, add the PPA repository called Ondrej.
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
How To Install PHP On Ubuntu 20.04 or 22.04
PHP stands for Hypertext Preprocessor, and it’s a script-based server-side programming language.
PHP helps automate various server tasks. It handles tasks such as dynamic content, database requests, and data preprocessing/display.
Read our tutorial on how to install PHP on Ubuntu 20.04 or 22.04 including integration with Apache and Nginx.

- A Linux system running Ubuntu 20.04 or 22.04
- Access to an Ubuntu user account with sudo privileges.
- Access to a command line/terminal window (Ctrl+Alt+T).
- A running web server (Apache or Nginx).
Installing PHP on Ubuntu
Like many developer tools, PHP has several different release versions. At the time of writing, PHP 7.4. and 8.1 are the currently supported and most used versions of the software.
The following command installs PHP using the apt package manager:
sudo apt install php
On Ubuntu 20.04, the command installs PHP 7.4 while Ubuntu 22.04 installs PHP version 8.1.
If you’re looking to choose the specific version of PHP on either system, follow one of the installation steps below.
1. Install PHP with Apache on Ubuntu
If you are running an Apache web server, install PHP with the Apache module. Below you will find installations for both PHP 7.4 and PHP 8.1.
1. Ensure you are using the latest repository updates by entering the following command into a terminal window:
sudo apt update && sudo apt upgrade
2. Install software-properties-common to help you manage distributions and independent software sources:
sudo apt install software-properties-common
If the package is already available, continue to the next step.
3. Next, add the ondrej/php PPA which provides different PHP versions for Ubuntu:
sudo add-apt-repository ppa:ondrej/php
After the PPA loads, press Enter to confirm adding the repository. The available PHP versions in the PPA are from 5.6 up to 8.2.
4. Update apt to include the new packages:
sudo apt update
5. Now you can install specific PHP versions. For example:
- To install PHP 7.4, run the command:
sudo apt -y install php7.4
- To install PHP 8.1, run the following command:
sudo apt -y install php8.1
Note: The command for installing PHP automatically installs the PHP module for Apache 2. If it fails to do so, run the following command instead:
sudo apt -y install libapache2-mod-php
6. Verify the installation with:
php -v
The output displays the PHP version you installed.
If you installed version 7.4, the output appears as in the image below.

If you installed PHP 8.1, the output shows:

2. Install PHP with Nginx on Ubuntu
The Nginx server does not have native PHP processing. If you are using an Nginx server instead of Apache, follow the steps below to install PHP 7.4 or 8.1 to work with Nginx.
1. Enter the following command into a terminal window to ensure you are using the latest software:
sudo apt update && sudo apt upgrade
2. Add the ondrej/php PPA to include various PHP versions:
sudo add-apt-repository ppa:ondrej/php
When the PPA information loads, press Enter to continue.
2. To install PHP for Nginx, use one of the following commands:
sudo apt install php7.4-fpm -y
sudo apt install php8.1-fpm -y
The system will reach out to download and install the package and its dependencies.

2. Once the installation finishes, restart the Nginx service to apply the changes by entering:
sudo systemctl restart nginx
3. Next, enable PHP support by editing the server block. Open the server block with the command:
sudo nano /etc/nginx/sites-available/default
4. Add the following code to your server block file for Nginx to make use of PHP:
server< # . . . existing configuration location ~ \.php$ < include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php-fpm.sock; > >
Exchange the for your PHP version.
4. Save the file and exit.
5. Finally, restart Nginx on Ubuntu and reload PHP:
sudo systemctl restart nginx
sudo systemctl reload php-fpm
Installing PHP Modules on Ubuntu
To install additional PHP modules, use the following syntax:
sudo apt install php-
sudo apt install php8.1-posix
Alternatively, install multiple modules at once. For example, to install modules mysql, zip and bcmath on PHP 7.4, you would run:
sudo apt install php7.4-
To list all loaded PHP modules run the command:
php -m
The output lists all compiled PHP modules, as in the example below.

How to Uninstall PHP on Ubuntu
To uninstall PHP from Ubuntu, follow the instructions below:
1. Run the following command in the terminal:
sudo apt-get purge php
For example, if you installed the PHP 7.4 FPM version, run:
sudo apt-get purge php7.4-fpm
2. Remove the orphaned packages with:
sudo apt-get autoremove
3. Lastly, check the PHP version to confirm the uninstall worked:
php -v
If the output does not show the version, the uninstall removed everything correctly.
After reading this article, you should have successfully installed PHP 7.4 or 8.1 with Apache or Nginx on your Ubuntu 20.04 and 22.04 system.
One important thing to remember is that PHP works in tandem with an existing server, so you need to install it specifically to your server software. Alternatively, you can always install a stable version from the default Ubuntu software repositories.
Was this article helpful?
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
August 6, 2019
If a script is not written correctly, or if something unusual happens, PHP can generate an error message. The .
Unable to install PHP 7.4 on Ubuntu 16.04 even with Ondrej PPA repository in apache2
I am trying to install PHP 7.4 on Ubuntu 16.04 for Apache2 server. To install the 7.4 version, I used PPA ondrej repository as follows:
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt install -y php7.4 php -v
I get the following error:
Reading package lists. Done Building dependency tree Reading state information. Done E: Unable to locate package php7.4 E: Couldn't find any package by glob 'php7.4' E: Couldn't find any package by regex 'php7.4'
I tried all the solutions mentioned in answer to this question: Unable to install PHP 7.3 on Ubuntu 20.04 None of them helped. How can I install this package?
205 5 5 silver badges 13 13 bronze badges
asked Jun 10, 2021 at 11:46
Santosh Dangare Santosh Dangare
695 1 1 gold badge 5 5 silver badges 15 15 bronze badges
You might have better luck asking on askubuntu.com since this has to do with installing from repositories instead of being a coding issue.
Jun 10, 2021 at 12:09
yeah @aynber , but I thought SO has bigger community than askubantu.com that’s why I aksed on SO
Jun 10, 2021 at 12:15
sure it has a bigger community but the problem is, we only answer programming questions, not system setup questions. See What topics can I ask about here? . Each stackexchange community answers different types of questions.
Jun 10, 2021 at 12:21
P.S. the link you’ve given refers to both a different version of PHP and a different version of Ubuntu. So there’s going to be no guarantee it’s applicable to your scenario. Ubuntu 16.04 just went out of support anyway so you should be upgrading the O/S, not the PHP version.
Jun 10, 2021 at 12:24
That’s unfortunate. But largely irrelevant. the bigger issue is running an unsupported O/S. Upgrade to a newer version and you should find that installing recent versions of PHP isn’t an issue.
Jun 10, 2021 at 12:42
4 Answers 4
You can archive this by adding this 3rd party repository to your system:
sudo add-apt-repository ppa:jczaplicki/xenial-php74-temp sudo apt-get update
CAUTION:
This is adding an external PPA repository to your system. If you do not trust the maintainer you should not do this, since it possibly exposes your system. Also note that this might work for installing php, but there will not be a guarantee that you recvive updates through it (The word «temp» in the repository name indicates that this is only temporary). I would rather recommend compiling it yourself if you really need to have it installed on 16.04.
As a «Long-Term Solution» you really should update your system.
