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.
- Open a terminal and type `echo $SHELL`.
- 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.
- 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
- 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.
- For Bash: If you are using Bash (the default on many systems), the configuration file is usually `~/.bashrc` or `~/.bash_profile`.
- For Zsh: If you are using Zsh, the configuration file is usually `~/.zshrc`.
4. Open The Configuration File In A Text Editor.
- 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
- If the configuration file do not exist, you can create it.
5. Modify The Path Variable.
- 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"
- 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"
- Replace `/path/to/add` and `/path/to/python` with the actual directories you want to include in the PATH.
- 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"
- 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"
- Save and exit the text editor.
6. Apply The Changes.
- To apply the changes immediately, you can either restart your terminal or run the following command (depending on your shell).
- For Bash: source ~/.bashrc.
- For Zsh: source ~/.zshrc.
- If you meet the error like: bash: export: `=’: not a valid identifier.
- You should remove the whitespace before and after the = character in the export PATH line like: export PATH=”$PATH:/path/to/add”.
- 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
- 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.
- There is another way to change the PATH variable value permanently.
- 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
- You can run the command sudo vim /etc/paths to edit this file and add your path value in one line.
- After you change it, you need to restart the macOS to make it take effect.
- For Linux OS ( Ubuntu ), you can run the command cat /etc/environment to see the existing PATH value.
- And then you can run the command sudo vim /etc/environment to add your path to the PATH variable value.
- Do not forget restart the Linux OS after you make the above changes to make it take effect.