June 4, 2024

How to Install and Uninstall PHP on Ubuntu

mr rockstar

How to Install and Uninstall PHP on Ubuntu
Cheap Dedicated Server

1. Introduction

PHP (Hypertext Preprocessor) is a widely-used open-source general-purpose scripting language that is especially suited for web development. It can be embedded into HTML, making it a versatile choice for web developers. This tutorial will guide you through installing and uninstalling PHP on Ubuntu, ensuring you can manage your development environment effectively.

2. Prerequisites

Before we begin, make sure you have the following:

  • A running Ubuntu system (20.04 or later recommended).
  • A user account with sudo privileges.
  • Access to the terminal.

3. Installing PHP on Ubuntu

Updating Package List

First, ensure your package list is up-to-date. Open the terminal and run:

sudo apt update

      

Installing PHP

To install PHP, use the following command. This command installs PHP along with common modules:

sudo apt install php libapache2-mod-php php-mysql

This command installs PHP along with the libapache2-mod-php module for integrating with the Apache web server and php-mysql for MySQL database support.

Verifying Installation

Once the installation is complete, verify it by checking the PHP version:

php -v

You should see output similar to:

PHP 8.0.2 (cli) (built: Feb 4 2021 18:36:41) ( NTS )

Installing PHP Extensions

PHP extensions add additional functionality to your PHP environment. Here are a few commonly used extensions:

  • php-curl: To handle URL requests.
  • php-gd: For image processing.
  • php-json: For JSON data manipulation.
  • php-mbstring: For multibyte string processing.
  • php-xml: For XML parsing.

To install these extensions, run:

sudo apt install php-curl php-gd php-json php-mbstring php-xml

4. Uninstalling PHP on Ubuntu

Removing PHP

To uninstall PHP and its related packages, use the following command:

sudo apt remove php libapache2-mod-php php-mysql

If you have additional extensions, you can include them in the command:

sudo apt remove php-curl php-gd php-json php-mbstring php-xml

Cleaning Up

After removing PHP, clean up the package dependencies that are no longer required:

sudo apt autoremove

To ensure everything related to PHP is completely removed, you can also purge the configuration files:

sudo apt purge php libapache2-mod-php php-mysql

5. Conclusion

Managing PHP on your Ubuntu system is straightforward once you know the steps. Whether you’re installing PHP for a new project or uninstalling it from a server, this guide provides a comprehensive approach to handling PHP. Keeping your development environment clean and up-to-date is crucial for maintaining optimal performance and security.

If you encounter any issues or have additional questions, feel free to leave a comment below. Happy coding!

How to Install and Uninstall PHP on Ubuntu (F.A.Q)

What is the difference between apt remove and apt purge when uninstalling PHP?

  • apt remove: This command removes the PHP packages but retains the configuration files. This is useful if you plan to reinstall PHP later and want to keep your settings.
  • apt purge: This command removes both the PHP packages and their associated configuration files. Use this command if you want to completely remove PHP and all its traces from your system.

How do I switch between different PHP versions on Ubuntu?

If you need to switch between different PHP versions (for example, PHP 7.4 and PHP 8.0), you can use the update-alternatives command to set the desired version as the default.

  1. Install the desired PHP versions:
    sudo apt install php7.4 php8.0

  2. Configure update-alternatives:
    sudo update-alternatives --set php /usr/bin/php7.4

    To switch to PHP 8.0, run:

    sudo update-alternatives --set php /usr/bin/php8.0

  3. Verify the active version:
    php -v
 

How do I restart Apache after installing or uninstalling PHP?

After installing or uninstalling PHP, it’s a good idea to restart Apache to ensure the changes take effect:

sudo systemctl restart apache2

This command restarts the Apache service, incorporating any changes made to PHP.

How do I check which PHP modules are installed and enabled?

To list all installed PHP modules, use the following command:

php -m

This command displays a list of installed and enabled PHP modules. To check specific modules, such as curl or mbstring, look for their names in the output list.

Popular Blog Posts