How to Install sqlcmd & bcp on Ubuntu

If you’ve installed SQL Server on your Ubuntu machine, you’ll need some sort of tool to connect to it.

Installing the sqlcmd and bcp utilities is good place to start.

What are sqlcmd and bcp?

  • sqlcmd is is a command-line utility for ad hoc, interactive execution of T-SQL statements and scripts and for automating T-SQL scripting tasks.
  • bcp (bulk copy program) is a utility that can be used to import large numbers of new rows into SQL Server tables or to export data out of tables into data files.

Installation

Below are the steps I took when installing sqlcmd and bcp on to Ubuntu 18.04.

First up, this installation process requires curl. If curl isn’t installed, you can install it like this:

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

If you have a different version of Ubuntu, you may need to change /ubuntu/18.04/ to reflect the applicable version number. For example /ubuntu/16.04/ or /ubuntu/20.04/.

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.

That’s all there is to installing the sqlcmd and bcp utilities on Ubuntu.

Add to PATH

It’s a good idea add /opt/mssql-tools/bin/ to your PATH environment variable. That way you can run these utilities without having to provide the full path each time.

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 the command-line tools are installed, you can use the command-line tools to connect to SQL Server.

Here’s how to connect using sqlcmd:

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

Be sure to replace '<YourPassword>' with your own password.

This uses localhost as the server, and therefore it assumes that you have SQL Server installed on your local computer.

To connect to a remote instance, change localhost to the machine name or IP address, and be sure to have the correct port open on the firewall (port 1433 is the default port for SQL Server).

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

If you’ve connected successfully to SQL Server, you should see the details of the SQL Server instance.