Ubuntu 23.04环境下PHP7.2安装方法及安装报错解决求助
Hey there, I totally get the frustration when you try installing PHP 7.2 on Ubuntu 23.04 and hit those "Unable to locate package" errors—let’s figure out what’s going on and how to fix it.
Why the error happens?
Ubuntu 23.04’s default software repositories don’t include PHP 7.2 anymore. That’s because PHP 7.2 reached its end-of-life (EOL) back in November 2020, meaning it no longer gets security updates or official support from the PHP team. For security reasons, Ubuntu removes EOL software from its default sources.
Step-by-step installation fix
If you really need PHP 7.2 (for an old project or testing purposes—note: not recommended for production!), you can use a third-party PPA that maintains legacy PHP versions:
First, install the tools needed to add new repositories:
sudo apt update && sudo apt install software-properties-commonAdd the popular Ondřej Surý PHP PPA (this repo keeps older PHP versions available for newer Ubuntu releases):
sudo add-apt-repository ppa:ondrej/phpYou’ll see a prompt about the PPA details—just press Enter to confirm adding it.
Update your package list again to include the new PPA’s packages:
sudo apt updateNow you can install PHP 7.2, along with common extensions you might need (adjust based on your requirements):
sudo apt install php7.2 php7.2-cli php7.2-fpm php7.2-mysql php7.2-curl php7.2-gd php7.2-mbstring php7.2-xml php7.2-zipVerify the installation worked by checking the PHP version:
php -v
Alternative: Use Docker (safer for production/testing)
Since PHP 7.2 is unmaintained, running it directly on your Ubuntu 23.04 system carries security risks. A better option is to use Docker to run PHP 7.2 in an isolated container:
- Install Docker if you haven’t already, then run:
This will start an Apache server with PHP 7.2, accessible atdocker run -d -p 8080:80 php:7.2-apachehttp://localhost:8080.
Important note
Never use PHP 7.2 in a production environment—it’s no longer receiving security patches, which leaves your system vulnerable to exploits. Only use it for legacy projects that you’re actively planning to upgrade.
备注:内容来源于stack exchange,提问作者Md.Tanjeeb Ahsan

