Guides 11792 Published by

If you’re still stuck on PHP 7.x and want the newest language features on Ubuntu 22.04 or 20.04, this guide walks you through adding Ondřej Surý’s PPA, removing older binaries, and installing PHP 8.2 along with its common extensions. After updating the package list and purging any legacy PHP packages, you run a single apt‑get install command that pulls in php8.2-cli, php8.2-fpm, mysql, xml, curl, mbstring, and any extra modules you need. The author then shows how to confirm the installation with php -v, reload services like nginx or Apache if using FPM, and offers quick fixes for common errors such as missing packages, lingering old modules, or permission problems on file writes. In short, the process is straightforward—just a few apt commands—and if something goes wrong you can revert to the PPA’s previous version or reinstall an older PHP release with minimal hassle.



How to Install PHP 8.2 on Ubuntu 22.04/20.04

If you’re running an old LAMP stack and need the new language features or just want a smoother runtime, installing PHP 8.2 on your Ubuntu 22.04 (Jammy) or 20.04 (Focal) box is surprisingly painless.

In this guide you’ll get the latest official build from Ondřej Surý’s PPA, keep any older PHP binaries out of the way, and verify that everything works before you start coding.

What’s in the Box
  • Ubuntu 22.04 or 20.04 LTS (the code names are Jammy and Focal but who cares?)
  • Root access (or sudo privileges)
  • A working internet connection to pull packages from the PPA
  • Basic shell knowledge – you’ll be typing a handful of commands
Add the PPA for PHP 8.2
sudo apt-get update
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

Why this matters: The default Ubuntu repositories ship PHP 7.x or early‑8.x builds that lag behind the official releases.

The Ondřej Surý PPA is the community’s go‑to source for current PHP packages; it keeps them in sync with upstream and offers a broad selection of extensions.

Remove Old PHP Versions

If you already have another PHP major version installed, clean it up first:

sudo apt-get purge 'php*'

Why: Leftover binaries can confuse the update-alternatives system or clash when you later enable Apache’s mod_php module.

Purging ensures a clean slate and that your server will run the 8.2 interpreter by default.

Install PHP 8.2 and Common Extensions
sudo apt-get install php8.2 \
    php8.2-cli \
    php8.2-fpm \
    php8.2-mysql \
    php8.2-xml \
    php8.2-curl \
    php8.2-mbstring

Why:

  • php8.2-cli is the command line interpreter – essential for cron jobs, scripts, and testing.
  • php8.2-fpm powers FastCGI with Nginx or Apache’s proxy module; it outperforms mod_php in most setups.
  • The MySQL, XML, cURL, and mbstring extensions are the workhorses of many PHP applications – from WordPress to custom frameworks.

If you need more, just add them to the command line (e.g., php8.2-gd, php8.2-zip).

Verify the Installation
php -v

You should see something like:

PHP 8.2.x (cli) (built: ... ) 
Copyright ...

Why: A quick sanity check guarantees that the interpreter is in your $PATH and the correct version is being invoked.

If you’re using FPM with Nginx, reload or restart it to pick up the new PHP binary:

sudo systemctl restart php8.2-fpm
sudo systemctl restart nginx
Troubleshoot Common Pitfalls
  • “Could not locate package php8.2”

Make sure you ran add-apt-repository and updated the package list afterwards.

  • Apache still uses PHP 7.x

Disable old modules: sudo a2dismod php7. (replace with your major/minor). Then enable the new one: sudo a2enmod php8.2.

  • Extensions missing

Some extensions are optional in the base package set; install them explicitly with apt-get install php8.2-<extension>.

  • Permission errors on PHP‑handled files

The FPM pool runs as www-data by default; check file ownership and adjust if your scripts write to disk.

Final Thoughts

That’s all there is to it: a couple of apt commands and you’re up and running with the latest stable PHP. If something goes sideways, just roll back the PPA or reinstall the older version—Ubuntu’s package system makes it painless.