如何升级PHP至7.3+以解决Symfony项目Composer依赖问题
Hey there, I’ve dealt with this exact scenario on Ubuntu 18.04 before—since the default system repositories only ship with PHP 7.2, you’ll need to use a trusted third-party PPA to get newer PHP versions. Here’s a step-by-step guide:
1. 添加PHP官方维护的PPA
The ondrej/php PPA is the go-to source for updated PHP packages on Ubuntu, maintained by the PHP community. Run these commands to add it and refresh your package list:
sudo add-apt-repository ppa:ondrej/php sudo apt update
2. 安装PHP 7.3(或更高版本)
Install PHP 7.3 along with common extensions that Symfony projects typically require. Adjust the extensions based on your project’s specific needs:
sudo apt install php7.3 php7.3-cli php7.3-fpm php7.3-mysql php7.3-xml php7.3-curl php7.3-gd php7.3-mbstring
If you need a newer version (like 7.4 or 8.0), just replace 7.3 with your target version number in the command above.
3. 切换系统默认PHP版本
Ubuntu will keep the old PHP 7.2 installed by default, so you need to set PHP 7.3 as the default for CLI:
sudo update-alternatives --set php /usr/bin/php7.3
Verify the change by running:
php -v
You should see output showing PHP 7.3.x instead of 7.2.24.
4. 适配Web服务器(如果需要)
If your Symfony project runs on a web server like Apache or Nginx, you’ll need to update the server to use the new PHP version:
- For Apache:
sudo a2dismod php7.2 sudo a2enmod php7.3 sudo systemctl restart apache2 - For Nginx:
Edit your site’s Nginx configuration file (usually located in/etc/nginx/sites-available/), find thefastcgi_passline, and update it to point to PHP 7.3’s socket:
Then restart Nginx:fastcgi_pass unix:/run/php/php7.3-fpm.sock;sudo systemctl restart nginx
5. 重新运行Composer
Once PHP 7.3 is set up, go back to your Symfony project directory and run:
composer install
This should resolve the version conflict with ocramius/package-versions and complete the dependency installation.
内容的提问来源于stack exchange,提问作者malongoDB

