When you run a command for a program in command line, it looks for the corresponding executable files using the directories listed in your computer or server’s PATH variable. The PATH variable is one of many Environment Variables your system uses.
Given this, when you add a new command line program to your computer, you have to add its directory to your PATH variable.
In the following instructions, I will demonstrate this process by adding the PHP executable that comes with XAMPP to my PATH, but the steps would be the same for whatever program you’re trying to configure.
Current paths
To start, use the following command to see what your PATH variable is currently set to:
echo $PATH
Example output:
/Users/Susan/go/bin:/usr/local/bin/node:/usr/local/bin/npm:/Applications/Visual Studio Code.app/Contents/Resources/app/bin:/usr/local/bin:/usr/sbin:/bin:/sbin:vendor/bin:~/.composer/vendor/bin[...etc...]
Note that each path is separated by a colon. For a more legible view, the following command will output the same results with each path shown on its own line:
tr ':' '\n' <<< $PATH
/Users/Susan/go/bin
/usr/local/bin/node
/usr/local/bin/npm
/Applications/Visual Studio Code.app/Contents/Resources/app/bin
/usr/local/bin
/usr/sbin
/bin
/sbin
vendor/bin
~/.composer/vendor/bin
[...etc...]
Adding paths
There are a few different config files where PATH information can be set on a Mac but the one we’ll use is /etc/paths
.
Open this file with the following command:
> sudo nano /etc/paths
Here we use sudo
to open the file as an admin, since /etc/paths
is a admin-protected file. Because we’re just making a quick edit, we’re opening the file using the simple command line text editor Nano (more info on Nano here...).
Use your arrow keys to move to the end of the file and add your new path, e.g.:
[...any existing paths...]
/Applications/XAMPP/xamppfiles/bin
Save your changes (ctrl
+ x
, then y
, then enter
).
Finally close and restart your command line program. (This step is important! If you don’t do this, your changes won’t take effect.)
To test that it worked you can use the which
command to ensure it’s resolving your new command to the correct path:
> which php
/Applications/XAMPP/xamppfiles/bin/php
You can also test it by invoking your command, for example:
> php --version
PHP 8.1.1 (cli) (built: Dec 21 2021 08:45:29) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
Other locations where you might find PATH configs include:
-
~/.bash_profile
or~/.bashrc
if you’re using Bash -
~/.zprofile
or~/.zshenv
if you’re using Zsh - The directory
/etc/paths.d
This is useful to know if you’re trying to remove a path as you might have to check different locations to see where it is set.