Microsoft has supported installing SQL Server on Linux distributions for some time now. Although some of the features available on Windows are not available in the Linux version, it is a great alternative to Oracle, MySQL, PostgreSQL, which have been on Linux for some time. Below, we detail the steps to follow to install SQL Server.
Unsupported features on Linux
You can consult the list of unsupported features at the following link Unsupported features and services.
Installation requirements.
- Ubuntu 22.04 (Also available on Ubuntu 20.04, Red Hat, Suse, Docker).
- At least 2GB of RAM.
- 6GB of free disk space.
Install SQL Server
The first step in the installation is to add the Microsoft repository. To do this, run:
# curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o /usr/share/keyrings/microsoft-prod.gpg
# curl -fsSL https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/mssql-server-2022.list | sed 's|\[arch=[^]]*|[arch=amd64 signed-by=/usr/share/keyrings/microsoft-prod.gpg|' > /etc/apt/sources.list.d/mssql-server-2022.list
# apt update
Now that we have added the repository, we can proceed to install SQL Server with the following command:
# apt-get install -y mssql-server
SQL Server is now installed, and all that remains is to run the initial setup.
SQL Server configuration
Once SQL Server is installed, you need to run "mssql-conf setup" to set the access password and choose the SQL Server edition you need. To do this, run the following command:
# /opt/mssql/bin/mssql-conf setup
Next, you will be asked to choose the desired version. For this example, we choose the "Standard" version, so we enter "5" and continue with the process.
# root@sql-server:~# /opt/mssql/bin/mssql-conf setup
usermod: no changes
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.
Details about editions can be found at
https://go.microsoft.com/fwlink/?LinkId=2109348&clcid=0x409
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):
Accept the terms.
The license terms for this product can be found in
/usr/share/doc/mssql-server or downloaded from:
https://go.microsoft.com/fwlink/?LinkId=2104294&clcid=0x409
The privacy statement can be viewed at:
https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x409
Do you accept the license terms? [Yes/No]:
Set the access password to finish the initial setup:
Enter the SQL Server system administrator password:
Confirm the SQL Server system administrator password:
Configuring SQL Server...
The licensing PID was successfully processed. The new edition is [Standard Edition].
ForceFlush is enabled for this instance.
ForceFlush feature is enabled for log durability.
Created symlink /etc/systemd/system/multi-user.target.wants/mssql-server.service → /lib/systemd/system/mssql-server.service.
Setup has completed successfully. SQL Server is now starting.
Use the following command to verify that SQL Server is running:
# root@sql-server:~# systemctl status mssql-server --no-pager
● mssql-server.service - Microsoft SQL Server Database Engine
Loaded: loaded (/lib/systemd/system/mssql-server.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-06-07 14:01:41 CEST; 1min 1s ago
Docs: https://docs.microsoft.com/en-us/sql/linux
Main PID: 52196 (sqlservr)
Tasks: 122
Memory: 563.7M
CGroup: /system.slice/mssql-server.service
├─52196 /opt/mssql/bin/sqlservr
└─52243 /opt/mssql/bin/sqlservr
Installing mssql-tools
To manage SQL Server from the command line, you need to install "mssql-tools". To do this, run:
# curl -fsSL https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sed 's|\[arch=[^]]*|[arch=amd64 signed-by=/usr/share/keyrings/microsoft-prod.gpg|' > /etc/apt/sources.list.d/mssql-server-prod.list
# apt update
# apt-get install mssql-tools unixodbc-dev
Finally, we need to add "mssql-tools" to the system PATH. To do this, run:
# echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
# echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
# source ~/.bashrc
SQL Server 2022 is now installed and configured on Ubuntu 22.04.
Connecting locally to SQL Server and creating a test database
To connect locally to SQL Server, run:
# sqlcmd -S localhost -U SA -P contraseña
To create a database, run:
# 1> CREATE DATABASE Test;
# 1> GO
To list the databases, run:
# 1> SELECT Name from sys.Databases;
# 1> GO
This will return the following:
Name
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
Test
(5 rows affected)
Remote connection to SQL Server
To connect to SQL Server, you will need to open port 1433 TCP to allow remote connections. At the following link, we explain How to create a firewall rule.