How to Install PHP on Ubuntu: The Complete Step-by-Step Guide

PHP still powers a vast share of the web, from WordPress and Laravel applications to custom internal tools. If you run a server on Ubuntu, getting a clean, correctly versioned PHP installation in place is the foundation everything else builds on. This guide walks you through every path: a fast apt install for the default version, the ondrej/php PPA when you need a specific release like PHP 8.3, the difference between running PHP under Apache and Nginx, installing the extensions real applications depend on, and verifying it all works.

The commands below target Ubuntu 22.04 LTS and 24.04 LTS, but the workflow is identical across modern releases. Every step uses the terminal, so you can apply it equally to a desktop, a VPS, or a cloud instance.

Key Takeaways
• Run `sudo apt update` first, then `sudo apt install php` to get the Ubuntu-default PHP version in seconds.
• To install a specific PHP version (for example PHP 8.3), add the ondrej/php PPA and install the versioned package such as `php8.3`.
• Use libapache2-mod-php for Apache, or php-fpm for Nginx and high-traffic setups.
• Install only the extensions your application needs: `php-mysql`, `php-curl`, `php-mbstring`, `php-xml`, `php-gd`, `php-zip`.
• Verify with `php -v` and switch between installed versions using `update-alternatives`.

Why does the PHP version on Ubuntu matter?

Ubuntu ships a single default PHP version tied to each release. Ubuntu 22.04 defaults to PHP 8.1, while Ubuntu 24.04 defaults to PHP 8.3. That default is fine for many projects, but real-world applications are often pinned to a particular version: a legacy app may require PHP 7.4, while a modern framework may demand 8.2 or later.

When the version you need differs from the Ubuntu default, the standard `apt` repositories will not have it. That is exactly the gap the ondrej/php PPA fills, and we cover it below.

How do I install PHP on Ubuntu with apt?

Start by refreshing your package index so apt knows about the latest available packages:

“`bash sudo apt update “`

Then install PHP with a single command:

“`bash sudo apt install php “`

This pulls in the default PHP version for your Ubuntu release along with the PHP CLI (command-line interpreter). Confirm it worked:

“`bash php -v “`

You should see output similar to this:

“` PHP 8.3.6 (cli) (built: …) Copyright (c) The PHP Group Zend Engine v4.3.6, … “`

That single `apt install php` is the fastest route and is the right choice whenever the default version meets your requirements.

How do I install a specific PHP version using the ondrej/php PPA?

When you need a version other than the Ubuntu default, the community-maintained ppa:ondrej/php repository is the standard solution. It packages every actively supported PHP version (and several older ones) for current Ubuntu releases.

First, install the prerequisites that let you manage PPAs:

“`bash sudo apt install software-properties-common “`

Add the repository and refresh:

“`bash sudo add-apt-repository ppa:ondrej/php sudo apt update “`

Now install the exact version you want by using its versioned package name:

“`bash sudo apt install php8.3 “`

Swap `php8.3` for `php8.2`, `php8.1`, `php7.4`, or whichever release your project requires. A major advantage of this approach is that you can install multiple PHP versions side by side on the same server and choose which one runs per command or per site.

A subtle but important detail: installing `php8.3` from the ondrej PPA does not automatically make it your active CLI version. The `php` command points to whichever version `update-alternatives` has registered as the default. After installing a new version, always run `php -v` to confirm which interpreter actually responds, then use `update-alternatives` (covered below) if you need to change it. Skipping this check is the single most common reason developers think a version “did not install” when in fact it installed perfectly and simply is not the active default.

apt default vs ondrej/php PPA at a glance

Consideration `apt install php` (default repo) `ppa:ondrej/php`
PHP version Locked to Ubuntu release default Any supported version you choose
Multiple versions Not practical Yes, install side by side
Setup steps One command Add PPA, then install
Maintained by Ubuntu Community (Ondrej Sury)
Best for Quick setups on the default version Specific versions, legacy apps, multi-version servers
Security updates Via Ubuntu updates Via PPA updates (`apt upgrade`)

Should I use PHP with Apache or Nginx?

How PHP connects to your web server depends on which server you run. The two common patterns are an Apache module and PHP-FPM.

Installing PHP for Apache

Apache can run PHP directly through a module. Install it with:

“`bash sudo apt install libapache2-mod-php “`

For a specific version from the ondrej PPA, use the matching package:

“`bash sudo apt install libapache2-mod-php8.3 “`

Restart Apache so it loads the module:

“`bash sudo systemctl restart apache2 “`

This mod_php approach is simple and works well for smaller sites, but it ties PHP processing to Apache’s worker model.

Installing PHP-FPM for Nginx

Nginx does not execute PHP itself. Instead it passes requests to PHP-FPM (FastCGI Process Manager), a separate service that handles PHP processing efficiently. Install it with:

“`bash sudo apt install php-fpm “`

Or a versioned variant:

“`bash sudo apt install php8.3-fpm “`

Confirm the service is running:

“`bash sudo systemctl status php8.3-fpm “`

