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 and add PHP repository:

$ sudo apt install software-properties-common curl gnupg2 wget -y
$ sudo add-apt-repository ppa:ondrej/php
$ 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

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

Install PHP 7.3

$ sudo apt-get install php7.3 php7.3-fpm
$ 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

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
$ 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
$ sudo apt-get update
$ ACCEPT_EULA=Y sudo apt-get -y --no-install-recommends install msodbcsql17

Install SQLSRV extension for PHP

Download and install SQLSRV for PHP:

$ cd ~/Downloads
$ wget https://pecl.php.net/get/sqlsrv-5.8.1.tgz
$ tar -zxvf sqlsrv-5.8.1.tgz
$ cd sqlsrv-5.8.1
$ make clean
$ phpize
$ ./configure
$ make
$ sudo make install
$ sudo sh -c 'echo "extension=sqlsrv.so" >> $(php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||")/30-sqlsrv.ini'
$ php -m

Install PDO_SQLSRV extension for PHP

$ cd ~/Downloads
$ wget https://pecl.php.net/get/pdo_sqlsrv-5.8.1.tgz
$ tar -zxvf pdo_sqlsrv-5.8.1.tgz
$ cd pdo_sqlsrv-5.8.1
$ phpize
$ ./configure
$ make
$ sudo make install
$ sudo sh -c 'echo "extension=pdo_sqlsrv.so" >> $(php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||")/30-pdo_sqlsrv.ini'
$ php -m

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.2.34-51+ubuntu22.04.1+deb.sury.org+1 (cli) (built: Aug  2 2024 16:14:06) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.34-51+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.1 2024-10-04 11:31:01

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