Sql server 2022 chạy trên linux

Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Install SQL Server 2022 Machine Learning Services (Python and R) on Linux

  • Article
  • 09/15/2022
  • 5 minutes to read

In this article

Applies to:

Sql server 2022 chạy trên linux
SQL Server 2022 (16.x) Preview - Linux

This article guides you in the installation of SQL Server Machine Learning Services on Linux. Python and R scripts can be executed in-database using Machine Learning Services.

You can install Machine Learning Services on Ubuntu. Currently, Red Hat Enterprise Linux (RHEL), SUSE Linux Enterprise Server (SLES) are unsupported.

You can install ML Services on a Docker container running a Linux distribution. Inside the Docker container, the steps would be the same as below.

For more information, see the Supported platforms section in the installation guidance for SQL Server on Linux.

Pre-install checklist

  • Install SQL Server on Linux and verify the installation.

  • Check the SQL Server Linux repositories for the Python and R extensions. If you already configured source repositories for the database engine install, you can run the mssql-server-extensibility package install commands using the same repo registration.

  • You should have a tool for running T-SQL commands.

    • You can use Azure Data Studio, a free database tool that runs on Linux, Windows, and macOS.

Package list

On an internet-connected device, packages are downloaded and installed independently of the database engine using the package installer for each operating system. The following table describes all available packages, but for R and Python, you specify packages that provide either the full feature installation or the minimum feature installation.

Available installation packages for SQL Server 2022 (16.x) Preview on Linux:

Package nameApplies-toDescription
mssql-server-extensibility All Extensibility framework used to run Python and R.

Install on Ubuntu

Refer to Quickstart: Install SQL Server and create a database on Ubuntu for SQL Server 2022 (16.x) Preview on Linux installation. Then, use the following steps to install SQL Server Machine Learning Services on Ubuntu:

  • Install R on Ubuntu
  • Install Python on Ubuntu

Install R on Ubuntu

The following commands register the repository providing the R language platform.

Tip

If possible, run apt-get update to refresh packages on the system prior to installation.

  1. Begin installation as root.

    sudo su
    
  2. Optionally, if your system does not have the https apt transport option:

    apt-get install apt-transport-https
    
  3. Set the location of the package repo the "prod" directory containing the distribution. This example specifies 20.04. Replace with 16.04 or 14.04 if you want those versions.

    wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb
    
  4. Register the repository.

    dpkg -i packages-microsoft-prod.deb
    
  5. Update packages on your system (required).

    apt-get update
    
  6. Install the SQL Server extensibility feature.

    apt-get install mssql-server-extensibility
    
  7. Download and install the version of R that is desired. Choose a version of R 4.2 or higher, available for download directly from cran.r-project.org. Follow the instructions for the desired runtime.

  8. Install CompatibilityAPI and RevoScaleR dependencies. From the R terminal of the version you have installed, run the following:

    # R Terminal
    install.packages("iterators")
    install.packages("foreach")
    install.packages("R6")
    install.packages("jsonlite")
    
  9. Download the CompatibilityAPI for Linux.

  10. Install CompatibilityAPI for Linux, specifying the absolute file path to the .tar.gz file.

    install.packages('/path/to/CompatibilityAPI.tar.gz', repos = NULL)
    
  11. Download RevoScaleR Linux.

  12. Install RevoScaleR for Linux, specifying the absolute file path to the .tar.gz file.

    install.packages('/path/to/RevoScaleR.tar.gz', repos = NULL)
    
  13. Verify RevoScaleR installation from the R terminal.

    library("RevoScaleR")
    
  14. Configure the installed R runtime with SQL Server for Linux, where path/to/ is the file path to the R binary, and RFolderVersion is the version-specific folder name for your installation of R runtime, for example, R4.2.

    /opt/mssql/bin/mssql-conf set extensibility rbinpath /path/to/RFolderVersion/lib/R/bin/R
    /opt/mssql/bin/mssql-conf set extensibility datadirectories /path/to/RFolderVersion/
    
  15. Restart the Launchpad service.

    systemctl restart mssql-launchpadd.service
    
  16. Configure SQL Server for Linux to allow external scripts using the sp_configure system stored procedure.

    EXEC sp_configure 'external scripts enabled', 1;
    GO
    RECONFIGURE
    GO
    
  17. Verify the installation by executing a simple T-SQL command to return the version of R:

    EXEC sp_execute_external_script @script=N'print(R.version)',@language=N'R';
    GO
    