In your Nginx server block, you then pass `.php` requests to the FPM socket, for example `fastcgi_pass unix:/run/php/php8.3-fpm.sock;`. PHP-FPM is the recommended choice for Nginx, for high-traffic Apache sites (via `mpm_event`), and for any setup where you want PHP processes managed independently of the web server.

Which PHP extensions should I install?

A bare PHP install handles core syntax but lacks the libraries most applications need, such as database drivers or image processing. Extensions are installed as separate packages and are easy to add. Install the common set in one command:

“`bash sudo apt install php-mysql php-curl php-mbstring php-xml php-gd php-zip “`

For a specific version, prefix the package names:

“`bash sudo apt install php8.3-mysql php8.3-curl php8.3-mbstring php8.3-xml php8.3-gd php8.3-zip “`

Here is what each common extension does:

Extension Package What it enables
MySQL `php-mysql` Connecting to MySQL and MariaDB databases (PDO and mysqli)
cURL `php-curl` Making outbound HTTP requests and calling external APIs
Mbstring `php-mbstring` Multibyte and UTF-8 safe string handling
XML `php-xml` Parsing and generating XML, used by many frameworks
GD `php-gd` Image creation, resizing, and manipulation
Zip `php-zip` Reading and writing ZIP archives (needed by Composer and CMSs)

After installing extensions, restart your PHP handler (`apache2` or the relevant `php-fpm` service) so they load. Check which extensions are active with:

“`bash php -m “`

How do I verify that PHP is working?

There are two reliable checks. The first is the command line:

“`bash php -v “`

The second confirms PHP works through your web server. Create a test file in your web root:

“`bash echo “” | sudo tee /var/www/html/info.php “`

Visit `http://your-server-ip/info.php` in a browser. The phpinfo() page lists the active version, loaded extensions, and configuration paths. Once you have confirmed everything, delete the file, because it exposes server details:

“`bash sudo rm /var/www/html/info.php “`

How do I switch between PHP versions?

If you installed several versions through the ondrej PPA, the update-alternatives tool controls which one the `php` command uses. Launch the interactive selector:

“`bash sudo update-alternatives –config php “`

You will see a numbered list of installed versions. Type the number you want and press Enter. Verify the change:

“`bash php -v “`

Note that this sets the CLI default. Your web server uses whichever module or FPM service its configuration points to, so for Apache you may also switch modules with `a2dismod` and `a2enmod`, and for Nginx you update the `fastcgi_pass` socket path to the desired version.

How do I configure php.ini?

The php.ini file holds runtime settings such as memory limits and upload sizes. Ubuntu keeps separate files for the CLI and the web server. Find them with:

“`bash php –ini “`

A typical web server path looks like `/etc/php/8.3/apache2/php.ini` or `/etc/php/8.3/fpm/php.ini`. Common values worth reviewing include `memory_limit`, `upload_max_filesize`, `post_max_size`, and `max_execution_time`. After editing the correct `php.ini`, restart the matching service so changes take effect:

“`bash sudo systemctl restart php8.3-fpm “`


Skip the manual setup with DarazHost

Installing and tuning PHP by hand gives you full control, but it is not the only path. DarazHost offers two approaches depending on how much you want to manage:

  • Managed hosting ships with PHP pre-installed and multiple PHP versions selectable through cPanel’s MultiPHP Manager, so you can switch a site between PHP 7.4 and 8.3 with a dropdown, no terminal required.
  • VPS hosting gives you full root access to install and configure any PHP version, extensions, and `php.ini` settings yourself, exactly as described in this guide, on a server you control.

Both come with 24/7 support from engineers who work with PHP environments every day. Whether you want a hands-off managed stack or a blank VPS to build on, DarazHost covers both ends.


Frequently asked questions

Which PHP version does Ubuntu install by default? It depends on your Ubuntu release. Ubuntu 22.04 LTS installs PHP 8.1 by default, and Ubuntu 24.04 LTS installs PHP 8.3. To get any other version, add the ondrej/php PPA and install the versioned package such as `php8.2`.

Can I install multiple PHP versions on one Ubuntu server? Yes. Using the ondrej/php PPA you can install several versions side by side, for example `php8.1`, `php8.2`, and `php8.3`. Use `update-alternatives –config php` to choose the CLI default, and point each web server site to the version it needs.

Do I need php-fpm or libapache2-mod-php? Use `libapache2-mod-php` if you run Apache and want the simplest setup. Use `php-fpm` if you run Nginx, since Nginx cannot execute PHP directly, or if you run high-traffic Apache and want PHP processes managed separately for better performance.

How do I know which PHP extensions are installed? Run `php -m` to list all loaded modules on the command line, or load a `phpinfo()` page in the browser to see extensions active for the web server. Install missing ones with `sudo apt install php-extensionname`.

Why does php -v show the wrong version after installing a new one? Installing a new versioned package does not change the active CLI default. The `php` command follows whatever `update-alternatives` has set. Run `sudo update-alternatives –config php`, select the version you want, and check again with `php -v`.

About the Author

Leave a Reply