Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save programmerShinobi/cd8e59ac7ded0a3a06542f059018aa64 to your computer and use it in GitHub Desktop.

Select an option

Save programmerShinobi/cd8e59ac7ded0a3a06542f059018aa64 to your computer and use it in GitHub Desktop.
Guide: Install PHP, Composer, and SQL Server Extensions on Ubuntu 22.04

Guide: Install PHP, Composer, and SQL Server Extensions on Ubuntu 22.04

1. System Preparation and Dependencies

Update your system and install the required build tools and ODBC packages.

sudo apt update
sudo apt install -y unixodbc-dev libodbc1 odbcinst odbcinst1debian2 build-essential autoconf

(Fallback: If the above fails, use the following alternative packages)

sudo apt install -y unixodbc-dev libodbccr2:i386 libodbc2:i386 libodbccr2 libodbc2 odbcinst libodbcinst2:i386 unixodbc-common libodbcinst2 build-essential autoconf

Install additional utilities and add the official PHP repository (Ondřej Surý):

sudo apt install -y software-properties-common curl gnupg2 wget
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

2. Install PHP

You can install multiple versions of PHP. Run the block corresponding to the version you need.

PHP 8.0

sudo apt install -y php8.0 php8.0-fpm php8.0-cli php8.0-mbstring php8.0-xml php8.0-gd php8.0-mysql php8.0-curl php8.0-ldap php8.0-zip php8.0-dev libtool

PHP 7.4

sudo apt install -y php7.4 php7.4-fpm php7.4-cli php7.4-mbstring php7.4-xml php7.4-gd php7.4-mysql php7.4-curl php7.4-ldap php7.4-zip php7.4-dev libtool

PHP 7.3

sudo apt install -y php7.3 php7.3-fpm php7.3-cli php7.3-mbstring php7.3-xml php7.3-gd php7.3-mysql php7.3-curl php7.3-ldap php7.3-zip php7.3-dev libtool

PHP 7.2

sudo apt install -y php7.2 php7.2-fpm php7.2-cli php7.2-mbstring php7.2-xml php7.2-gd php7.2-mysql php7.2-curl php7.2-ldap php7.2-zip php7.2-dev libtool

3. Set the Default PHP Version

To switch between installed PHP versions globally:

sudo update-alternatives --config php

Select the appropriate number for your target version (e.g., /usr/bin/php7.2) and press <Enter>.

Verify the active version:

php -v

4. Install Microsoft ODBC Driver for SQL Server

Add the Microsoft GPG key and repository:

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
echo "deb [arch=amd64] https://packages.microsoft.com/ubuntu/22.04/prod jammy main" | sudo tee /etc/apt/sources.list.d/mssql-release.list

Update package lists and install the driver:

sudo apt update
ACCEPT_EULA=Y sudo apt install -y --no-install-recommends msodbcsql17

5. Install SQLSRV & PDO_SQLSRV Extensions

Important: PECL extensions must be compiled against the correct PHP development headers. If you are targeting PHP 7.2, you must point the build tools to PHP 7.2 before running PECL.

Step 1: Align build tools with your target PHP version (e.g., PHP 7.2)

sudo update-alternatives --set phpize /usr/bin/phpize7.2
sudo update-alternatives --set php-config /usr/bin/php-config7.2

Step 2: Install via PECL

sudo pecl clear-cache
sudo pecl channel-update pecl.php.net
sudo pecl install sqlsrv-5.8.1
sudo pecl install pdo_sqlsrv-5.8.1

(Fallback: If PECL returns a "No releases available" error, install manually)

wget https://pecl.php.net/get/pdo_sqlsrv-5.8.1.tgz
sudo pecl install pdo_sqlsrv-5.8.1.tgz
rm pdo_sqlsrv-5.8.1.tgz

6. Configure and Enable Extensions

Once compiled, you must register the .so files with PHP and restart the FPM service. Replace 7.2 with your active PHP version if different.

Enable sqlsrv:

echo "extension=sqlsrv.so" | sudo tee /etc/php/7.2/mods-available/sqlsrv.ini
sudo phpenmod -v 7.2 sqlsrv

Enable pdo_sqlsrv:

echo "extension=pdo_sqlsrv.so" | sudo tee /etc/php/7.2/mods-available/pdo_sqlsrv.ini
sudo phpenmod -v 7.2 pdo_sqlsrv

Restart PHP-FPM:

sudo systemctl restart php7.2-fpm

Verify the Extensions:

php -m | grep -i sqlsrv

Expected Output:

pdo_sqlsrv
sqlsrv

Troubleshooting: "Module 'PDO' already loaded" Warning

If you receive a warning that the PDO module is already loaded, it means PHP is loading it twice.

  1. Check for duplicate configuration files:
ls /etc/php/7.2/cli/conf.d
  1. Locate any extraneous PDO configuration (like 10-pdo.ini) and remove or comment it out:
sudo rm /etc/php/7.2/cli/conf.d/10-pdo.ini
  1. Reset OPcache and verify:
php -r "opcache_reset();"
php -m | grep PDO

7. Install Composer

Download the Composer installer and move it to your local binaries for global access.

sudo apt install -y git unzip
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Add the Composer vendor directory to your system PATH:

echo 'export PATH="$PATH:$HOME/.config/composer/vendor/bin"' >> ~/.bashrc
source ~/.bashrc

Verify the installation:

composer -V
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment