How to Install SQL Server on Ubuntu 18.04

This article provides step-by-step instructions for installing SQL Server 2019 for Linux on Ubuntu 18.04.

It should also work with Ubuntu 16.04 (just change the path as instructed).

You should have at least 2GB of memory before you install SQL Server on your Ubuntu machine.

Install the mssql-server Package

The following steps are what I used to install SQL Server 2019 for Linux on Ubuntu 18.04.

1 – Import the public repository GPG keys

The first step is to import the public repository GPG keys.

Open a terminal, and run the following command:

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

2 – Register the Repository

Next step is to register the Microsoft SQL Server Ubuntu repository for SQL Server 2019.

If you’re installing onto Ubuntu 16.04, change /ubuntu/18.04/ to /ubuntu/16.04/.

sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/18.04/mssql-server-2019.list)"

3 – Install SQL Server

Now we can go ahead and install SQL Server.

sudo apt-get update
sudo apt-get install -y mssql-server

Once it’s done all its unpacking, linking, selecting, setting up, and processing, it should eventually stop, and you should see a message like this:

+-------------------------------------------------------------------------+
Please run 'sudo /opt/mssql/bin/mssql-conf setup' to complete the setup of Microsoft SQL Server.
+-------------------------------------------------------------------------+

This is good. It means you’re almost finished.

4 – Run Setup

As instructed in the message, run the following command:

sudo /opt/mssql/bin/mssql-conf setup

Follow the prompts to choose your edition, accept the license terms, choose a language, and set the SA password.

It must be a strong password. Specify a password that has a minimum length of 8 characters, including uppercase and lowercase letters, base 10 digits and/or non-alphanumeric symbols.

It will look something like this, once you’ve gone through the prompts:

Choose an edition of SQL Server:
1) Evaluation (free, no production use rights, 180-day limit)
2) Developer (free, no production use rights)
3) Express (free)
4) Web (PAID)
5) Standard (PAID)
6) Enterprise (PAID) - CPU core utilization restricted to 20 physical/40 hyperthreaded
7) Enterprise Core (PAID) - CPU core utilization up to Operating System Maximum
8) I bought a license through a retail sales channel and have a product key to enter.

Use of PAID editions of this software requires separate licensing through a Microsoft Volume Licensing program.
By choosing a PAID edition, you are verifying that you have the appropriate number of licenses in place to install and run this software.

Enter your edition (1-8):2

Do you accept the license terms? [Yes/No]:Yes

Choose the language for SQL Server. (11 languages are presented)
Enter Option 1-11:1
Enter the SQL Server system administrator password:<MyPassword>

You’ll also see a couple of URL links to license terms and the privacy statement.

Once you’ve entered those details, setup will proceed. Once it has completed, you should see the following message:

Setup has completed successfully. SQL Server is now starting.

If your system doesn’t have enough memory (2GB), this is when you’ll be informed of that.

I know this, because that’s what happened to me when I accidentally installed SQL Server on a virtual machine with only 1 GB allocated to memory!

5 – Verify

Once you’ve completed the setup process, you can run the following command to verify that the service is up and running.

systemctl status mssql-server --no-pager

You should see a bunch of stuff, including the words mssql-server.service – Microsoft SQL Server Database Engine and active (running).

SQL Server for Linux is now running on your Ubuntu system.

Install SQL Server Command-Line Tools

Now that you’ve installed SQL Server, you’ll need some sort of tool to connect to it.

The following steps will get you set up with two SQL Server command-line tools: sqlcmd and bcp.

The next two steps require curl. If curl isn’t installed, you can install it with:

sudo apt install curl

If you already have curl (or you’ve just installed it), you can continue with the following steps.

Import the public repository GPG keys:

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

Register the Microsoft Ubuntu repository:

curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list

Update the sources list and run the installation command with the unixODBC developer package:

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

This should result in the Package Configuration screen being displayed, along with text that reads:

Do you accept the license terms? <Yes> <No>

If you accept the license terms, click Yes.

Add to PATH

Next, you can optionally add /opt/mssql-tools/bin/ to your PATH environment variable.

To make sqlcmd/bcp accessible from the bash shell for login sessions:

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile

To make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions:

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

Connect to SQL Server

Now that SQL Server is installed, and the command-line tools are also installed, we can use the command-line tools to connect to SQL Server.

Enter the following, but replace '<YourPassword>' with your own password:

sqlcmd -S localhost -U SA -P '<YourPassword>'

To connect to a remote instance, change localhost to the machine name or IP address, and be sure to have port 1433 open on the firewall.

Once you’ve connected successfully, you should see a prompt that looks like this:

1>

From here, you can use T-SQL to create databases, run queries, and more.

For now, enter the following code to check your version of SQL Server:

SELECT @@version
GO

A GUI Option

Azure Data Studio is a GUI tool that you can use for administering your SQL Server databases, either on your local machine or remote.

See How to Install Azure Data Studio on Ubuntu 18.04 for installation instructions, including screenshots. You can install it via the GUI or at the command-line.

Also, if you’re new to SQL Server, check out my SQL Server tutorial over at Quackit.