Running programs through the terminal is a fast and efficient way to perform operations on your Mac. It provides comprehensive functionality and granular control over the program’s functionality and output, which would otherwise be missing from its GUI equivalent. However, starting programs via CLI sometimes also causes problems. One is the command not found error that the shell throws at you when you try to run certain programs/commands. This is where PATH comes in.
While you can temporarily fix this by placing your command before the absolute path of the program, this isn’t a very practical approach if you want to use that program multiple times. An alternative (read efficient) workaround around the issue is to set the PATH variable for this program. Follow along as we describe the PATH variable and list the steps for setting the PATH variable in macOS.
What is the PATH Environment Variable?
PATH or PATH variable is a type of environment variable on all Unix and Unix like operating systems. Environment variables form name-value pairs for different programs or processes on an operating system, such as the path, locations of system programs or processes, and other critical information required by other system programs.
Speaking of PATH, the variable lists all the directories (for different programs added to the PATH) that the shell has to look for in order to run the requested programs via a terminal command.
Why Do You Need to Set the PATH Environment Variable?
On macOS, when you run a command in the terminal, it searches for the path of the requested program in that command inside the PATH environment variable. If a path address is found, it executes the command successfully and returns the output. If not, you get the command not found error.
As we mentioned at first, specifying the absolute or full path of the directory where the program is installed/stored in your command is one way to fix this error. Unfortunately, since this would take a lot of your time and effort over and over again, this is not a very efficient approach and cannot be used if you want to run commands repeatedly.
On the other hand, if you set the path for that program in the PATH variable, you can easily use it in any directory on your system without specifying its absolute path.
Basically, what this means is that instead of running your command like this:
/path/to/program/script.sh
You can simply use:
script.sh
Inside any directory on the file system.
How to Set the PATH Variable on macOS
Setting the PATH variable on macOS requires using the CLI—unlike Windows, which lets you do so using both GUI and CLI. Plus, depending on your requirements, there are two ways to set the PATH on your Mac: temporary and permanent.
When you set PATH temporarily, your path changes apply only to the current session. When you log out of the session, the PATH reverts to its previous state. Conversely, if you set the PATH permanently, your changes will be permanently preserved and will be applied to all your sessions, even after you start a new terminal session or restart your Mac.
With that out of the way, follow the steps in the sections below to set a PATH on your Mac.
Identifying the Current PATH Entries
Before you add a program’s path to the PATH variable on your Mac, you should first identify the current entries in your system’s PATH to verify that there isn’t already an entry for the same.
To view the current PATH settings, open the Terminal app and run:
echo $PATH
If you’re setting a PATH for a new program/script, you can skip this step.
Setting the PATH Variable Temporarily
Once you’ve identified the current PATH entries, you can now set the PATH for any program. If you want to use/execute a program via terminal only in your current session, you can set its path temporarily using the following command:
export PATH=$PATH:absolute/path/to/program/
For example, if you want to set PATH for Python 3.6, you’d run:
export PATH=$PATH:/Library/Frameworks/Python.framework/Versions/3.6/bin
Doing so will set a temporary variable for the program, which you can use in your commands in the current terminal session or the active terminal window.
Setting the PATH Variable Permanently
In case you want to use a program regularly, you must set its path permanently. To do this, you need to access the shell’s configuration or profile file and add the program’s path to it.
Depending on the macOS version you’re running on your Mac, this can be done via either the bash shell or zsh (z shell).
- For older macOS versions (before Catalina): bash (.bashrc or .bash_profile)
- For macOS Catalina and later: zsh (.zshrc or .zsh_profile)
Now that you know the file where you need to add the path for your program/script, open the terminal and enter a command based on your shell:
For bash
nano ~/.bash_profile
or
nano ~/.bashrc
For zsh:
nano ~/.zsh_profile
or
nano ~/.zshrc
If the config file or profile file is missing from your system, this command will create a new one; in case it exists, it’ll open the same, and you can then edit it.
Now, all you have to do is find the full path for the program or script on the file system. For this, open the Finder and navigate to the directory where the program or script is stored or installed. Here, right-click on the program/script, press the Option key, and select Copy xyz as Pathname, where xyz is a program name.
Go back to the terminal and open the appropriate config file or profile for your shell in nano or any other text editor. Inside the file, enter the following line:
export PATH=$PATH:/path/to/directory
…where you need to replace path/to/directory with the exact path you copied in the previous step.
Or, if the file already contains path entries, append the line with a colon (:) followed by:
/path/to/directory
Hit Control + O to write your changes to the file. When prompted to confirm the file name, hit Return to proceed with the default. Press Control + X to exit the editor.
Now, verify if the path has been added by opening a terminal window and running:
echo $PATH
Finally, close the terminal window and reopen it to start a new session. Alternatively, you can run the following command to apply the changes immediately:
source ~/.bashrc
or
source ~/.bash_profile
or
source ~/.zshrc
or
source ~/.zsh_profile
Once that’s done, you should be able to run your program or script from any directory in the file system.
Add to PATH Mac: Accessing Programs From Anywhere via Terminal
With the PATH environment variable set to use the path of the program you want to use, you can now execute/access it from anywhere in the file system via the terminal. If you use Python or shell scripts to automate your workflow, setting the PATH for these scripts can simplify your life as you can now access them inside any directory without needing to specify their absolute paths.
FAQs About Setting PATH on macOS
1. How do I set an environment variable in Catalina Mac?
If you’re running macOS Catalina (or above), setting the environment variable is as simple as modifying the shell config or profile file to incorporate the path of the program/script you want to access anywhere. Since macOS uses zsh as the default shell on Catalina (and above) versions, you just need to edit either the .zshrc or .zsh_profile file and add the path of your program or script. Follow the steps earlier in the post to know the steps and the different ways to do this.
2. How do I permanently set PATH on Mac?
Setting the PATH on Mac permanently means your PATH environment variable changes aren’t limited to your current shell session, unlike the temporary variable settings. So your system’s shell can continue to access it even when you start a new session or restart your Mac. To permanently set PATH on Mac, all you have to do is open either bash files (.zshrc or .zsh_profile) or zsh files (.zshrc or .zsh_profile) and add your program or script’s PATH to it. Steps for doing, which are listed in the guide above.
3. How do I find the PATH variable on a Mac?
To find the PATH variable on Mac, open a terminal window and run echo $PATH. After which, the shell will return a list of all the directories currently listed under the PATH environment variable on your Mac.
Refrence
techpp, How to Set the PATH Variable on macOS, https://techpp.com/2021/09/08/set-path-variable-in-macos-guide