Upgrade Your PHP Game: Effortlessly Set Default PHP Version in Ubuntu with Powerful Code Examples.

Table of content

  1. Introduction
  2. Why Setting Default PHP Version is Important?
  3. Checking Current PHP Version in Ubuntu
  4. Installing Different Versions of PHP in Ubuntu
  5. Setting Default PHP Version with Apache 2
  6. Setting Default PHP Version with Nginx
  7. Troubleshooting Common Issues
  8. Conclusion


When it comes to PHP development, Ubuntu is a popular choice for developers to work in. However, setting up and changing the default PHP version in Ubuntu can be a challenging task. This is especially true for beginners who may not be familiar with Ubuntu's command-line interface. Thankfully, there are ways to streamline this process and make it effortless. In this article, we'll cover the basics of how to set up and manage default PHP versions in Ubuntu using simple and powerful code examples. By the end, you should have a clear understanding of how to manage PHP versions in Ubuntu and a handy reference guide for future use. So let's get started!

Why Setting Default PHP Version is Important?

PHP is a popular scripting language used to develop web applications. However, every version of PHP has its own set of features, performance improvements and security patches. Therefore, it is important to ensure that your web applications run on the right PHP version.


Different web applications require a different version of PHP for compatibility reasons. If your application is built on an older version of PHP, upgrading to a newer version may cause compatibility issues. Therefore, it is important to ensure that the required PHP version is installed and set as the default version.


Keeping up with the latest version of PHP ensures the security of your web application. Older versions of PHP are more susceptible to security vulnerabilities, which can leave your application and its data unprotected. Upgrading to the latest PHP version ensures your application is secure.


Each version of PHP has performance improvements that can impact the speed of your application. Upgrading to a newer version may result in faster load times and better overall performance.

Setting the appropriate version of PHP as default is essential for compatibility, security and performance of your web application.

Checking Current PHP Version in Ubuntu

Before upgrading your PHP game in Ubuntu, it is necessary to know the current PHP version installed on your system. You can easily check this by using the command line interface (CLI). Follow the steps below:

  1. Open the terminal by pressing Ctrl + Alt + T or searching for "terminal" in the Activities menu.

  2. Type the command php -v and press Enter.

  3. The terminal output will display the current PHP version installed on the system, which should look something like this:

    PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS )

    The version number is displayed in the format PHP <version number>.

  4. You can also check the version number of specific PHP modules installed on the system by running the command php -m. This will display all the installed modules along with their respective version numbers.

    [PHP Modules]
    [Zend Modules]

    In this example, "bcmath" is one of the installed modules and no version number is shown beside it. This means that it is using the version of PHP that is currently installed on the system.

By following these simple steps, you can easily check the current PHP version installed on your Ubuntu system. This information is crucial for identifying what version you may need to upgrade to and whether your system is compatible with certain PHP modules or applications.

Installing Different Versions of PHP in Ubuntu

In order to install different versions of PHP in Ubuntu, you can use the ondrej/php PPA (Personal Package Archive) repository. This repository contains a variety of PHP versions that you can install with just a few simple commands.

Here are the steps to install a specific version of PHP (in this example, PHP 7.4) on Ubuntu:

  1. Add the ondrej/php PPA repository to your system:
sudo add-apt-repository ppa:ondrej/php
  1. Update your package list:
sudo apt update
  1. Install the specific version of PHP you want:
sudo apt install php7.4
  1. Verify that the correct version of PHP is installed by checking the version number:
php -v

You can repeat these steps for any version of PHP available in the ondrej/php repository. This makes it easy to have multiple versions of PHP installed on your system and switch between them as needed.

Keep in mind that running multiple versions of PHP on the same system can sometimes cause conflicts, so it's a good idea to test your applications thoroughly after upgrading or downgrading PHP versions.

Setting Default PHP Version with Apache 2

One of the most common web servers in use today is Apache 2, and it is also widely used for PHP development. To set the default PHP version for Apache 2, follow these simple steps:

  1. First, check which versions of PHP are installed on your system using the following command:

    $ sudo update-alternatives --list php
  2. Once you know which versions of PHP you have installed, you can set the default version by entering the following command:

    $ sudo a2dismod phpX.X
    $ sudo a2enmod phpY.Y
    $ sudo service apache2 restart

    Replace X.X with the version number you want to disable, and Y.Y with the version number you want to enable.

  3. Finally, you can verify that the correct version of PHP is being used by creating a PHP info file and checking its output. To create a PHP info file, create a new file in your web root directory called phpinfo.php, and add the following code:


    Then, browse to http://localhost/phpinfo.php in your web browser, and look for the version information in the output.