Install Python on Ubuntu

  1. Begin installation as root.

    sudo su
    
  2. Update packages on your system (required).

    apt-get update
    
  3. Install the SQL Server extensibility feature.

    apt-get install mssql-server-extensibility
    
  4. Download and install the version of Python that is desired. Choose a version of Python 3.10 or higher, available for download directly from python.org. Follow the instructions for the desired runtime.

  5. Download revoscalepy for Linux.

  6. Install revoscalepy for the root user, specifying the absolute file path to the .whl file.

    pip install /path/to/revoscalepy.whl
    
  7. Verify the revoscalepy installation from the python terminal. Verify the library can be imported.

    import revoscalepy
    
  8. Configure the installed Python runtime with SQL Server, where /path/to is the path to the python installation binary, and pythonFolderVersion is the desired version of Python installed, for example, python3.10. Use the following script with your actual installation path:

    /opt/mssql/bin/mssql-conf set extensibility pythonbinpath /path/to/pythonFolderVersion
    /opt/mssql/bin/mssql-conf set extensibility datadirectories /path/to/:/path/to/lib/pythonFolderVersion/dist-packages/
    
  9. Restart the Launchpad service.

    systemctl restart mssql-launchpadd.service
    
  10. Configure SQL Server for Linux to allow external scripts using the sp_configure system stored procedure.

    EXEC sp_configure 'external scripts enabled', 1;
    GO
    RECONFIGURE
    GO
    
  11. Verify the installation by executing a simple T-SQL command to return the version of python:

    EXEC sp_execute_external_script @script=N'import sys;print(sys.version)',@language=N'Python';
    GO
    

Install on RHEL

Installation for SQL Server 2022 (16.x) Preview Machine Learning Services for Red Hat Enterprise Linux (RHEL) is currently not supported.

Install on SUSE Linux

Installation for SQL Server 2022 (16.x) Preview Machine Learning Services for SUSE Linux Enterprise Server (SLES) v15 is currently not supported.

Install Java

To install the Java language extension, see Install SQL Server Java Language Extension on Linux.

Verify installation

To validate installation:

  • Run a T-SQL script that executes a system stored procedure invoking Python or R using a query tool.

  • Execute the following SQL command to test R execution in SQL Server. Errors? Try a service restart, sudo systemctl restart mssql-server.service.

    EXEC sp_execute_external_script   
    @language =N'R', 
    @script=N' 
    OutputDataSet <- InputDataSet', 
    @input_data_1 =N'SELECT 1 AS hello' 
    WITH RESULT SETS (([hello] int not null)); 
    GO 
    
  • Execute the following SQL command to test Python execution in SQL Server.

    EXEC sp_execute_external_script  
    @language =N'Python', 
    @script=N' 
    OutputDataSet = InputDataSet; 
    ', 
    @input_data_1 =N'SELECT 1 AS hello' 
    WITH RESULT SETS (([hello] int not null)); 
    GO 
    

Offline installation

Follow the Offline installation instructions for steps on installing the packages. Find your download site, and then download specific packages using the package list below.

Tip

Several of the package management tools provide commands that can help you determine package dependencies. For yum, use sudo yum deplist [package]. For Ubuntu, use sudo apt-get install --reinstall --download-only [package name] followed by dpkg -I [package name].deb.

Next steps

Python developers can learn how to use Python with SQL Server by following these tutorials:

  • Python tutorial: Predict ski rental with linear regression in SQL Server Machine Learning Services
  • Python tutorial: Categorizing customers using k-means clustering with SQL Server Machine Learning Services

R developers can get started with some simple examples, and learn the basics of how R works with SQL Server. For your next step, see the following links:

  • Quickstart: Run R in T-SQL
  • Tutorial: In-database analytics for R developers

Feedback

Submit and view feedback for