{"id":47635,"date":"2024-11-28T15:23:37","date_gmt":"2024-11-28T15:23:37","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=47635"},"modified":"2025-11-22T08:35:30","modified_gmt":"2025-11-22T08:35:30","slug":"install-or-upgrade-to-php-8-4-in-ubuntu-24-04-22-04","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2024\/11\/install-or-upgrade-to-php-8-4-in-ubuntu-24-04-22-04\/","title":{"rendered":"Install or Upgrade to PHP 8.5 in Ubuntu 24.04 | 22.04"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-logo.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-43436\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-logo-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-logo-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-logo-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-logo-600x600.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-logo-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-logo.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>PHP, the popular scripting language for web development, announced new 8.5 release few days ago. Here&#8217;s how to install or upgrade to Php 8.5 in Ubuntu 22.04 or Ubuntu 24.04 to work with Apache2 or Nginx web server.<\/p>\n<p>PHP 8.5 introduced a number of new features such as new &#8220;URI&#8221; extension, new pipe operator (|&gt;), new #[\\NoDiscard] attribute, support for closures, casts, and first class callables in constant expressions, and much more. See the <a href=\"https:\/\/www.php.net\/ChangeLog-8.php#PHP_8_5\" target=\"_blank\" rel=\"noopener\">release page<\/a> for more.<\/p>\n<p><!--more--><\/p>\n<h3>Why you need to upgrade PHP<\/h3>\n<p>The most obvious reason to upgrade php is <b>security<\/b>! Older versions are more likely to have security vulnerabilities and outdated code that is not longer supported.<\/p>\n<p>Newer versions may support new features and functionality great for web development. It usually includes performance improvements to make your website load <b>faster<\/b>!<\/p>\n<p>And, you can generally expect to see a <b>decrease in memory usage<\/b> when upgrading php from older to newer versions, which is really helpful for server with small amount of RAM.<\/p>\n<h3>Step 1: Install PHP 8.5 in Ubuntu 22.04 | 24.04<\/h3>\n<p>PHP 8.5 is easy to install in all current Ubuntu LTS releases through <a href=\"https:\/\/launchpad.net\/~ondrej\/+archive\/ubuntu\/php\" target=\"_blank\" rel=\"noopener\">Ondrej Sury&#8217;s PPA<\/a>, which is practically &#8220;official&#8221;, as it&#8217;s maintained by the maintainer of official Debian PHP packages.<\/p>\n<p><b>NOTE: If you&#8217;re trying to upgrade from PHP 7.x or even earlier versions. You needs to do some searches first, as it may break your site!!<\/b><\/p>\n<p><b>1.<\/b> First, connect to your Ubuntu server, then run command to install the package for the <code>add-apt-repository<\/code> tool.<\/p>\n<pre>sudo apt install software-properties-common<\/pre>\n<p><b>2.<\/b> Next, run command to add the PPA repository and hit Enter to continue:<\/p>\n<pre>LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej\/php<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-unofficial-ppa.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-43437\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-unofficial-ppa-600x415.webp\" alt=\"\" width=\"600\" height=\"415\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-unofficial-ppa-600x415.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-unofficial-ppa-300x208.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/12\/php-unofficial-ppa.webp 640w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><b>3.<\/b> Adding PPA now automatically refresh package cache. Just in case, you may run <code>sudo apt update<\/code> to update cache manually.<\/p>\n<p>Finally, install PHP 8.5 depends on your web server:<\/p>\n<ul>\n<li>To install php 8.5 for <b>Apache 2<\/b>, use command:\n<pre>sudo apt install php8.5 libapache2-mod-php8.5<\/pre>\n<\/li>\n<li>Or run this command instead for <b>Nginx<\/b>:\n<pre>sudo apt install php8.5 php8.5-fpm<\/pre>\n<\/li>\n<\/ul>\n<p><b>4.<\/b> You may also install some other modules, e.g., php8.5-tidy, php8.5-mysql, php8.5-xml, php8.5-zip, and more according what you need by running similar command below:<\/p>\n<pre>sudo apt install php8.5-tidy php8.5-mysql php8.5-xml php8.5-zip<\/pre>\n<p>If you&#8217;re trying to upgrade php, and don&#8217;t even know what modules you need to install, then run command <code>apt list --installed |grep php<\/code> to tell all the modules you installed for old php version, and install the corresponding ones for php8.5.<\/p>\n<h3>Step 2: Apply Php 8.5 for Apache 2 or Nginx<\/h3>\n<p>After installed php 8.5, you also need to do something to make your web server to take use the new php version.<\/p>\n<p><b>For Apache 2<\/b>, if you have an old php (v8.3 for example) in use, then run the command below to disable it:<\/p>\n<pre>sudo a2dismod php8.3<\/pre>\n<p><i>In command replace <code>php8.3<\/code> with current running php version. For Ubuntu 22.04, it may be <code>php8.1<\/code><\/i>.<\/p>\n<p>Then, enable the new php version via command:<\/p>\n<pre>sudo a2enmod php8.5<\/pre>\n<p>Finally, replace the apache2 service to apply change:<\/p>\n<pre>sudo systemctl restart apache2.service<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-50070\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/enable-php85apache-700x438.webp\" alt=\"\" width=\"610\" height=\"382\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/enable-php85apache-700x438.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/enable-php85apache-300x188.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/enable-php85apache-768x481.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/enable-php85apache.webp 794w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/p>\n<p><b>For Nginx<\/b>, you may first run the command below to configure php-fpm:<\/p>\n<pre>sudo nano \/etc\/php\/8.5\/fpm\/pool.d\/www.conf<\/pre>\n<p>If you upgraded from old php, you may edit the file according to the old <code>\/etc\/php\/<b>8.3<\/b>\/fpm\/pool.d\/www.conf<\/code> file, though here I assume you had php8.3.<\/p>\n<p>Then, edit the nginx configuration file for you website:<\/p>\n<pre>sudo nano \/etc\/nginx\/sites-available\/default<\/pre>\n<p>Here, replace <b>default<\/b> depends on which file in the directory you use for your website.<\/p>\n<p>When file opens, scroll down and find out the <b>location ~ \\.php$ {}<\/b> section, and set <b>fastcgi_pass unix:\/run\/php\/php8.5-fpm.sock;<\/b> If you configured php-fpm to listen on the TCP port, then you may just leave the file unchanged.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-50071\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/php85-nginx-700x528.webp\" alt=\"\" width=\"610\" height=\"460\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/php85-nginx-700x528.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/php85-nginx-300x226.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/php85-nginx.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/p>\n<p>Finally, press <code>Ctrl+S<\/code> to save file and <code>Ctrl+X<\/code> to exit. And, restart both nginx and php-fpm to apply changes:<\/p>\n<pre>sudo systemctl restart nginx php8.5-fpm<\/pre>\n<p>Also, stop the old php-fpm service (version 8.3 for example):<\/p>\n<pre>sudo systemctl stop php8.3-fpm<\/pre>\n<h3>Verify:<\/h3>\n<p>To verify if the new php 8.5 is working, just create a simple php file under your domain&#8217;s root directory:<\/p>\n<pre>sudo nano \/var\/www\/html\/info.php<\/pre>\n<p>Here replace <code>\/var\/www\/html<\/code> if you set the domain root to another directory.<\/p>\n<p>When file opens, write the 3 lines below then save (press Ctrl+S, then Ctrl+X)<\/p>\n<pre>&lt;?php\r\nphpinfo ();\r\n?&gt;<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/info-php.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47638\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/info-php-700x476.webp\" alt=\"\" width=\"610\" height=\"415\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/info-php-700x476.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/info-php-300x204.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/info-php-768x522.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/info-php.webp 796w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>Finally, visit either <code>localhost\/info.php<\/code> from server itself or <code>your_domain\/info.php<\/code> in web browser to check php details.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-50072\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/php85-info-700x499.webp\" alt=\"\" width=\"610\" height=\"435\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/php85-info-700x499.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/php85-info-300x214.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/php85-info-768x548.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/11\/php85-info.webp 1050w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/p>","protected":false},"excerpt":{"rendered":"<p>PHP, the popular scripting language for web development, announced new 8.5 release few days ago. Here&#8217;s how to install or upgrade to Php 8.5 in Ubuntu 22.04 or Ubuntu 24.04 to work with Apache2 or Nginx web server. PHP 8.5 introduced a number of new features such as new &#8220;URI&#8221; extension, new pipe operator (|&gt;), [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":43436,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[745,748,746,2057],"class_list":["post-47635","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-apache","tag-nginx","tag-php","tag-web"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=47635"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47635\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/43436"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=47635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=47635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=47635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}