Help! How do I add a PATH?!…

(on MacOS)

If you’re here you probably have this issue:
You just downloaded this new program. But when you try to use it:
“Error: program “new_shiny_program” not found in PATH”…

One thing I wish I knew when I first started to code is:

“how the heck do I add this “PATH”?!

Let me start by briefly talk about the command line. If you know what it is, and how it’s basics work, feel free to skip ahead.

The command line, also called CLI (command line interface), terminal or shell is basicly a program where you can interact with your computer. You can:

  • change into different directories/folders
  • create files
  • download and install stuff
  • write scripts to automate tasks
  • a lot more…

Why do we even need the command line?

  • Sometime it’s just faster to simply type a little command into the command line, instead of opening an entire program.
  • Other programs do’t even have a GUI (graphical user interface) like a Microsoft’s word.

The latter are called “command line tools”. And if you’re starting out: it is most likely, that one of your first programs can only be run through the command line.

A little Example: Let’s say you have Python installed on your machine and you wrote a little Python-program that prints “hello, world!”. You will need to open the command line. (It shows a promt, probably showing two right angel brackets (>>). And behing them you can type:
python my_program.py
And “magically” the text “hello, world!” appears below (also in the command line).

How does that work?

I won’t tell you what happens on the OS (operating system) level. But is really useful to know, that the command line (which is a program in itself) is running other programs for your (the the python interpreter in the example above).
Also when you type “ls” (to see what files and folders are in your current folder) the command line actually executes a program called ls.

How does the command line know what to do?
Let’s get a bit more specific. One of the available command lines (also most likely the one that opens when you open terminal on your mac) is called bash. And bash will look into a couple of directories on your computer to see, which programs are available for it to run.
But those folders are hidden folders. They can be seen though by adding the -a flag to the ls command (ls -a). Or if you’re in the Finder you can use this nifty shortcut cmd + shift + .  (<- the third key is a dot)  to make the hidden folders visible/invisible.

Where are those folders?

On your mac you will find them in your root directory:
In my case: Harddrive/some_hidden_folder
You can find folders like:

  • bin
  • usr/bin
  • usr/local/bin

Inside those folders are the programs your command line can use. Why? because those folders are specified in the PATH variable of your bash command line program.

How to actually add a PATH

Your are here because some program could not be run. And it complained that the “path is missing” and you should add it. Let’s do it than.
We need to know where the program is located, so we can that specific path that lead to the binary (program), to the command line environment variable PATH.
Just imagine we would have installed some_program into Users/your_user_name/programs/cool_program
So the program is not in one of the usr, usr/bin, something hidden directories. Therefore our command line cannot find it.
Here comes the rescue:
Type into your your command line PATH=”Users/your_user_name/programs/cool_program”:$PATH

Little aside, incase you don’t understand the slightly cryptic command.

The command line also has a syntax. You can even write entire scripts, with loops, conditions, variables and everything. (If you like to learn a bit more about that, I highly recommend the codecademy learn the command line tutorial.)
Here, the variable is calles PATH. We set it to a new value with the equal character (=). No spaces are allowed. The value (our path) is surrounded by quotes (” “). And at the end we add the old PATH to it, using the colon (:). The old path’s value is accessed by putting the dollar-sign in front of it’s variable name ($PATH).

This adds the path (including the folder cool_program where some_program resides) to the PATH variable. To be more specific: the path get’s added to the front of the already existing PATH. We could add it to the end with:
PATH=$PATH:”Users/your_user_name/programs/cool_program”
Your program should be found by the command line now and you can use it.

BUT! This only works temporarily. More specific, for as long as you keep the current command line window open. Once you close it, the recently added path is gone again. Sorry.

How to add a PATH permanently?

There are user specific folders like:

  • Harddrive/your_user_name/.bash_profile
  • Harddrive/your_user_name/.bashrc
  • Harddrive/your_user_name/.profile

Those hidden files are essentially text files. You can double click on them to open them (if made visible with cmd + shift + . ). You will find some very familiar looking lines of text, or more concrete instructions.
Your bash-command-line looks into the above mentioned folders, because of these files.
The files might contain something like this.

bash environment setting

The lines prefixed with the hashtag (#) are just comments. Below you can see export and a path declaration (like the one we discussed earlier). Export is a built-in-command of your bash-command-line. It simply puts the declared variable (here it is a PATH) into your command line environment, ready for you or the bash-command-line to use it.

Et voilà! You have added a PATH to your command line environment. I hope this was helpful for you and that you can understand the command line a little better now. If you would like to know more about the command line you might wanna have a look at the learn UNIX tutorial by tutorialspoint.

If you liked this post or if you have some comments, please feel free to send me an email. Or share and comment it on twitter.

Thanks for stopping by
Späters

niilz