How do I install MSSQL Server and/or Tools for Linux on 16.04?

This post was created to try and be a 'catch all' canonical question/answer pair for 'How do I Install MSSQL Server' and 'How Do I Install MSSQL Server Tools' questions.

I am looking to set up MS SQL Server for Linux, and quite possibly its tools (on the server itself or other systems) so I can interact with SQL Server. How can I go about doing this?


This answer covers installing MSSQL Server and Tools for any 16.04 system. It does not cover the Docker solution to getting MSSQL Server on a system. A separate answer will be written for MSSQL Server via Docker installation option for SQL Server.

There are a couple different components here in packages from Microsoft repositories only.

  1. mssql-server - Microsoft SQL Server for Linux Preview
  2. mssql-tools - sqlcmd, etc. commands to interact with MSSQL Server.

There are some evil caveats here:

  1. You must have 16.04 for all components; it does NOT work on older versions.
  2. At the moment, there are only 64-bit versions of the packages available. You will not be able to run mssql-server and possibly not the tools either unless you are on 64-bit systems.

All commands below are from Microsoft's suggested steps. I do not suggest using some of these commands outside of following these steps exactly, due to the nature of security risks that some of the procedures here introduce (such as logging into a superuser shell).


mssql-server installation steps (source)

These steps cover setting up MS SQL Server for Linux Public Preview. This and the Docker method are the two methods for getting MSSQL Server for Linux set up.

Caveats:

  1. You need to have a 64-bit system for MSSQL Server.
  2. You need at least 4GB of RAM on the system for MSSQL Server to install.
  3. You must have Ubuntu 16.04 for this, there are currently no other versions on their repository servers.

To install the mssql-server Package on Ubuntu, follow these steps:

  1. Enter superuser mode.

    sudo su
    
  2. Import the public repository GPG keys:

    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    
  3. Register the Microsoft SQL Server Ubuntu repository:

    sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list)"
    
  4. Exit superuser mode.

    exit
    
  5. Run the following commands to install SQL Server:

    sudo apt-get update
    sudo apt-get install -y mssql-server
    
  6. After the package installation finishes, run the configuration script and follow the prompts.

    sudo /opt/mssql/bin/mssql-conf setup
    
  7. Once the configuration is done, verify that the service is running:

    systemctl status mssql-server
    

See: Quickstart: Install SQL Server and create a database on Ubuntu.


mssql-tools Installation Steps (source)

This section covers setting up of mssql-tools which includes the sqlcmd command. These steps are needed for any systems in which you wish to use the sqlcmd command or other Microsoft-originating utilities on Linux to interact with an MSSQL Server.

(This is not required for other Python libraries for interacting with MSSQL, or other utilities such as DataGrip which can interact with servers, etc., which do not need the mssql-tools pacakge to operate.)

Caveats:

  1. I have not found a version of this for non-64bit systems. You may need a 64bit system to make these tools work.
  2. You can only use tools on 16.04 currently.
  1. Enter Superuser Mode

    sudo su
    
  2. Import the public repository GPG keys:

    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    
  3. Register the Microsoft Ubuntu repository:

    sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/prod.list)"
    
  4. Exit superuser mode:

    exit
    
  5. Update the sources list and run the installation command:

    sudo apt-get update
    sudo apt-get install mssql-tools
    

Using Docker (amd64 only)

It is also possible to run MSSQL Server on Ubuntu using Docker. This can be done by following the steps below carefully:

  1. If you are using Ubuntu 14.04 or any other release older than Xenial (16.04), you will need to install a newer version of Docker than the archives provide.

    If so, follow the instructions on this page.

    If not, simply run:

    sudo apt-get install docker.io
    
  2. Verify that you can connect to the local Docker daemon using:

    docker info
    

    If you receive an error such as Cannot connect to the Docker daemon., you will need to add yourself to the docker group:

    sudo usermod -a -G docker <USERNAME>
    

    ...where <USERNAME> is replaced with your username. You will need to log out and back in for the changes to take effect.

  3. Pull the MSSQL image from Docker Hub:

    docker pull microsoft/mssql-server-linux
    
  4. Create a directory on the host that will store data from the container and keep the value in an environment variable for convenience:

    export DIR=/var/lib/mssql
    sudo mkdir $DIR
    
  5. Start the container:

    docker run \
        -d \
        --name mssql \
        -e 'ACCEPT_EULA=Y' \
        -e 'SA_PASSWORD=<PASSWORD>' \
        -p 1433:1433 \
        -v $DIR:/var/opt/mssql \
        microsoft/mssql-server-linux
    

    Replace <PASSWORD> with a unique value that will be used for authentication later.

  6. Verify that the container started without error:

    docker ps -af name=mssql
    

    If the STATUS column shows "Up ..." under the STATUS column, then everything is running correctly. If, however, an error is displayed:

    CONTAINER ID ... STATUS ...
    ba79fa12fbf1 ... Exited (0) 3 seconds ago ...
    

    ...then you can use docker logs mssql to obtain further information.

To connect to the container from an application, simply specify port 1433.

By default, the container is started when you first run it. You can stop the container with:

docker stop mssql

To remove the container, first stop it, and then run:

docker rm mssql