Microsoft SQL Server is a relational database management system from Microsoft. SQL Server 2019 runs on Linux and is available in free editions (Developer, Express) and paid editions (Standard, Enterprise). This guide walks you through installing Microsoft SQL Server 2019 on Ubuntu 20.04: adding the Microsoft repository, running mssql-conf setup to set the SA password and edition, installing the mssql-tools (including sqlcmd and bcp), and connecting locally to create a database and run Transact-SQL queries.
In this guide you’ll:
- Add the Microsoft SQL Server 2019 repository and install the mssql-server package on Ubuntu 20.04
- Configure SQL Server with
mssql-conf setup(SA password, edition) and enable the mssql-server systemd service - Install mssql-tools (sqlcmd, bcp) and add them to your PATH
- Connect with sqlcmd and run basic T-SQL (create database, table, insert, select)
Related guides:
- How to install and set up MS SQL Server on CentOS 8
- How to run MS SQL Server 2019 with Docker and Docker Compose
- Install and set up PHP to connect to MS SQL Server (CentOS 8)
Prerequisites
- Updated ubuntu 20.04 server
- Access to the commandline as root user
Ensure that the server is up to date
Before proceeding, it is always a good practice to make sure that the server we are working on has updated packages. Use this command to ensure that the server is updated
| |
Ensure that some necessary packages are installed
| |
Installing MsSQL Server 2019 in Ubuntu
MsSQL Server is not available in the default Ubuntu repos. To set up the repo and install ubuntu, follow these steps:
Import the public repository GPG keys:
| |
Register the Microsoft SQL Server Ubuntu repository for SQL Server 2019:
| |
Run the following commands to update the new repo and install SQL Server:
| |
Configuring MsSQL Server
After the package installation finishes, run mssql-conf setup and follow the prompts to set the SA password and choose your edition.
| |
Make sure to specify a strong password for the SA account (Minimum length 8 characters, including uppercase and lowercase letters, base 10 digits and/or non-alphanumeric symbols).
This is the output on my server
| |
Starting and enabling the service
Once the configuration is done, verify that the service is running:
| |
This is the output on my server
| |
If you plan to connect remotely, you might also need to open the SQL Server TCP port (default 1433) on your firewall.
At this point, SQL Server 2019 is running on your Ubuntu machine and is ready to use!
If you want to enable the service to start on boot use this command
| |
Install the SQL Server command-line tools
To create a database and run queries, you need a tool that can execute Transact-SQL (T-SQL) against SQL Server. The following steps install the official command-line tools: sqlcmd and bcp.
Use the following steps to install the mssql-tools on Ubuntu.
Import the public repository GPG keys.
| |
Register the Microsoft Ubuntu repository.
| |
Update the sources list and run the installation command with the unixODBC developer package.
| |
Add /opt/mssql-tools/bin/ to your PATH environment variable in a bash shell.
To make sqlcmd/bcp accessible from the bash shell for login sessions, modify your PATH in the ~/.bash_profile file with the following command:
| |
To make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions, modify the PATH in the ~/.bashrc file with the following command:
| |
Connecting locally
Upon successful installation, we can connect to the database server and perform some operations
Run sqlcmd with parameters for your SQL Server name (-S), the user name (-U), and the password (-P). In this tutorial, you are connecting locally, so the server name is localhost. The user name is SA and the password is the one you provided for the SA account during setup.
| |
You can omit the password on the command line to be prompted to enter it.
If you later decide to connect remotely, specify the machine name or IP address for the -S parameter, and make sure port 1433 is open on your firewall.
If successful, you should get to a sqlcmd command prompt: 1>.
| |
Executing basic sql queries
In this section, we will explore using sqlcmd to create a new database, add data, and run a simple query.
Note: You must type GO on a new line to execute the commands.
To create a new database, use this:
| |
To get names of all the databases:
| |
Let us create a new table, Users, and insert new rows.
Switch context to the new CitizixDB database:
| |
Create new table named Users:
| |
Insert data into the new table:
| |
Select data from our database table
| |
To end your sqlcmd session, type QUIT:
| |
Frequently Asked Questions (FAQ)
What is Microsoft SQL Server 2019?
Microsoft SQL Server 2019 is the current long-term release of SQL Server that runs on Linux (including Ubuntu), Windows, and in Docker. It includes the database engine, T-SQL, and support for free editions (Developer, Express) and paid editions (Standard, Enterprise). On Ubuntu you install the mssql-server package from Microsoft’s repository.
What port does SQL Server use?
SQL Server listens on TCP port 1433 by default. For remote connections, open port 1433 in your firewall (e.g. sudo ufw allow 1433/tcp). Local connections use localhost or 127.0.0.1 as the server name in sqlcmd (-S localhost).
How do I connect to SQL Server from the command line on Ubuntu?
Install mssql-tools (which includes sqlcmd), add /opt/mssql-tools/bin to your PATH, then run: sqlcmd -S localhost -U SA -P 'YourPassword'. You can omit -P to be prompted for the password. Use the SA account and password you set during mssql-conf setup.
What is the SA account?
SA (system administrator) is the default SQL Server admin login. You set the SA password when you run sudo /opt/mssql/bin/mssql-conf setup. The password must be at least 8 characters and include uppercase, lowercase, digits, and/or non-alphanumeric symbols.
Can I run SQL Server 2019 in Docker instead?
Yes. For a containerized setup without installing on the host, see How to run MS SQL Server 2019 with Docker and Docker Compose.
Conclusion
You now have Microsoft SQL Server 2019 installed on Ubuntu 20.04: the mssql-server service is running and enabled, and you can connect with sqlcmd to create databases and run T-SQL. For the same setup on CentOS/Rocky Linux, see how to install and set up MS SQL Server on CentOS 8; for Docker, see MS SQL Server 2019 with Docker Compose.