4

I'm following these instructions to install the ODBC driver v18 on Ubuntu 22.04. I'm aware that the script does not want to install anything for Ubuntu 22.04 but I bet it's a typo because the repository for 22.04 does exist.

After issuing:

sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update

it does not find version 18:

$ apt-cache search msodbc
msodbcsql17 - ODBC Driver for Microsoft(R) SQL Server(R)

What is my fault here?

EDIT

Testing code after answer by mizera:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$SERVER = "192.168.0.50";
$DATABASE = "DB";
$DRIVER = "/usr/lib/libmsodbcsql-18.so";
const USERNAME = "user";
const PASSWORD = "pswd";

try {
    $db = new PDO("odbc:driver=$DRIVER;server=$SERVER;database=$DATABASE", USERNAME, PASSWORD);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo '<div class="alert alert-danger" role="alert">';
    echo "Connection to database failed:" . PHP_EOL . $e->getMessage();
    echo '</div>';

    var_dump($e);
}

The output is:

object(PDOException)#3 (8) { ["message":protected]=> string(21) "could not find driver" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(27) "/home/user/www/database.php" ["line":protected]=> int(25) ["trace":"Exception":private]=> array(2) { [0]=> array(5) { ["file"]=> string(27) "/home/user/www/database.php" ["line"]=> int(25) ["function"]=> string(11) "__construct" ["class"]=> string(3) "PDO" ["type"]=> string(2) "->" } [1]=> array(4) { ["file"]=> string(25) "/home/user/www/tables.php" ["line"]=> int(2) ["args"]=> array(1) { [0]=> string(27) "/home/user/www/database.php" } ["function"]=> string(7) "require" } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> NULL }
Mark
  • 477
  • 1
  • 9
  • 26
  • 3
    Thats a question for Microsoft - its possible the version 18 is not yet packaged for 22.04 in their repos – Thomas Ward May 09 '22 at 13:25
  • 1
    I made a [discussion](https://techcommunity.microsoft.com/t5/sql-server/odbc-drivers-for-ubuntu-22-04/m-p/3356858#M1595) on MS tech community about this subject – CaldeiraG May 12 '22 at 14:34

2 Answers2

4

I tried install from Ubuntu 21.10 package, and it is working (first step is important, because without it install cause error):

apt-get install odbcinst

sudo curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

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

apt update

apt install msodbcsql18

EDIT: My answer was about install driver, if you want to use with PHP, try these steps for PHP 8.1 (php-dev, pecl, php-pear etc):

    add-apt-repository ppa:ondrej/php -y
    apt-get update -y
    apt-get install php8.1-dev -y --allow-unauthenticated
    apt-get install unixodbc-dev -y
    apt install php-pear -y 
    pecl install pdo_sqlsrv
    pecl install sqlsrv
    printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.1/mods-available/sqlsrv.ini
    printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.1/mods-available/pdo_sqlsrv.ini
    phpenmod -v 8.1 sqlsrv pdo_sqlsrv
    systemctl restart apache2

EDIT2 And code for testing (against MSSql Server 2016):

    $conn = \sqlsrv_connect(
        $server, 
            [
                'Database' => $db, 
                'UID' => $user, 
                'PWD' => $pass, 
                'CharacterSet' => 'UTF-8', 
                'Encrypt' => 1, 
                'TrustServerCertificate' => 1
            ]
        );
    if ($conn === false) {
        echo "Unable to connect.</br>";
        die(print_r(\sqlsrv_errors(), true));
    }
mizera
  • 56
  • 2
  • 1
    I followed exactly your instruction and I have `/usr/lib/libmsodbcsql-18.so` that is a symlink to `/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.0.so.1.1`. Setting either one to the code above leads to the "driver not found error" (see question updated). I have to change anything in the code? – Mark May 19 '22 at 05:58
  • May you share your testing code to understand why it does not work on my system? – Mark May 19 '22 at 06:11
0

I'm trying to install msodbcsql18 package on a Jammy 22.04 docker container and Google brings here. It seems as of June 2023 the package is already there. lsb-base and apt-key commands fail and you can install it as following:

curl -sS https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/mssql.gpg
curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
apt-get update
ACCEPT_EULA=Y apt-get install -y msodbcsql18

You most likely need to use sudo or su. This is from CI which already runs as root