PHP is one of the most widely used server-side scripting languages, crucial for web development. If you’re working with Ubuntu and need to upgrade or install PHP, this guide will walk you through the process.
Step 1: Check the Current Version of PHP
Before making any changes, it’s always a good idea to check which version of PHP is already installed. Open a terminal and run:
This command will display the current PHP version installed on your system. If PHP is not installed, you will receive a command-not-found message.
Step 2: Update Your Package List
Before upgrading or installing new software, it’s always a good idea to update your package lists. Run the following command to make sure your system is up-to-date:
Step 3: Install PHP (If Not Already Installed)
If PHP is not yet installed on your system, you can install it by running:
This command installs the default version of PHP available in the Ubuntu repository, which might not be the latest one. You can also specify a specific version, for example:
Replace 7.4 with the version you wish to install.
Step 4: Adding a PPA for Newer PHP Versions
If the version available in the default Ubuntu repository is outdated and you need a newer PHP version (like PHP 8.0 or 8.1), you’ll need to add a third-party PPA repository. One commonly used repository for this is maintained by Ondřej Surý.
To add the repository, run:
Now you can install newer PHP versions.
Step 5: Install or Upgrade to a Specific PHP Version
Once the repository is added, you can install a specific version of PHP. For example, to install PHP 8.1, you would run:
To install additional PHP modules (for example, php8.1-mysql, php8.1-xml, php8.1-curl, etc.), you can use the following command:
Step 6: Set the Default PHP Version
If you have multiple PHP versions installed on your system and want to switch between them, you can use the update-alternatives command. To configure the default PHP version, run:
Replace php8.1 with the version you want to use as the default. You can check the PHP version that is now set as default with:
Step 7: Restart Apache or Nginx
If you are using Apache or Nginx to serve your web applications, you need to restart the web server for the changes to take effect.
For Apache:
For Nginx:


