Skip to main content

DEV101 ⚡ Adding applications to the PATH env variable

· 6 min read

It often occurs in a developers day that they need to install a new application and be able to access it from the command line. In most cases a nicely packaged installer (.msi, .exe or .AppImage) is there to handle this process for you along with setting up directories and default config, all with a few clicks through a GUI. Same goes for installing packages via a package manger like apt for Ubuntu or choco for Windows. A single command and the process is done.

Although this is incredibly convenient, not all applications ship with an installer. You may encounter application directories that have been "zipped" or "tar-balled" and once extracted gives you access to the application and dependencies when executed from its current home. When you receive a compressed folder like this it might just be the needed app inside or there may be a folder within called bin which contains your app. The easiest way to make the app accessible from the CLI is to add the folder path containing it to the PATH environment variable. By adding the app directory to the PATH env. variable, we build a list of directories that are searched any time an app is called from the command line. The few steps below will walk you through doing this in both Linux and Windows.

note

As an example, we are going to install the dfu-programmer CLI which allows a user to reflash Atmel AVR microcontrollers via the USB-DFU class. The contents or use of the application is not important here but rather the process. If you'd like to follow along installing a different application, feel free to do so.

To begin, place the uncompressed app folder wherever you'd like on the filesystem. Typically when installing applications in this manner, you would place the application in the /usr/local folder.

sudo mv dfu-programmer/ /usr/local/dfu-programmer

If we ls the app directory, we find the dfu-progammer executable. You may have another layer with a bin folder within that then contains your executable you want to execute. Regardless of where the application actually lives we want to add the full path to its storage location, to our PATH env variable. To do so, edit the .bashrc file located in your home directory.

vi ~/.bashrc

With the .bashrc file open - Add the following line anywhere in the file.

export PATH=/usr/local/dfu-programmer:$PATH
caution

When adding the export statements you should use the path to the folder that contains the executable and should not include the executable name.

The above line pre-pends the path to our dfu-programmer directory to the current $PATH env variable and then assigns that new list back to itself (PATH). This rc file we just edited is loaded every time a terminal is opened and the line we added will let our current shell know about the dfu-programmer location. To re-iterate, this line adds to the enviroment variable and does not overwrite it. This means if we have multiple applications to install in this manner, we can have multiple export statements one after another that will not overwrite one another.

To test if our changes worked, source the rc file - making our current shell aware of the new directory we've added to the PATH environment variable.

source ~/.bashrc

Now try and execute our application from any directory on the file system.

cd ~
dfu-programmer --version

If we were succesfull then we should see a version string of 1.0.0 printed on the console.

dfu-programmer 1.0.0
note

If the above did not work and you see something like Command X not found, did you mean:, verify that the full path to the folder containing your executable was properly added to the .bashrc file. To guarantee the new rc file is being sourced you can close your terminal, open a new one and try the command again.

That's it! You now have access to your application via the command line and are ready to integrate the new app into your automation or general workflows.