How to Upgrade PHP Version in Ubuntu

PHP is a popular programming language used for web development. It regularly releases updates with new features and performance improvements. So you may need to upgrade to PHP 7.4 or 8.0, to avail its new features and security improvements. Here are the steps to upgrade PHP version in Ubuntu. We will look at how to upgrade to PHP 7.4 in Ubuntu 18.04. You can use these steps to upgrade PHP 7.0 to PHP 7.4 or PHP 7.2 to PHP 7.4.

How to Upgrade PHP Version in Ubuntu

Here is how to upgrade to PHP 7.4 in ubuntu 18.04. There is no way to directly upgrade your existing PHP to PHP 7.4. You need to install PHP 7.4 and then change the system PHP to it.


1. Add PPA repository for PHP

PHP 7.4 is maintained by ondej/php repository. Run the following command to add it to our list of sources.

$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php

Also read : How to Setup FTP server in Ubuntu


2. Update Ubuntu

Open terminal and run the following commands to update Ubuntu packages.

$ sudo apt-get update

Also read : How to List all processes in Linux using systemctl


3. Install PHP 7.4

Install PHP 7.4 with the following command

$ sudo apt install php7.4

Check php version

$ sudo php -v

Also read : How to Redirect 403 to 404 in NGINX


4. Install PHP Extensions (Optional)

PHP extensions are commonly used along with PHP. Install them with the following command.

$ sudo apt install php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl -y

Also read : How to Configure X-Frame-Options in Apache


5. Enable PHP 7.4 for Apache

Next you need to disable your old PHP (say 7.0) and enable new PHP 7.4.

$ sudo a2dismod php7.0
$ sudo a2enmod php7.4

Also read : How to Install Erlang in Ubuntu


6. Restart Apache Server

Restart Apache web server to apply changes

$ sudo service apache2 restart

That’s it. Your PHP version will be updated to the latest version. If you want to update to PHP 8.0 in Ubuntu, follow the same steps but replace 7.4 with 8.0 in every command.