Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»PHP»How To Install PHP (8.2, 8.1, 7.4) on Ubuntu 22.04

    How To Install PHP (8.2, 8.1, 7.4) on Ubuntu 22.04

    By RahulFebruary 3, 20233 Mins Read

    PHP is a programming language used for developing web applications. You must install PHP packages on a Ubuntu system to run the application written on it. Generally, it is used to create e-commerce websites, blogs, and API applications. If you’re looking for an easy way to install PHP on Ubuntu 22.04, look no further. This blog post will show you how to do it quickly and easily.

    Advertisement

    We will use the Ondrej PPA for installing PHP on Ubuntu 22.04 LTS system. Which contains PHP 8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0 & PHP 5.6 packages. You can install any of the versions required for your application. The new application developers are suggested to use the latest PHP version ie PHP 8.2.

    In this tutorial, you will learn how to install PHP on Ubuntu 22.04 LTS system. This tutorial is also compatible with Ubuntu 20.04 LTS, and Ubuntu 18.04 LTS systems.

    Before We Begin

    First, log in to Ubuntu 22.04 via the console. Then update the Apt cache and upgrade the current packages of the system using the following command:

    sudo apt update && sudo apt upgrade 
    

    When prompted, press ‘y’ to confirm the installation.

    Installing PHP on Ubuntu 22.04

    PHP installation on Ubuntu systems is pretty straightforward. You just need to add the required PPA and you can install any PHP version on the Ubuntu system.

    Follow these steps to complete PHP installation on Ubuntu:

    1. Install a few dependencies required by this tutorial with the below-mentioned command:
      sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https 
      
    2. Add the Ondrej PPA to your system, which contains all versions of PHP packages for Ubuntu systems.
      LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/php 
      
    3. Now, update the Apt package manager cache.
      sudo apt update 
      
    4. The SURY repository contains PHP 8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0 & PHP 5.6. As the latest stable version of PHP is 8.2, but a large number of websites still required PHP 7. You can install any of the required PHP versions on your system.
      • Install PHP 8.2:
        sudo apt install php8.2 
        
      • Install PHP 8.1:
        sudo apt install php8.1 
        
      • Install PHP 7.4:
        sudo apt install php7.4 
        
      • Install PHP 5.6 (EOL):
        sudo apt install php5.6 
        

      Replace version 8.2, 8.1, 7.4, or 5.6 with the required PHP version to install on Ubuntu. Even you can install multiple PHP versions on a single Ubuntu system.

    5. Most PHP applications depend on various extensions to extend their features. That can also be installed using the following syntax:
      sudo apt install php8.2-[extension]
      

      Replace [extension] with the extension you want to install, if you want to add multiple extensions then include them in braces, I am going to install “php-mbstring, php-mysql, php-xml, and php-curl” by running the below-mentioned command:

      sudo apt install php8.2-mysql php8.2-mbstring php8.2-xml php8.2-curl 
      

      Users who have installed different PHP versions, need to replace 8.2 with the required PHP versions.

    Check Active PHP Version

    Now after installation verify that the correct version of PHP is installed by checking the version number by the below-mentioned command:

    php -v 
    
    Output:
    PHP 8.2.1 (cli) (built: Jan 13 2023 10:43:08) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.1, Copyright (c) Zend Technologies with Zend OPcache v8.2.1, Copyright (c), by Zend Technologies

    About PHP Configuration Files

    The PHP configuration files are stored under /etc/php directory with the version numbers. For example, all the configuration files related to PHP 8.2 are located below:

    1. Main PHP configuration file location:
      • PHP CLI: /etc/php/8.2/cli/php.ini
      • Apache: /etc/php/8.2/apache2/php.ini
      • PHP FPM: /etc/php/8.2/fpm/php.ini
    2. All the installed PHP modules are stored under /etc/php/8.2/mods-available directory.
    3. PHP Active modules configuration directory location:
      • PHP CLI: /etc/php/8.2/cli/conf.d/
      • Apache: /etc/php/8.2/apache2/conf.d/
      • PHP FPM: /etc/php/8.2/fpm/conf.d/

    To check files for the other PHP versions, just change the PHP version number (8.2 in the above example) in the files and directory path.

    Switch Default PHP Version for CLI

    You can use an `update-alternatives` command to set the default PHP version. Use this tutorial to read more details about switching the PHP version for CLI and Apache.

    sudo update-alternatives --config php
    
    There are 4 choices for the alternative php (providing /usr/bin/php).
    
      Selection    Path             Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/php8.1   81        auto mode
      1            /usr/bin/php5.6   56        manual mode
      2            /usr/bin/php7.4   74        manual mode
      3            /usr/bin/php8.0   80        manual mode
      4            /usr/bin/php8.1   81        manual mode
      5            /usr/bin/php8.2   82        manual mode
    
    Press  to keep the current choice[*], or type selection number: 2
    

    The above output shows all the installed PHP versions on your system. Selection number 2 set PHP 7.4 as the default PHP version for the command line.

    Uninstalling PHP

    If any PHP version is no more required, it can be removed from the system. That will free the disk space as well as system security.

    To uninstall any PHP version just type:

    sudo apt remove php5.6 
    

    Also, uninstall all the modules for that version with the following command:

    sudo apt remove php5.6-* 
    

    Conclusion

    This tutorial provides you with the instructions to install PHP on Ubuntu 22.04. The Ondrej PPA allows us to install PHP on Ubuntu systems quickly. It also allows us to install multiple PHP versions on a single system. You can switch to any PHP version as default anytime with the update-alternative utility.

    PHP PHP 5.6 PHP 7.4 PHP 8 PHP 8.1 PHP 8.2 Ubuntu 22.04
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Installing PHP on Pop!_OS

    How to Install PHP 8.x on Pop!_OS

    Managing Dependencies with Composer: A Beginner’s Guide

    10 Simple Ways to Speed Up Your WordPress Website

    10 Simple Ways to Speed Up Your WordPress Website

    View 6 Comments

    6 Comments

    1. john on December 9, 2022 10:18 pm

      how can I install 5.6.11

      Reply
    2. Matthew on November 21, 2022 3:37 am

      Thanks for this!

      Reply
    3. Brian on November 10, 2022 5:12 pm

      Thank you very much for hosting these and for this article. One question, though. I’m also looking for PHP7-cgi (and/or PHP5-cgi) since I need /usr/bin/php-cgi for lighttpd for use with the older versions of PHP on Jammy. However, I see this repository doesn’t seem to have them in there. Something you may want to consider in the future since I have to go see how to install that elsewhere. For plain PHP, this was great, though, and thanks again.

      Reply
      • Brian on November 10, 2022 8:45 pm

        As a follow-up, it installed just fine after using your instructions here. Just needed to apt-get install php5.6-cgi. Thanks again.

        Reply
    4. Everett Vincent on September 28, 2022 4:03 pm

      Thank you. This was very clear and useful.

      Reply
    5. Gustavo Silva on August 17, 2022 12:19 am

      Very good and helpful.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    • sleep Command in Linux with Examples
    • 20 Basic Linux Commands for the Beginners (Recommended)
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.