Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save programmerShinobi/33f2fcc7ec0e33d8bec8de8b3521aa20 to your computer and use it in GitHub Desktop.
Save programmerShinobi/33f2fcc7ec0e33d8bec8de8b3521aa20 to your computer and use it in GitHub Desktop.
How to install PHP & Composer with extension (sqlsrv & pdo_sqlsrv) on Linux (Ubuntu 22.04)

Install PHP and Composer

1. Install PHP

Update and upgrade the system:

$ sudo apt update
$ sudo apt upgrade

Install necessary packages

$ sudo apt-get install unixodbc-dev libodbc1 odbcinst odbcinst1debian2 build-essential autoconf

Install alternative packages if the previous install fails

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

Install software-properties-common and other utilities

$ sudo apt install software-properties-common curl gnupg2 wget -y

Add PHP repository

$ sudo add-apt-repository ppa:ondrej/php

Update and upgrade system packages

$ sudo apt update
$ sudo apt upgrade

Install PHP 8.0

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

Install PHP 7.4

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

Install PHP 7.3

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

Install PHP 7.2

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

Install Microsoft ODBC Driver (Optional)

$ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
$ sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg

Install Microsoft ODBC Driver for SQL Server

$ cat /etc/apt/sources.list.d/mssql-release.list

expected output:

deb [arch=amd64] https://packages.microsoft.com/ubuntu/22.04/prod jammy main

if not present, add the following line to your /etc/apt/sources.list.d/mssql-release.list:

$ echo "deb [arch=amd64] https://packages.microsoft.com/ubuntu/22.04/prod jammy main" | sudo tee /etc/apt/sources.list.d/mssql-release.list
$ sudo apt-get update

Next, install SQL Server:

$ ACCEPT_EULA=Y sudo apt-get -y --no-install-recommends install msodbcsql17

Install SQLSRV & PDO_SQLSRV extension for PHP

$ sudo pecl install sqlsrv-5.8.1
$ sudo pecl install pdo_sqlsrv-5.8.1

Verifying the pdo_sqlsrv and sqlsrv Extensions

After installing the pdo_sqlsrv and sqlsrv extensions, follow these steps to ensure they are properly loaded and functioning in your PHP environment:

a. Check the Installed Extensions

List the loaded PHP extensions to confirm that pdo_sqlsrv and sqlsrv are included:

$ php -m | grep -i sqlsrv

Expected output:

pdo_sqlsrv
sqlsrv

If both extensions are listed, they are successfully loaded.

b. Verify Using phpinfo

Run the following command to display detailed information about your PHP environment and check for pdo_sqlsrv and sqlsrv:

$ php -r "phpinfo();" | grep -i sqlsrv

Expected output:

pdo_sqlsrv
pdo_sqlsrv support => enabled
pdo_sqlsrv.client_buffer_max_kb_size => 10240 => 10240
sqlsrv
sqlsrv support => enabled
sqlsrv.ClientBufferMaxKBSize => 10240 => 10240

This confirms the extensions are loaded and configured correctly.

c. Troubleshooting

  • If you encounter warnings such as PHP Startup: Unable to load dynamic library, ensure the extensions are correctly specified in your php.ini:

    $ sudo nano /etc/php/7.x/cli/php.ini  # Replace 7.x with your PHP version
    add the following lines: `extension=pdo`, `extension=pdo_sqlsrv.so` and `extension=sqlsrv.so`
  • Double-check that the .so files exist in the correct directory:

    $ ls /usr/lib/php/
    $ ls /usr/lib/php/20180731/

    Verify that both pdo_sqlsrv.so and sqlsrv.so are present.

Resolving the Warning: "Module 'PDO' already loaded"

$ php -m

The warning "Module 'PDO' already loaded in Unknown on line 0" occurs because the pdo module is being loaded multiple times in the PHP configuration. Follow these steps to resolve it:

a. Check Additional Configuration Files (conf.d)

PHP also uses additional configuration files, typically located in a directory like /etc/php/7.x/cli/conf.d. Ensure that the pdo module is not being reloaded from one of these additional files.

  • List the configuration files:

    $ ls /etc/php/7.x/cli/conf.d # Replace 7.x with your PHP version
  • Look for files such as 10-pdo.ini that may contain:

    extension=pdo.so
  • If extension=pdo.so is also enabled in this file, you can either:

    • Comment out the entry by adding ; at the beginning of the line:
      ; extension=pdo.so
    • Or, remove the configuration file if it is not needed:
      $ sudo rm /etc/php/7.3/cli/conf.d/10-pdo.ini

b. Clear the OPcache (If Enabled)

If OPcache is enabled, clear the cache to ensure the changes are applied:

php -r "opcache_reset();"

c. Verify the Changes

After editing the configuration, check to ensure the warning no longer appears:

$ php -m | grep PDO
$ php -r "phpinfo();" | grep -i pdo_sqlsrv

Update the system again:

$ sudo apt update
$ sudo apt upgrade

Check PHP version

$ php -v

You should see the PHP version in the following output:

PHP 7.3.33-24+ubuntu22.04.1+deb.sury.org+1 (cli) (built: Dec 24 2024 07:05:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.33-24+ubuntu22.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

2. Changing the PHP Default Version

To switch between installed PHP versions:

$ sudo update-alternatives --config php

You will be prompted to select the PHP version:

There are 4 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.0   80        auto mode
  1            /usr/bin/php7.2   72        manual mode
  2            /usr/bin/php7.3   73        manual mode
  3            /usr/bin/php7.4   74        manual mode
  4            /usr/bin/php8.0   80        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

The output should look like this:

update-alternatives: using /usr/bin/php7.2 to provide /usr/bin/php (php) in manual mode

3. Install Composer

Update the system and install necessary packages:

$ sudo apt update && sudo apt upgrade -y
$ sudo apt install git unzip -y

Download and install Composer:

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

Add Composer to the system PATH:

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

The output should show Composer details:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /    
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/     
                    /_/                          

Composer version 2.8.4 2024-12-11 11:57:47

Now PHP and Composer are installed and configured on your system!

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