If we have written an application, it would be ideal if we could run it from anywhere in our terminal, regardless of our current location.
In this tutorial, we explore ways in which we can make a program executable from anywhere in our system.
There are several ways to do this, let’s take a look!
2. What About /bin or /sbin?
Most programs in Linux are located in either /bin or /sbin. These are therefore executable from anywhere in our system.
We could move our program to one of these folders. While this solves our problem, it’s a bad idea to do so because /bin and /sbin are reserved locations.
Assuming our program is neither essential to the OS nor an administrator command, it has no place in these locations. We also don’t want to mess around in these directories. We could accidentally break things.
3. The /usr Directory
The /usr directory contains user applications. These applications are programs that can be run by any user and are non-essential to the operating system.
Non-essential regular applications are stored in /usr/bin, non-essential system administration applications are stored in /usr/sbin.
We could place (a symbolic link to) our program in one of these directories. However, package managers like yum or apt-get also use these directories for installing applications. We have to be careful not to break installed packages.
There is a better option within /usr, which is /usr/local. This is the directory where the system stores locally compiled applications to avoid them from interfering with the package manager.
4. The /opt Directory
The best option is to put our program in a location where we are sure that it won’t interfere with other programs. Our home directory would meet this requirement, but that would make it impossible for other users to execute our program.
Fortunately, Linux provides us with the /opt directory, meant for optional software packages. We can put our program in a subdirectory of /opt. However, by default, programs in /opt are not executable from anywhere, as the directory is not on our PATH.
5. The PATH Variable
PATH is an environment variable that tells our shell where it has to search for executable files. By appending to PATH, we can add additional directories for our shell to search for executables.
When we put our program in /opt/program/ for example, we can add this line to our profile in ~/.bash_profile to make it executable from anywhere:
Note that we use the tilde character (~) to refer to our home directory.
To make the program executable from anywhere for all users, we can add it to the global profile settings in /etc/profile.
We should note that /etc/profile and ~/.bash_profile are specific to the Bash shell. Other shells will likely use different locations. For example, zsh uses /etc/zshrc and ~/.zshrc.
To make a program executable from anywhere in our system, we should put it in a directory that is on our PATH. Furthermore, we don’t want to interfere with system commands or any programs that are under package management.
By putting our programs in /opt and adding their location to PATH, we can easily share them with other users, and we don’t need to worry about breaking our system.