Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 46 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save kstevenson722/3fff3a76b3f25d4693a2da53438f3341 to your computer and use it in GitHub Desktop.
Save kstevenson722/3fff3a76b3f25d4693a2da53438f3341 to your computer and use it in GitHub Desktop.

Setup Microsoft SQL Server on Ubuntu 20.04

✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
YouTube Setup Microsoft SQL Server on Ubuntu 20.04 🔗 https://youtu.be/x6pYoWwtVAY
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
SUPPORT MY WORK - Everything Helps Thanks YouTube 🔗 https://YouTube.GetMeTheGeek.com
Buy Me a Coffee ☕ https://www.buymeacoffee.com/getmethegeek
Hire US 🔗 https://getmethegeek.com
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨

Update Ubuntu

sudo apt update
sudo apt upgrade -y
sudo reboot

Import Microsoft public repository GPG key for Ubuntu

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

Register the Microsoft SQL Server Ubuntu repository for SQL Server 2019

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

Install SQL Server

sudo apt update
sudo apt install -y mssql-server

Configure SQL server and set SA password

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

Check SQL service is running

systemctl status mssql-server --no-pager

Check the listening port for MSSQL server

sudo apt install net-tools
sudo netstat -tnlp | grep sqlservr

Open up the firewall port 1433 to connect remotely

Allow ssh and enable firewall

sudo ufw allow 22
sudo ufw allow 1433
sudo ufw allow 1434
sudo ufw enable

Install SQL Server command-line tools

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/20.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list

Install tools and ODBC drivers

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

Add tools folder to PATH environment variable in a bash shell.

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

Connect to your server

sqlcmd -S localhost -U SA -P 'Password'

If successful, you should get a command prompt: 1>.

Attaching existing SQL Server Database

Copy yourdbname.mdf and yourdbname_log.ldf to the /var/opt/mssql/data/ directory. Set the owner and permissions to mssql.

sudo cp yourdbname* /var/opt/mssql/data/
sudo su
Chown mssql:mssql /var/opt/mssql/data/yourdbname*
chmod u=+rw,g=+rw,o=-rw  /var/opt/mssql/data/yourdbname*

Run sql on the master

USE master;

CREATE DATABASE yourdbname 
ON PRIMARY (FILENAME = '/var/opt/mssql/data/yourdbname.mdf'),
   (FILENAME = '/var/opt/mssql/data/yourdbname_log.ldf') 
FOR ATTACH
@JFM1170
Copy link

JFM1170 commented Nov 26, 2020

Hi I set this up on a new install of Ubuntu 20.04.1 LTS started to get this message on every command after a short time, worked great at first.

Msg 208, Level 16, State 1, Server jim-System-Product-Name, Line 1 Invalid object name 'employee'.

Tried to reinstall got errors while trying to set it up again and still getting the error message.
Works good with the Microsoft management studio connects no problem. The linux terminal is where I get the error.

@Lu7h0r
Copy link

Lu7h0r commented Jan 13, 2021

What a great guide I followed your steps and it works perfect for me a thousand thanks, I tested in native ubuntu 20.24

@tuliocaviquioli
Copy link

Thank you very much, it helped a lot!

@datatalking
Copy link

datatalking commented May 10, 2021

This was a fantastic zero bugs, zero issues for an install until I get to the FOR ATTACH.

Ok so after FOR ATTACH I still get the 11> but your instructions just stop.
What do I do now?
In your video you just... POOF.. "OK here we are with sqlserver mangement studio" ummm what?
I've never used this software and there is a few gaping holes in the setup process for a complete beginner.
Where did that come in, how did you install it?

So many tutorials get you to the 99 yard line and poof abandon you. Where can I find a book or manual to learn the rest of this stuff.

@Agustin29
Copy link

Good evening, I have tried to do the steps you mention but still it throws an error of (SQLSTATE [08001]: [Microsoft] [ODBC Driver 17 for SQL Server] SSL Provider: [error: 1425F102: SSL routines: ssl_choose_client_version: unsupported protocol] (SQL: select * from sys.sysobjects where id = object_id (migrations) and xtype in ('U', 'V'))
). I am running a Laravel 8 api on an ubuntu 20.04 server, which must be connected to SQL Server 2012 I have followed the step by step you have written and it did not work for me, even the one from microsoft using this link (https: // docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15).
If anyone has any ideas I would really appreciate it, thank you.

@datatalking
Copy link

@Agustin29 I was able to get this all installed on Ubuntu, Docker, SQL and I think data studio installed. I could post a link to it here from my GitHub

@john-cheng-888
Copy link

Great tutorial ,It works on Ubuntu 20.04.3 LTS
Thank you so much

@Samson-Mayeem
Copy link

I can't find the gui. how do i?

@aserrano
Copy link

Hi, thanks for putting this up! I have been trying to do the install but I keep getting stuck on error
E: Unable to locate package mssql-server
after running command
sudo apt install -y mssql-server
Anyone has any ideas what this is all about? It seems obvious mssql-server is not in the repository. Could there have been and update to the address for it?

Copy link

ghost commented Jan 16, 2022

@aserrano you used gpg key First?

@bharathisomu
Copy link

Hi how to install mssql by using ansible playbooks

@pbesra
Copy link

pbesra commented Jul 20, 2022

Beekeeper studio provides(free and paid) gui for database (supports sql server, mysql, and many others).
https://www.beekeeperstudio.io/get

@cemalkarabulakli
Copy link

Hello, I get this error

The following packages have unmet dependencies:
mssql-server : Depends: libldap-2.4-2 but it is not installable

I tried to install this library but I couldn't install it. Can anyone solve this? I use Ubuntu. Thanks.

@nKeee
Copy link

nKeee commented Nov 23, 2022

Hello, I get this error

The following packages have unmet dependencies:
mssql-server : Depends: libldap-2.4-2 but it is not installable

I tried to install this library but I couldn't install it. Can anyone solve this? I use Ubuntu. Thanks.

You're probably not running the right Ubuntu version.

@abu3baid
Copy link

great guide!
thank you so much.

@ChrisG-HGH
Copy link

Hello, I get this error

The following packages have unmet dependencies:
mssql-server : Depends: libldap-2.4-2 but it is not installable

I tried to install this library but I couldn't install it. Can anyone solve this? I use Ubuntu. Thanks.

You're probably not running the right Ubuntu version.

I am getting the exact same error. I have done a clean install of Ubuntu Server 20.04 LTS, as the docs say this is the highest supported version of Ubuntu for SQL Server. My guess is that when Ubuntu Server 20.04 LTS is installed, it does some degree of self-updates, and that breaks libldap-2.4-2...

I hope someone else who has also seen this error in 20.04 LTS knows how to work around it!

@ChrisG-HGH
Copy link

Just to answer my own question, I solved it by downloading the libldap-2.4-2 from Debian

wget http://ftp.us.debian.org/debian/pool/main/o/openldap/libldap-2.4-2_2.4.47+dfsg-3+deb10u7_amd64.deb
sudo dpkg -i libldap-2.4-2_2.4.47+dfsg-3+deb10u7_amd64.deb

@SamukaDEV
Copy link

Just to answer my own question, I solved it by downloading the libldap-2.4-2 from Debian

wget http://ftp.us.debian.org/debian/pool/main/o/openldap/libldap-2.4-2_2.4.47+dfsg-3+deb10u7_amd64.deb sudo dpkg -i libldap-2.4-2_2.4.47+dfsg-3+deb10u7_amd64.deb

I have faced the same problem, and solved with this comment, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment