How to Change the PATH Environment Variable on macOS

In this tutorial, we will show you how to set the PATH environment variable on macOS.

macOS Set Path Environment Variable

The path environment variable is used in numerous operating systems such as macOS, Linux, and even Windows.

This variable contains a list of directories that the OS will use when it is looking to execute a command. It saves you from having to remember the full path to every executable.

For example, if you use “ping” in the macOS terminal, it runs an executable stored in the directory “/sbin/“. This directory is referenced within the PATH environment variable.

Adding your own directory to the list will allow you to run any executable stored within it quickly.

Over the following steps, we will show you how to temporarily and permanently set the path environment variable.

For all of the following sections, you will need to open the terminal on your macOS device. The easiest and fastest way to do this is to use the Spotlight feature.

View your Current PATH Variable on macOS

Before modifying the path environment variable on macOS, you might want to see what it is currently set to.

Viewing the contents of this variable is incredibly simple. All we need to do is use “echo“, followed by a reference to the path environment variable.

You can reference variables within the terminal by placing a dollar sign symbol ($) in front of the name.

For example, you would run the following command to list out the current PATH variable on macOS.

echo $PATH

Below you can see the contents of the “PATH” variable on our macOS device. You can see that we have installed Homebrew on our Mac.

/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin

Setting the PATH Environment Variable on macOS

Now that you know how view the current PATH variable let us continue. In this section, we will show you how to add folders to the PATH environment variable on macOS.

The first section will cover setting the macOS path variable temporarily, and the second will cover a more permanent change.

Temporarily Modifying the PATH Variable

In Unix-like operating systems such as macOS, it is possible to change an environment variable like path temporarily.

Doing this is useful when you only need a path to be added for the current terminal session and don’t want the path variable to be permanently changed.

1. To temporarily set the path environment variable on macOS you will want to use the following syntax. Replace “<PATH>” with the path you want to add.

If you want to add multiple paths, ensure a colon (:) separates each one.

PATH=<PATH>:$PATH

You will notice that at the end of the line, we reference “$PATH“. We do this so the current contents of macOS’s PATH variable will be kept intact. If you removed it, you would replace the entire contents of the variable.

2. You can verify that you have set a new PATH environment variable by using the following command.

echo $PATH

3. You should now see that your new path is the first item in the list. For our example, we added “/home/” to our macOS path variable.

/home/:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin

Permanently Setting the Environment Path Variable on macOS

If you want to set the environment path variable on macOS permanently, you will have a couple of different ways. However, we will only be focusing on one of those methods.

Adding paths to the PATH environment variable is useful when running software or scripts from a particular folder constantly.

The way that macOS recommends is to add a file into the “/etc/paths.d/” directory. When you start a new terminal session, macOS will automatically read these files and add the folders to your path environment variable.

Adding to the Environment Path Variable using the Path File

In this section, we will be creating a new file within the “/etc/paths.d” directory. Adding a file to this directory allows us to add as many paths as we want to the paths environment variable on macOS. It also allows us to easily remove any extra paths as we only need to delete the file we created.

Please note to add a file to this directory, you must be an admin on your Mac device.

1. Let us begin by creating a new file within the “/etc/paths.d/” directory. Typically, you should name this file relevant to the paths you store within it. The reason for this is it makes it much easier to find where something is set later on.

For this example, we will be creating a file called “pimylifeup” within our “/etc/paths.d” directory. We are choosing to use the nano text editor as it is one of the easiest text editors for newcomers.

sudo nano /etc/paths.d/pimylifeup

Before you can edit this file, you will be prompted to enter the password for your account.

2. Now, you simply need to add the path that you want added to your macOS path environment variable.

You can add multiple lines in a single file. Simply put each one on a new line. With the example below, we will be adding two folders to our path variable.

/home/pimylifeup
/etc/pimylifeup/example

3. After making these changes, save and quit by pressing CTRL + X, followed by Y, then the ENTER key.

4. Now close out of the current terminal session and open a new one.

The changes won’t affect anything currently running as they need to re-read the path environment variable.

5. With a new terminal session open, let us check the contents of the macOS path environment variable by using the command below.

echo $PATH

6. You can see that by adding our file to the “/etc/paths.d” directory we have set two new folders on the PATH environment variable.

/home/:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/home/pimylifeup:/etc/pimylifeup/example

Conclusion

At this point in the guide, you will hopefully now have a good understanding of how to set the PATH environment variable on a macOS device.

This PATH variable is incredibly useful for improving the access to software on your device. Adding a directory to this list means you will no longer have to type the full path in to launch software contained within it.

Please feel free to comment below if you have any questions about how this works.

Be sure to check out our other macOS tutorials if you found this guide to be helpful.

Leave a Reply

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