How To Change System Environment Variable Path Value On macOS & Linux Permanently

On macOS and Linux, you can change the system environment variable PATH permanently by modifying the appropriate shell configuration file or /etc file. The process may vary slightly depending on the shell you’re using (e.g., Bash, Zsh). Here are the general steps to change the PATH variable permanently on macOS and Linux.

1. Determine The Shell You’re Using.

  1. Open a terminal and type `echo $SHELL`.
  2. This will display the path of the shell you are currently using.
    $ echo $SHELL
    /bin/bash
    
    or
    
    /bin/zsh

2. Display The Current PATH Variable Values.

  1. Open a terminal and run the command echo $PATH, then it will display the current value of the PATH system environment variable.
    $ echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
  2. After you change the PATH environment variable value, you can run the command echo $PATH again to see the changes.

3. Edit The Shell Configuration File.

  1. For Bash: If you are using Bash (the default on many systems), the configuration file is usually `~/.bashrc` or `~/.bash_profile`.
  2. For Zsh: If you are using Zsh, the configuration file is usually `~/.zshrc`.

4. Open The Configuration File In A Text Editor.

  1. For example, you can use the vim or sublime text text editor to edit the above configuration file.
    $ sudo vim ~/.bashrc
    
    or
    
    $ sudo vim ~/.zshrc
  2. If the configuration file do not exist, you can create it.

5. Modify The Path Variable.

  1. Find the line that sets the PATH variable. It might look like this, if the PATH line do not exist, then add the below line to the configuration file.
    export PATH="/usr/bin:/usr/local/bin:/path/to/add"
  2. Add the path you want to add to the PATH variable. For example, if you want to add Python to the PATH, you can add this.

    export PATH="/usr/bin:/usr/local/bin:/path/to/add:/path/to/python"
  3. Replace `/path/to/add` and `/path/to/python` with the actual directories you want to include in the PATH.
  4. To not overwrite existing PATH variable value, you can change the above export line to below, in this way it will add the path at the end of the existing PATH value.
    export PATH="$PATH:/path/to/add"
  5. If you want to overwrite existing PATH value, you can add your path at the beginning of the export PATH line like below, then it will search your command in your path first.
    export PATH="/path/to/add:$PATH"
  6. Save and exit the text editor.

6. Apply The Changes.

  1. To apply the changes immediately, you can either restart your terminal or run the following command (depending on your shell).
  2. For Bash: source ~/.bashrc.
  3. For Zsh: source ~/.zshrc.
  4. If you meet the error like: bash: export: `=’: not a valid identifier.
  5. You should remove the whitespace before and after the = character in the export PATH line like: export PATH=”$PATH:/path/to/add”.
  6. After you apply the changes, you can run the command echo $PATH again to see the changes.
    $ echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/path/to/add
  7. Now, the changes you made to the PATH variable should be permanent, and your system will use the updated PATH every time you open a new terminal session. 

7. Change The System PATH Variable Value By Editing The /etc File.

  1. There is another way to change the PATH variable value permanently.
  2. For macOS, you can run the command cat /etc/paths, then you can see the PATH variable values are listed in the file.
    % sudo cat /etc/paths
    /usr/local/bin
    /usr/bin
    /bin
    /usr/sbin
    /sbin
  3. You can run the command sudo vim /etc/paths to edit this file and add your path value in one line.
  4. After you change it, you need to restart the macOS to make it take effect.
  5. For Linux OS ( Ubuntu ), you can run the command cat /etc/environment to see the existing PATH value.
  6. And then you can run the command sudo vim /etc/environment to add your path to the PATH variable value.
  7. Do not forget restart the Linux OS after you make the above changes to make it take effect.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.