How to Install PHP on Linux Mint 21 or 20

PHP, a widely-used open-source scripting language, is essential for developing dynamic web pages. This guide will demonstrate how to install PHP on Linux Mint 21 or 20, offering a step-by-step approach to equip your system with this powerful tool. PHP’s versatility and efficiency make it a top choice for web developers. Here, you’ll learn not only the installation process but also gain insights into PHP’s key features.

Key Features of PHP:

  • Flexibility: PHP seamlessly integrates with various databases.
  • Efficiency: Fast performance, suitable for complex applications.
  • Ease of Use: Its syntax is understandable, making coding simpler.
  • Open Source: Free to use and has a vast community support.
  • Cross-Platform: Runs on multiple operating systems.

Transitioning into the technical aspects, the installation of PHP on Linux Mint is straightforward. With a few commands, you can set up PHP and begin exploring its extensive capabilities in web development. Let’s dive into the process of enhancing your web development environment.

Install PHP on Linux Mint 21 or 20 via APT

This section will walk you through installing PHP on Linux Mint using the default repository. We will cover each step in detail, explaining what each command does and why it’s necessary.

Step 1: Update Linux Mint Before PHP Installation

Before you begin, run the following commands to ensure your system is current. These commands will update the package index and upgrade installed packages to their latest versions:

sudo apt update
sudo apt upgrade

Step 2: Install PHP

Now, let’s install PHP by running the following command:

sudo apt install php

This command will download and install PHP and its required dependencies from the Linux Mint repository. Once the installation is complete, you’ll have PHP installed on your system.

Step 3: Install PHP FPM

PHP FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation that provides improved performance and resource management for PHP applications. To install PHP FPM, run the following command:

sudo apt install php-fpm

Step 4: Install PHP Extensions

PHP extensions are additional modules that extend the functionality of PHP. You can install specific extensions depending on your project requirements. For example, if you need to connect to a MySQL database, you’ll need to install the php-mysql extension.

To install a PHP extension, use the following command format:

sudo apt install php-[extension-name]

Replace [extension-name] with the name of the extension you want to install.

Step 5: Confirm PHP Installation

After installing PHP and any necessary extensions, you can verify the installation by checking the PHP version. Run the following command:

php -v

This command will display the installed PHP version, confirming that PHP has been installed correctly.

Step 6: Check PHP Status

To ensure that PHP FPM is running properly, you can check its status by running the following command:

sudo systemctl status php7.4-fpm

Replace php7.4-fpm with the specific version of PHP FPM installed on your system. If PHP FPM is running correctly, you’ll see its status as active (running).

Install PHP on Linux Mint 21 or 20 via PPA

In this section, we will guide you through the process of installing PHP using the Ondrey Sury Launchpad PPA. This method allows you to install the latest versions of PHP (8.0, 8.1, and 8.2) on your Linux Mint system. We will explain each command in detail, so you understand what is happening before and after executing them.

Step 1: Import PHP LaunchPAD PPA

First, you must import the Ondrey Sury Launchpad PPA, which provides the latest PHP packages. To do this, run the following command:

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

These commands will install the software-properties-common package (if not already installed) and add the Ondrey Sury PHP PPA to your system.

Step 2: Update Package List

After adding the PPA, update your package list to include the latest PHP packages from the PPA repository:

sudo apt update

This command ensures that your system has access to the most recent PHP packages available in the PPA.

Step 3: Install PHP 8.x

Now, you can choose which version of PHP you want to install. We will provide instructions for installing PHP 8.0, 8.1, 8.2 and 8.3.

Option 1: Install PHP 8.0

To install PHP 8.0, run the following command:

sudo apt install php8.0

This command will download and install PHP 8.0 and its required dependencies on your system.

Option 2: Install PHP 8.1

To install PHP 8.1, run the following command:

sudo apt install php8.1

This command will download and install PHP 8.1 and its required dependencies on your system.

Option 3: Install PHP 8.2

To install PHP 8.2, run the following command:

sudo apt install php8.2

This command will download and install PHP 8.2 and its required dependencies on your system.

