Adding SQL Server Tools to the PATH Environment Variable on Linux
By: Daniel Farina | Comments | Related: More > SQL Server on Linux
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.
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.
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.
As you can see on the next image, the previous command returns a list of folders separated by a colon (:).
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.
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.
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
- Check out SQL Server 2016 tips.
- Learn how to install SQL Server vNext on Mac: Run SQL Server vNext (CTP1) as a Docker Container on a Mac.
- Stay tuned for more SQL Server on Linux Tips.
About the author
View all my tips