A PHP Auto-Installer Script for Ubuntu
· Jerwin Arnado · 4 min read ·
Spinning up a new PHP version on a server is one of those tasks I do often enough to automate, but not often enough to remember every module name by heart. So I wrote a small Bash script that takes a version number and installs PHP along with the modules that 99% of modern applications actually need.
A Quick Reality Check First
When people automate this, the temptation is to install every available PHP module “just in case.” Don’t. There are hundreds of them. Many are highly specialized, some are deprecated, and a few will actively conflict with each other. You end up with a slower, messier install and nothing to show for it.
What you want instead is a sensible “kitchen sink” stack — the core extensions that Laravel, WordPress, and most custom apps need to run flawlessly. That’s exactly what this script installs.
The Script
Create the file:
nano install-php.sh
Paste in the following:
#!/bin/bash
# Check if a version was provided as an argument, otherwise ask for it
PHP_VER=$1
if [ -z "$PHP_VER" ]; then
read -p "Enter the PHP version you want to install (e.g., 8.1, 8.2, 8.3): " PHP_VER
fi
echo "=========================================="
echo " Preparing to install PHP $PHP_VER..."
echo "=========================================="
# 1. Add the Ondrej PPA (The official standard for Ubuntu PHP versions)
sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update
# 2. Define the "Kitchen Sink" module list
MODULES=(
"php$PHP_VER"
"php$PHP_VER-cli"
"php$PHP_VER-common"
"php$PHP_VER-fpm"
"php$PHP_VER-mysql"
"php$PHP_VER-zip"
"php$PHP_VER-gd"
"php$PHP_VER-mbstring"
"php$PHP_VER-curl"
"php$PHP_VER-xml"
"php$PHP_VER-bcmath"
"php$PHP_VER-intl"
"php$PHP_VER-readline"
"php$PHP_VER-opcache"
"php$PHP_VER-sqlite3"
)
# 3. Install the packages
echo "Installing PHP and common modules..."
sudo apt install -y "${MODULES[@]}"
echo "=========================================="
echo " PHP $PHP_VER installation complete! 🎉"
php$PHP_VER -v
echo "=========================================="
Save and exit (Ctrl+O, Enter, Ctrl+X), then make it executable:
chmod +x install-php.sh
How It Works
The script leans on the Ondřej Surý PPA, the de facto standard for getting current PHP versions on Ubuntu. It adds the repo, then maps the version number you pass into the full list of package names so you never have to type php8.3-mbstring by hand.
The module list covers the usual suspects: fpm for serving, mysql and sqlite3 for databases, mbstring, curl, xml, zip, gd, bcmath, intl, readline, and opcache. That’s the baseline Laravel and most CMS installs check for on boot.
Using It
There are two ways to run it.
Inline argument — pass the version on the command line:
./install-php.sh 8.3
Prompt — run it bare and it asks you for the version:
./install-php.sh
Either way it adds the repository, expands the version into every module name, installs the lot, and prints php -v at the end so you can confirm what landed. One script, every future PHP bump handled.