How to Install and Configure SQL Server 2022 on Ubuntu 22.04

# How to Install and Configure SQL Server 2022 on Ubuntu 22.04

For decades, Microsoft SQL Server was inextricably linked to the Windows operating system. This posed a significant barrier for organizations seeking to standardize on Linux infrastructure while still requiring a premier, enterprise-grade relational database.

Microsoft fundamentally shifted this paradigm by porting the SQL Server engine natively to Linux. Today, SQL Server 2022 on Linux offers the same core database engine, performance, and security features as its Windows counterpart, allowing database administrators to utilize familiar tools (like SQL Server Management Studio) to manage databases hosted entirely on Linux hardware.

This guide provides a comprehensive workflow for installing, configuring, and connecting to Microsoft SQL Server 2022 on an Ubuntu 22.04 server.

## Prerequisites

Before beginning, ensure your Ubuntu server meets Microsoft’s strict hardware requirements for SQL Server:
1. **Memory:** At least 2 GB of RAM (4 GB or more is highly recommended for production).
2. **File System:** XFS or EXT4.
3. **Privileges:** Root or `sudo` access to the Ubuntu server.
4. **Network:** Port 1433 (the default SQL Server port) must be accessible from your client machine.

## Step 1: Import the Microsoft Repository Keys

SQL Server is not available in the default Ubuntu repositories. You must add Microsoft’s official package repository to your system.

1. **Update your package index and install required dependencies:**
“`bash
sudo apt update
sudo apt install wget software-properties-common apt-transport-https -y
“`

2. **Import the Microsoft public GPG key:**
This key ensures the integrity and authenticity of the packages you are about to download.
“`bash
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc
“`

3. **Add the SQL Server 2022 repository for Ubuntu 22.04:**
“`bash
sudo add-apt-repository “$(wget -qO- https://packages.microsoft.com/config/ubuntu/22.04/mssql-server-2022.list)”
“`

## Step 2: Install the SQL Server Engine

With the repository added, you can now install the database engine package.

1. **Update the repository index:**
“`bash
sudo apt update
“`

2. **Install the `mssql-server` package:**
“`bash
sudo apt install mssql-server -y
“`

The installation process will download and unpack the binaries, but the database engine will not be active until you run the initial configuration script.

## Step 3: Configure SQL Server

Microsoft provides a dedicated setup script to configure the database edition and the initial System Administrator (SA) password.

1. **Run the setup script:**
“`bash
sudo /opt/mssql/bin/mssql-conf setup
“`

2. **Select the Edition:**
The script will prompt you to choose a SQL Server edition.
– Choose `2` for **Developer Edition** (free, full-featured, for non-production use).
– Choose `3` for **Express Edition** (free, limited resources, for lightweight production).
– Alternatively, enter a product key if you possess a license for Standard or Enterprise.

3. **Accept the License Terms:**
Type `Yes` to accept the EULA.

4. **Set the SA Password:**
You will be prompted to enter a password for the SQL Server system administrator (`SA`) account. **This is critical.** The password must meet strict complexity requirements: at least 8 characters, containing uppercase, lowercase, numbers, and non-alphanumeric symbols.
*(Note: The terminal will not display asterisks as you type. This is normal).*

5. **Verify the Service is Running:**
Once the script completes, verify that the SQL Server daemon is active and enabled.
“`bash
systemctl status mssql-server –no-pager
“`
Look for `Active: active (running)`.

## Step 4: Install the SQL Server Command-Line Tools

To interact with the database directly from the Ubuntu terminal, you need the SQL Server command-line tools (`sqlcmd` and `bcp`). These are packaged separately from the engine.

1. **Add the Microsoft tools repository:**
“`bash
sudo add-apt-repository “$(wget -qO- https://packages.microsoft.com/config/ubuntu/22.04/prod.list)”
sudo apt update
“`

2. **Install the `mssql-tools` package:**
“`bash
sudo apt install mssql-tools18 unixodbc-dev -y
“`
*You will be prompted twice to accept the license terms. Use the arrow keys to select `` and press Enter.*

3. **Add the tools to your system PATH:**
To run `sqlcmd` without typing its full directory path, add it to your bash profile.
“`bash
echo ‘export PATH=”$PATH:/opt/mssql-tools18/bin”‘ >> ~/.bashrc
source ~/.bashrc
“`

## Step 5: Connect and Verify the Database

You will now use `sqlcmd` to connect locally to the database engine, authenticating with the SA credentials you created in Step 3.

*(Note: `mssql-tools18` enforces mandatory connection encryption. If you haven’t configured SSL certificates yet, you must use the `-No` flag to trust the server certificate during this initial connection).*

1. **Connect to SQL Server:**
“`bash
sqlcmd -S localhost -U SA -No
“`
You will be prompted for your SA password. Upon successful authentication, the prompt will change to `1>`.

2. **Execute a test query:**
Let’s query the system to verify the version installed.
“`sql
1> SELECT @@VERSION;
2> GO
“`
The output will confirm you are running Microsoft SQL Server 2022 on Ubuntu 22.04.

3. **Exit the sqlcmd prompt:**
“`sql
1> QUIT
“`

## Step 6: Configure the Firewall

To manage the database remotely (e.g., using SQL Server Management Studio or Azure Data Studio from a Windows machine), you must open the SQL Server port in the Ubuntu firewall.

1. **Allow TCP traffic on port 1433:**
“`bash
sudo ufw allow 1433/tcp
“`
*(Ensure you also restrict access to this port via cloud provider security groups or network firewalls to prevent unauthorized access from the public internet).*

## Conclusion

Your Ubuntu server is now hosting a fully functional, enterprise-grade instance of Microsoft SQL Server 2022. From a client machine on your network, you can now point Azure Data Studio or SSMS to the Ubuntu server’s IP address, authenticate using the SA account, and begin creating databases, executing T-SQL, and deploying applications just as you would on a Windows server.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.