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
You can install multiple versions of PHP. Run the block corresponding to the version you need.
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
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
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
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
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
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
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
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
If you receive a warning that the PDO module is already loaded, it means PHP is loading it twice.
- Check for duplicate configuration files:
ls /etc/php/7.2/cli/conf.d
- 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
- Reset OPcache and verify:
php -r "opcache_reset();"
php -m | grep PDO
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