Installing Multiple Versions of PHP on Ubuntu, Debian (also WSL).
Published: 2024-05-04 06:30:49

Installing multiple versions of PHP on your Ubuntu or Debian system can be incredibly useful for development and testing purposes. It allows you to seamlessly switch between different PHP versions to ensure compatibility with various projects or frameworks. In this tutorial, we'll walk through the steps to install multiple versions of PHP using a simple Bash script. By the end of this tutorial, you'll have a system with multiple PHP versions installed, ready to tackle any PHP development challenge that comes your way. Let's get started!


## Step 1: Prepare Your System

Before installing multiple versions of PHP, ensure that your system is up-to-date and has necessary dependencies installed. Open a terminal and run the following commands:

```bash
sudo apt update
sudo apt install software-properties-common
```

## Step 2: Download the Script

Copy the provided script into a file on your system. You can use any text editor to create the file. Let's name it install_php_versions.sh.

```bash
nano install_php_versions.sh
```
Paste the script into the file and save it.

## Step 3: Make the Script Executable

Make the script executable using the following command:

```bash
chmod +x install_php_versions.sh
```

## Step 4: Run the Script

Now, you can execute the script to install multiple versions of PHP. Run the following command:

```bash
./install_php_versions.sh
```
The script will add the Ondřej Surý PPA repository, update package lists, and install PHP versions 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, and 8.0.

## Step 5: Verify Installation

After the script completes, you can verify that all PHP versions are installed correctly by running:

```
php -v
```

![PHP Installed Version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qox9lrb2yaaxgmedv1v.png)

This command will display the version of PHP currently active in your system. You can also switch between installed PHP versions using appropriate commands depending on your needs.

## Step 6: Optional Configuration

If you need to set a default PHP version, you can use the update-alternatives command as indicated in the script. Replace X.X with the version you want to set as default.

## Step 7: Restart Web Server

If you have a web server installed (Apache or Nginx), the script will attempt to restart it after installing PHP versions. If you encounter any issues with your web server, restart it manually.

## Step 8: Finish

You have now successfully installed multiple versions of PHP on your system. You can use any installed version based on your project requirements.

That's it! You now have multiple versions of PHP installed on your Ubuntu or Debian system. If you have any questions or encounter any issues, feel free to ask for further assistance.

Below is the complete script for your reference:

```bash
#!/bin/bash

# Check if the Ondřej Surý PPA repository is already added
if ! grep -q "ondrej/php" /etc/apt/sources.list /etc/apt/sources.list.d/*; then
    echo "Adding Ondřej Surý PPA repository to get the latest PHP versions..."
    sudo add-apt-repository -y ppa:ondrej/php
    sudo apt update
fi

# Function to install a PHP version
install_php_version() {
    local version="$1"
    sudo apt install -y "php$version" "php$version-cli" "php$version-fpm"
}

# Start installation message
echo "Starting installation of PHP versions..."

# Install different PHP versions
versions=("5.6" "7.0" "7.1" "7.2" "7.3" "7.4" "8.0")
for version in "${versions[@]}"; do
    install_php_version "$version"
    echo "PHP $version installed successfully."
done

# Completion message
echo "All PHP versions have been installed successfully."

# Set default PHP version (optional)
# sudo update-alternatives --set php /usr/bin/phpX.X  # Replace X.X with desired version

# Check if Apache is installed and restart the web server
if command -v apache2ctl &>/dev/null; then
    sudo systemctl restart apache2
    echo "Apache restarted."
elif command -v nginx &>/dev/null; then
    sudo systemctl restart nginx
    echo "Nginx restarted."
else
    echo "No web server detected."
fi

# Check installed PHP versions
php -v

# Check all installed PHP versions
dpkg -l | grep php | awk '{print $2}' | grep -E "^php[0-9]+\.[0-9]+$"


```