Setting the default PHP version for Apache 2 is a simple process that can greatly improve your PHP development experience. By following these steps, you can ensure that your code is using the correct version of PHP, which will help you avoid compatibility issues and create more efficient and effective web applications.

Setting Default PHP Version with Nginx

When using the Nginx web server, setting the default PHP version can be done through a configuration file. Here's how to do it:

  1. First, locate your Nginx configuration file. This can typically be found in /etc/nginx/ or /usr/local/etc/nginx/.

  2. Open the file using your preferred text editor. The file name may vary depending on your setup, but it should end with .conf.

  3. Inside the http block, add the following code:

    # Set default PHP version
    location ~ \.php$ {
        fastcgi_pass   unix:/run/php/php<version>-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;

    Replace <version> with the version of PHP you want to use. For example, if you want to use PHP 7.4, the line should read:

    fastcgi_pass   unix:/run/php/php7.4-fpm.sock;
  4. Save the file and exit your text editor.

  5. Restart Nginx to apply the changes:

    sudo systemctl restart nginx

That's it! Now, any PHP files served by Nginx will use the specified version of PHP by default.

Troubleshooting Common Issues

When upgrading your PHP game, you may run into some common issues. Here are a few troubleshooting tips to help you navigate problematic scenarios:

Issue 1: php-fpm.service fails to start

If the php-fpm.service fails to start after setting your default PHP version, here are some steps you can take to troubleshoot:

  • Check the error logs by running sudo journalctl -xe and look for any errors related to php-fpm.service.
  • Verify that the listen.owner and listen.group directives in your PHP-FPM pool configuration file match the user and group that owns the /run/php/php{version}-fpm.sock file.
  • If you've made changes to your php-fpm configuration files, verify that they are valid and free of syntax errors by running sudo php-fpm -t.

Issue 2: PHP files aren't executing

If PHP files aren't executing after upgrading your PHP version, here are some steps you can take to troubleshoot:

  • Verify that PHP is enabled in your web server configuration file by adding the following code to your .htaccess file: AddHandler php{version} .php.
  • Double-check that you've installed all necessary PHP extensions for your application. You can do this by running sudo apt-get install php{version}-<extension>.
  • Ensure that your php.ini file is configured correctly by checking that the short_open_tag option is set to On and that the error_reporting directive is set to the appropriate level for your application.

Issue 3: PHP modules aren't loading

If PHP modules aren't loading after upgrading your PHP version, here are some steps you can take to troubleshoot:

  • Ensure that the modules are installed by running sudo apt-get install php{version}-<module>.
  • Check that the module is enabled in your php.ini file by uncommenting the appropriate line.
  • Restart PHP-FPM and your web server to ensure that the changes have taken effect.

By following these troubleshooting tips, you can easily overcome common issues encountered when upgrading your PHP game in Ubuntu.


Setting default PHP version in Ubuntu may seem like a daunting task, but with the right tools and knowledge, it can be achieved effortlessly. Whether you prefer to use the command line or a graphical user interface, there are a variety of options to choose from.

As we have seen through the examples provided, using either update-alternatives or php-switch command is a simple and effective way to switch between different PHP versions. Additionally, using php-versions tool, you can easily install and manage multiple PHP versions on your system, and use them for different applications.

Whether you are a beginner or an experienced developer, understanding how to manage PHP version is an essential skill. It can help you optimize your web applications and avoid compatibility issues. Hopefully, the information provided in this article has been helpful in upgrading your PHP game. Keep practicing and experimenting with different tools, and you'll soon become a master of managing PHP versions on Ubuntu.

As a developer, I have experience in full-stack web application development, and I'm passionate about utilizing innovative design strategies and cutting-edge technologies to develop distributed web applications and services. My areas of interest extend to IoT, Blockchain, Cloud, and Virtualization technologies, and I have a proficiency in building efficient Cloud Native Big Data applications. Throughout my academic projects and industry experiences, I have worked with various programming languages such as Go, Python, Ruby, and Elixir/Erlang. My diverse skillset allows me to approach problems from different angles and implement effective solutions. Above all, I value the opportunity to learn and grow in a dynamic environment. I believe that the eagerness to learn is crucial in developing oneself, and I strive to work with the best in order to bring out the best in myself.
Posts created 294

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top