Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
What the hell is env variables and how to add to path

Environment Variables

Check out this Super User explanation of environment variables

We sometimes need to add folders containing programs to the PATH-variable to make them executable from the command line.

How to add folders to PATH on Windows 10

Go to Control Panel > System and Security > System and click on Advanced System Settings.

Imgur

In the new windows, go to Advanced then click on Environment Variables at the bottom.

Imgur

In the new window that pops up, select Path in the list and the click Edit.

Imgur

Here you can remove or add different folders to the path. Every folder added here can be accessed from the command line. If there is an executable file in these folders, you can run it from the command line (normally). Click on New to add a new folder of your choice.

Imgur

Sublime example

If I want to add so I can start Sublime from the terminal I can add the folder that Sublime is in on my computer.

C:/Program Files/Sublime Text 3

In this folder I have the program subl.exe. If this folder is in my path I can write subl from the command line and Sublime will open in a new window. You can also open specific files with subl file.txt for example.


How to add folder to path on Mac

You are probably running bash if you are starting your terminal on macOS. If you want to learn more about using bash you can read this guide: bash-guide@GitHub. bash has a configuration-file in your homefolder where you can set the env variables. You can edit this configuration by running this command in the terminal:

nano ~/.bash_profile

You should get a windows that looks something like this. It's and editor in the terminal window. Here you can add your environment variables

Imgur

You save by pressing Ctrl + O and exit by pressing Ctrl + X. The syntax for doing this is:

export PATH=$PATH:/path/to/folder

For example, this folder should already be in your path but it's just an example:

export PATH=$PATH:/usr/local/bin

When you are done you need to reload bash so it recognizes the changes

. ~/.bash_profile

Symlinks for editors

You can also created symbolic links to programs so you can start them from the terminal. You basically create a shortcut from their folders to /user/local/bin which probably already is in your path. Change the name at the end of the command to whatever you want. In these examples you will start sublime with sublime and atom with atom.

Sublime
sudo ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime
Atom
sudo ln -s /Applications/Atom.app/Contents/Resources/app/atom.sh /usr/local/bin/atom

Brackets

sudo ln -s /Applications/Brackets.app/Contents/Resources/brackets.sh /usr/local/bin/Brackets

Fix npm --global errors

If you are getting installation errors when installing npm packages globally on macOS just run this command in the terminal and enter your password:

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

It will change permissions for the folders that npm packages are installed in. Source

@kriskornel

This comment has been minimized.

Copy link

@kriskornel kriskornel commented Feb 1, 2019

Thank You

@davinceleecode

This comment has been minimized.

Copy link

@davinceleecode davinceleecode commented Apr 3, 2020

Many thanks :)

@kingassune

This comment has been minimized.

Copy link

@kingassune kingassune commented Dec 12, 2020

Thank you also. I was having a terrible time.

@theohogberg

This comment has been minimized.

Copy link

@theohogberg theohogberg commented Dec 28, 2020

wow. did this back in 2010, some things hardly change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment