How to Install PHP on Linux Mint 22, 21 or 20

PHP is a popular and versatile server-side scripting language designed for web development. Known for its ease of use, flexibility, and compatibility, PHP powers millions of websites and applications worldwide. Key features of PHP include support for a wide range of databases, robust session management, comprehensive error handling, and extensive library support. It also offers powerful output buffering, data encryption, and integration with various web services, making it an essential tool for developers building dynamic and interactive web applications.

To install PHP on Linux Mint 22, 21, or 20, you can use the command-line terminal to access the official repositories. This guide will walk you through the steps to set up PHP on your Linux Mint system, ensuring you have the latest version and features.

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

Method 1: Install PHP via the Linux Mint Repository

The first method explains how to install PHP directly from the repository of your distribution version. Depending on your requirements, this version is often recommended as the first option.

Proceed with PHP Installation

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.

Optional: PHP Installation Extensions

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

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.

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.

Check PHP Status

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

sudo systemctl status php8.1-fpm

Replace php8.1-fpm with the specific version of PHP FPM installed on your system. If PHP FPM is running correctly, its status will be active (running).

Method 2: Install PHP via PPA

This section will guide you through 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.

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.

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 can access the most recent PHP packages available in the PPA.

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.

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.

Install PHP Extensions

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

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

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

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:

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.

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.

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.

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.

Conclusion: Installing PHP on Linux Mint

With PHP successfully installed on your Linux Mint system, you can leverage its powerful features for web development. Ensure you regularly update PHP to benefit from the latest security patches and enhancements. Explore PHP’s extensive library and framework support to build dynamic and interactive web applications efficiently. Enjoy the flexibility and robustness that PHP brings to your development projects.

Leave a Comment