Option 4: Install PHP 8.3

To install PHP 8.3, run the following command:

sudo apt install php8.3

This command will download and install PHP 8.3 and its required dependencies on your system.

Step 4: Install PHP-FPM

After installing the desired PHP version, install the corresponding PHP-FPM package to improve performance and resource management.

Option 1: Install PHP 8.0 FPM

To install PHP 8.0 FPM, run the following command:

sudo apt install php8.0-fpm

This command will install the PHP 8.0 FPM package, enabling your web server to handle PHP requests more efficiently.

Option 2: Install PHP 8.1 FPM

To install PHP 8.1 FPM, run the following command:

sudo apt install php8.1-fpm

This command will install the PHP 8.1 FPM package, enabling your web server to handle PHP requests more efficiently.

Option 3: Install PHP 8.2 FPM

To install PHP 8.2 FPM, run the following command:

sudo apt install php8.2-fpm

This command will install the PHP 8.2 FPM package, enabling your web server to handle PHP requests more efficiently.

Option 3: Install PHP 8.3 FPM

To install PHP 8.3 FPM, run the following command:

sudo apt install php8.3-fpm

This command will install the PHP 8.3 FPM package, enabling your web server to handle PHP requests more efficiently.

Step 5: Install PHP Extensions

Now, let’s install PHP extensions to extend the functionality of PHP for your specific project requirements.

Step 1: List PHP Extensions

To list available PHP extensions for a specific PHP version, run the following command:

sudo apt-cache search php8.2

Replace 8.2 with the version number of your installed PHP. This command will display a list of available extensions for PHP 8.2.

Alternatively, using the following command will list already enabled PHP extensions:

php -m

Step 2: Install PHP Extensions

To install a specific extension for PHP 8.2, use the following command format:

sudo apt install php8.2-[extension-name]

Replace [extension-name] with the name of the extension you want to install. For example, to install the mysqli extension for PHP 8.2, run:

sudo apt install php8.2-mysqli

Additional Commands & Tips for PHP on Linux Mint 21 or 20

This section will cover some useful commands and tips for managing multiple PHP versions on your Linux Mint system. We will explain each command and its purpose so you understand the process of switching between different PHP versions.

Switch PHP Alternative Versions

On Linux Mint, you can switch between different versions of PHP using the update-alternatives command. Here are the steps to switch to alternative PHP versions:

Step 1: Check the Current PHP Version

First, check the current PHP version active on your system by running the following command:

php -v

This command will display the active PHP version on your system.

Step 2: List Available PHP Versions

Use the following command to see a list of all available PHP versions on your system:

This command will show a list of installed PHP versions, allowing you to choose which version to switch to.

Step 3: Switch PHP Version

To switch to a different version of PHP, use the following command format:

sudo update-alternatives --set php /usr/bin/php[version]

Replace [version] with the desired PHP version number. For example, to switch to PHP 8.1 from 8.2, you would use the following command:

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

After executing the command, you will likely see an output similar to the following in your terminal:

update-alternatives: using /usr/bin/php8.1 to provide /usr/bin/php (php) in manual mode

This output confirms that you have successfully switched to the specified PHP version.

Step 4: Verify the New PHP Version

After switching to a different PHP version, you can verify the newly activated version by running the following command:

php -v

This command will display the currently active PHP version, confirming that you have successfully switched to the desired PHP version.

Following these steps, you can easily switch between different PHP versions on your Linux Mint system, allowing you to test your PHP applications with multiple PHP versions or adapt your development environment to specific project requirements.

Conclusion: Installing PHP on Linux Mint

In conclusion, installing PHP on Linux Mint is a straightforward process that can be accomplished using the default Linux Mint repository or the Ondrey Sury Launchpad PPA for the latest PHP versions. Following the steps in this guide will help you set up your development environment with the desired PHP version and extensions to match your project requirements. By understanding how to switch between different PHP versions and manage extensions, you can maintain a flexible and efficient development environment for your PHP projects.

Leave a Comment


Your Mastodon Instance
Share to...