Adding SQL Server Tools to the PATH Environment Variable on Linux

By:   |   Updated: 2017-03-23   |   Comments   |   Related: > SQL Server on Linux


Problem

You want to run SQL commands with the sqlcmd tool on a Linux terminal, but you don't want to have to change to the tools folder for every terminal window you open. In this tip I will show you how to set the $PATH environment variable on Linux for all users.

Solution

Since we have always used SQL Server on Windows platforms, we expect a similar behavior or user experience when moving to Linux. One of those behaviors is the ability to execute any SQL Server tool from the command line without moving to the SQL Server tools folder. Furthermore, on Linux you will be using the sqlcmd command shell more often than on Windows as shown below.

On Windows you can execute SQL Server commands from your profile folder.

The PATH Environment Variable

Any of us that have worked on Windows for a long time, and that includes using DOS, may remember the PATH environment variable. Linux has that variable too, in fact it was born on UNIX systems like Linux. The purpose of the PATH variable is that when a program is not found in the current folder, this variable holds the folders the system will look for that program.

When we install SQL Server on Windows, the installer automatically adds the SQL Server folder to the PATH environment variable for all users. On Linux we have to do it ourselves.

Setting the PATH Environment Variable on Linux

The most important thing to remember is that on Linux the variables are case sensitive. Let's start by displaying the current PATH environment variable value with the following command.

echo $PATH

As you can see on the next image, the previous command returns a list of folders separated by a colon (:).

The SQL Server tools folder is not on the PATH environment variable.

In order to use SQL Server tools from any folder we must add the folders /opt/mssql/bin and /opt/mssql-tools/bin to the PATH environment variable with the export command as shown below.

export PATH=$PATH:/opt/mssql/bin:/opt/mssql-tools/bin

On the next image you can see that after executing the previous command, I am able to execute the sqlcmd tool from my local home folder.

Now you can run SQL Server tools from any folder.

Setting PATH for All Users

At this point we just changed the PATH environment variable for the current user on the current terminal session. If I open another session I will need to run the previous command again. Let's see how to change the PATH environment variable permanently and for every user.

Linux configurations are stored in text files; there is no registry like on Windows. There is a file in the etc folder named environment (/etc/environment). This file contains the value of the PATH environment variable for all users. We just need to edit this file with any text editor and add the folders /opt/mssql/bin and /opt/mssql-tools/bin to the variable definition. This must be done on using an elevated permissions prompt.

As an alternative, instead of using a text editor you can execute the following line using an elevated prompt.

echo PATH=\"$PATH:/opt/mssql/bin:/opt/mssql-tools/bin\" >/etc/environment
Next Steps


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Daniel Farina Daniel Farina was born in Buenos Aires, Argentina. Self-educated, since childhood he showed a passion for learning.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2017-03-23

Comments For This Article

















get free sql tips
agree to terms