Published on

July 16, 2021

Installing SQL Server Command Line Tools on Linux

In this article, we will explore how to install SQL Server command line tools like SQLCMD and BCP on a Linux system. These tools are essential for accessing and managing SQL Server databases locally.

Checking for SQLCMD Installation

Before we proceed with the installation, let’s check if SQLCMD is already installed on your system. Open your terminal and type the following command:

sqlcmd

If SQLCMD is installed, you will see details about the utility. Otherwise, you may encounter a connect error.

Installing SQL Server Command Line Tools

If SQLCMD is not installed, we need to download and install the necessary repository. Follow these steps:

  1. Verify the SQL Server repository by running the command:
sudo ls /etc/yum.repos.d

If you see the repository for SQL Server 2019, proceed to the next step. Otherwise, download the repository configuration file by executing the following command:

sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/8/prod.repo
  1. If you are installing SQL Server 2019 and the command line tools on a new machine, you can skip this step. However, if you are installing on an older machine with a previous version of mssql-tools installed, you need to remove the older unixODBC packages. Run the following command to remove the component:
sudo yum remove unixODBC-utf16 unixODBC-utf16-devel
  1. Now, let’s install the SQL Server command line tool (mssql-tools) along with the unixODBC-devel package. Execute the following commands:
sudo yum install -y mssql-tools unixODBC-devel

During the installation, you will be prompted to accept the license terms. Enter “YES” to proceed.

Configuring the PATH Environment Variable

If you encounter issues accessing or launching the SQLCMD tool, you may need to add the path environment variable. Run the following commands to modify the PATH for both login sessions and interactive/non-login sessions:

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

After adding the path environment variable, you should be able to access SQLCMD easily.

Validating the Installation

To validate the installation, you can connect to the SQL Server 2019 instance using the SQLCMD tool. Use the following command to establish a database connection:

sqlcmd -S <server_ip_or_name> -U <username>

Replace <server_ip_or_name> with the IP address or name of your SQL Server instance, and <username> with the appropriate username.

Once connected, you can execute T-SQL statements to interact with the database.

Conclusion

By following the steps outlined in this article, you should now have SQL Server command line tools (SQLCMD and BCP) installed on your Linux system. These tools provide a convenient way to access and manage SQL Server databases locally. If you encounter any issues, make sure to check the installation steps and verify the PATH environment variable.

Thank you for reading!

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.