Linux & Unix $PATH tutorial: Fix the “Command Not Found” error

If you’ve ever gotten an error when trying to run a command that says “Command Not Found”, and you don’t know what to do, then this tutorial is for you.

The $PATH environment variable

Your computer has system wide “environment variables”. The most commonly known is your $PATH variable which tells your system which directories to look in for commands. You can see which folders are in your $PATH by running this:

$ echo $PATH

If there is an executable program and it’s NOT in one of the folders in your $PATH, then your computer will not be able to find it unless you specify a path to the executable.

Let’s take a look at some photos of my terminal to see what I mean. ls, mv, touch, cat, and rmdir are all terminal commands, so they’ve got to be somewhere in the $PATH, otherwise the computer wouldn’t know what to do when someone types in “ls”.

See how the $PATH is just a list of a bunch of folders? All the directory paths are separated by Colons. ls, cat, mv, etc… must all be in one of these folders, because when I use them they work! You can search through these folders, but I already know which one they’re in… Try looking in /bin.

Learn to Code for Free!

Learning to Code isn't hard. All you need is a little guidance and you'll be well on your way to banging out apps like there's no tomorrow. We'll take you by the hand and show you step-by-step how to get started in programming.

envelope

Let’s say you downloaded a program off Github (or you create your own) and try to run it… You may get an error that says “command not found”.

That means you need to do one of…. three things.

1. Move the program (the executable) to a folder in your $PATH

2. Specify the path to the executable when you run the command

3. Add the location of the executable to your $PATH

Change Your $PATH

to TEMPORARILY change your path, run a command like this in your terminal:

$ export PATH=$PATH:/Path/to/executable_file

This takes your PATH, and sets it to the existing path ($PATH), in addition to adding /Path/to/executable_file to the path, so any executables in /Path/to/executable_file will be able to run.

These changes only last until you close out your terminal!

To make your changes permanent you’ll need to add the changes to your path via a shell login or startup script like .bashrc or .bash_profile in your home directory.

Here’s what part of my .bash_profile looks like on my Mac:

#dummy changes
export PATH=$PATH:/Users/johncurry/ilikepie
export PATH=$PATH:/Users/johncurry/testdir

Again, this adds the /Users/johncurry/ilikepie and the /Users/johncurry/testdir to my $PATH

Once you add the changes to your .bash_profile or .bashrc file, close out the shell, and start it up again. Then Confirm it worked by running $ echo $PATH and make sure your new folder(s) are there.

If you want to see me run through an example, be sure to watch the YouTube video embedded in this article.

Conclusion

Your $PATH environment variable lists all the directories where “executables” are stored. In order for your terminal to run a command, it must either be located in one of the directories in the $PATH, or you must specify the location of the executable every time you run it.

So if you’re installing something like composer (a package manager) or a program off Github, you’ll need to put those files somewhere in your path or add the folder to your path in order to run those programs.

You must have an account and be logged in to solve challenges and lessons!

Submit a Comment

Your email address will not be published. Required fields are marked *