74

I'm defining a custom $PATH environment variable in my ~/.bash_profile (on a Mac), like so:

PATH="$HOME/.cargo/bin:$PATH:$HOME/bin"

However, VS Code of course does not run my .bash_profile, so it does not have my custom paths. In fact, if I Toggle Developer Tools and check process.env.PATH, it doesn't even seem to have /usr/local/bin.

How do I globally set the PATH environment variable in VS Code?

(I want to set it globally, not per project or per task, since I'm maintaining a lot of small packages.)

starball
  • 20,030
  • 7
  • 43
  • 238
Jo Liss
  • 30,333
  • 19
  • 121
  • 170
  • 4
    The answers below are all out of date. VSCode now inspects the value of `$SHELL` and actually does run the corresponding startup file (`.bash_profile`, `.zshrc`, `config.fish`, etc) before launching, so it does see the `PATH` you've set in one of those files. – BallpointBen Feb 22 '21 at 19:13
  • 1
    Hm, my `$SHELL` is `/usr/local/bin/bash` and I'm running the lastest VSCODE yet it's not running `.bash_profile` so far as I can tell. – Christopher King Aug 24 '21 at 06:13

13 Answers13

94

If you only need the $PATH to be set in the integrated terminal, you can use VS Code's terminal.integrated.env.<platform> variable (added in version 1.15). Press Cmd+Shift+P (or Ctrl+Shift+P) and search for "Preferences: Open Settings (JSON)". Then add the following entry to the settings file:

"terminal.integrated.env.osx": {
  "PATH": "...:/usr/bin:/bin:..."
}

(Replace .osx with .linux or .windows as needed.)

To see your system's $PATH, type echo "$PATH" in Terminal.app, and copy and paste it into the settings snippet above.


As for having the $PATH available everwhere in VS Code, so that it will be used by extensions that call binaries, the only workaround I've found so far is this:

  1. Configure your shell to have the $PATH you want. For example, I'm using Bash, and my ~/.bash_profile has the following line:

     PATH="$PATH:$HOME/bin"
    
  2. In VS Code, press ⇧⌘P and type install 'code' command if you haven't done so before.

  3. Quit VS Code.

  4. Launch VS Code not by clicking its icon in the dock, but by opening Terminal.app and typing code. Your newly set path will be active in VS Code until you quit it.

  5. If VS Code restarts, for example due to an upgrade, the $PATH will reset to the system default. In that case, quit VS Code and re-launch it by typing code.

Update: VS Code on Mac and Linux now apparently tries to automatically resolve the shell environment when it is started by clicking the icon (rather than via code). It does this by temporarily starting a shell and reading the environment variables. I haven't tested this though.

Jo Liss
  • 30,333
  • 19
  • 121
  • 170
  • So here is my issue on Debian if I check my PATH when ssh into the server I can see the VSCODE has magically added itself to PATH but can not figure out where it has been added as grepping the system does not find it. Also how can I see what VSC thinks my PATH is? – Tegra Detra May 28 '20 at 13:13
  • 1
    On Windows integrated shell or external powershell to see your path, type $ENV:Path – vhamon Feb 14 '21 at 08:02
  • How is the new conf taken into account ? Do we have to reload manually ? – vhamon Feb 14 '21 at 08:06
  • My windows paths have spaces. I don't know how then to enter the "paths". – David Pesetsky Apr 14 '21 at 12:25
  • we can use ${env:PATH} to insert default PATH. – ipcjs May 27 '22 at 10:40
  • My terminal wouldn't work. I don't know why, but for me this variable was set to "". I set it to the value returned by echo $PATH, and my terminal was working again. THANKS! – Patrick Da Silva Sep 06 '22 at 16:40
  • Note that you can also set this in `.vscode/settings.json` at the per-project level, if that's what you want. I found that editing the `"env"` key in `launch.json` worked for debugging my Python scripts (F5), but not simply running them (F9). This seems like a much better method. – tsbertalan Aug 22 '23 at 14:58
28

In:

> Preferences: Open Settings (JSON)

add to the JSON file:

"terminal.integrated.env.windows": {
    "PATH": "${env:PATH}"
},

-> terminal.integrated.env should end with .osx, .linux or .windows depending on your OS.


In order to check if it works execute in your VS Code Terminal:

# For PowerShell
echo $env:PATH
# For bash
echo "$PATH"
holzkohlengrill
  • 1,044
  • 19
  • 29
  • 2
    You will not believe how long this has been a pain for me. Thanks, @holzkohlengrill. – Eric McLachlan Oct 18 '22 at 15:21
  • 1
    This should be top answer. Current top answer helped me too, but I had difficulties with the Windows path syntax (inverted "\"). Perfect one-liner here, thanks ! – ToddEmon Nov 12 '22 at 13:25
  • I needed a double backslash: `"PYTHONPATH": "${workspaceFolder};${workspaceFolder}\\src;${env:PYTHONPATH}"` – tsbertalan Aug 22 '23 at 14:59
20

I am using vscode on macos for C/C++ development in conjunction with CMake.

The vscode extension CMake Tools allows to manipulate environment variables via the configuration properties cmake.configureEnvironment, cmake.buildEnvironment and cmake.environment (acting respectively on the CMake configuration phase, the build phase and both - see docs).

Then you can extend your system PATH with custom paths by adding the following snippet to your user or project settings.json:

"cmake.environment": {
    "PATH": "~/.myTool/bin:${env:PATH}"
},
Paolo
  • 301
  • 2
  • 3
  • What if I want a list of environment variables? – Elvis Dukaj Mar 13 '20 at 14:01
  • 1
    @elvis.dukay if you want to define other environment variables, you can just add them there as a comma separated list: "PATH": "...", "ANOTHER_VAR": "value" – Paolo Mar 19 '20 at 13:41
  • It's worth noting that the variable is case sensitive. For example: env.PATH and Env.Path are not the same. – matt.baker Dec 23 '20 at 13:31
10

Since this is the top Google search result for variants of "VS Code path", I will add my answer here.

I'm running Linux and my problem was that VS Code couldn't find some executable needed to build my project. I was running VS Code from the quick launcher (ALT+F2), and not from a Terminal. I tried modifying the PATH variable in many different places, but I couldn't seem to get it right.

In the end, placing the right PATH inside of ~/.zshenv is what worked. It's because .zshenv is the only file that gets sourced for non-interactive shell command execution like from inside of VS Code (more detailed explanation here https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout )

Ivan Š
  • 1,024
  • 2
  • 10
  • 15
  • 1
    This resolved my problem! latex workshop couldn't find latexmk or any other build tools despite them being on my path! This solved the issue for me. Thank you! – Ethan Jan 11 '23 at 21:16
9

Visual Studio Code is the problem.

No matter how you set your PATH variable in the shell, there are cases where Visual Studio Code will not inherit your PATH setting. If you're using an application launcher like LaunchBar to start Visual Studio Code, your PATH variable will not be inherited.

Here is a system-wide fix:

In the /etc/paths.d directory, create a file with your Unix username. In that file, place the additional paths that Visual Studio Code needs to work. In my case, this is the contents of my /etc/paths.d file:

/usr/ucb /opt/local/bin /opt/local/sbin ~/go/bin

Note: Your /etc/paths.d file will be processed system-wide. Since most systems are single-user, this shouldn't be a problem for most developers.

russes
  • 1,110
  • 11
  • 17
  • 3
    In macOS there is a file `/etc/paths`, which has the paths, and then a folder `/etc/paths.d` which has paths created by other apps (e.g., I have one from installing TeX) – BallpointBen Jan 24 '19 at 18:48
  • creating files `/etc/paths.d/Code` and `/etc/paths.d/$USER` containing `/usr/local/bin` had no effect whatsoever for me. – Klas Mellbourn Nov 26 '19 at 17:24
  • Correct me if I'm mistaken, but I believe this fix only works on MacOS. – ljleb Dec 02 '19 at 01:50
  • 1
    Yes, this modification is for macOS only. – russes Dec 03 '19 at 02:48
  • 1
    This is a great tip for m1 based macs using brew. The files go in a nonstandard place and adding ``/opt/homebrew/bin`` in the way suggested above can fix a few things – Vorsprung Nov 11 '22 at 08:45
  • I prefer the /opt/homebrew/bin. Whenever possible, I leave /usr/local/bin for 3rd party tools like VS Code & Sourcetree to leave additions to the command line tools. – russes Dec 17 '22 at 20:32
6

This was even easier to fix than the above answers suggested.

Open VSCode Settings (Ctrl + ,) and search for terminal.defaultProfile.

I updated my Terminal > Integrated > Default Profile: Windows.

It was set to null by default. As soon as I changed it to PowerShell and restarted the terminal, it picked up my system's path variables!

ForOhFor
  • 786
  • 10
  • 16
  • This worked for me, I had to restart the whole vs code, before it picked up the update. – nck Mar 08 '22 at 09:10
  • PowerShell was already starting in terminal before this change, but only after it all user specific environment variables (not only PATH) became available. – Dr.Dax Mar 16 '22 at 19:07
3

What did the trick in my case (Linux Mint 19.3 Cinnamon, VS code installed via snap) was to put my appended PATH in ~/.profile . Since this file is read at the beginning of a user session, don't forget to logout/login or reboot after editing this file.

Ciradil
  • 31
  • 1
1

As of VS Code v1.63.2, you can proceed with Ctrl + Shift + P and then type Open Settings (JSON), and simply add the following line.

"terminal.integrated.inheritEnv": true

In my case the code was already there, but set to false. After changing it, everything was fine.

  • does this setting still exist? I don't see it in my installation (version 1.78). – starball May 06 '23 at 21:09
  • Just updated to the newest version (1.78), and yes, I can still use my terminal env variables. If you don't have this in your settings, just try adding this line in User Settings (JSON), or Default Settings (JSON). In fact, I don't remember changing my default settings, but I found the following line already present: `// Whether new shells should inherit their environment from VS Code, which may source a login shell to ensure $PATH and other development variables are initialized. This has no effect on Windows. "terminal.integrated.inheritEnv": true,` – Kaan Güven May 07 '23 at 07:27
  • ah my bad. I was looking at a workspace settings.json file, and this setting is not configurable on a per-workspace basis- only in user settings.json. – starball May 07 '23 at 07:29
0

I'm working with ubuntu 18.04. I had a similar problem, my enviroment variables were defined and the terminal knows the $PATH but when I tried to debug with golang, go libraries were not found in $PATH variable.

So, to solve it I uninstall the default version from ubuntu software and install manually using the following instructions:

https://code.visualstudio.com/docs/setup/linux

It works for me.

mikemaal
  • 317
  • 3
  • 6
  • This question already has a high quality answer. What your answer is trying to provide? if you just want to share the link, it can safely go as a comment. – tod Oct 19 '18 at 06:33
0

For me it's resolved by editing the .desktop file.

Originally I have

Exec=/usr/bin/code-oss --unity-launch %F

. Just changed to

Exec=zsh -c "source ~/.zshrc && /usr/bin/code-oss --unity-launch %F"

since I use zsh, instead of bash. But if you have the same problem with bash, simply replace zsh with bash. And shortcuts from your desktop environment should be fixed.

0

VS Code's environment (the environment variables it has) will be its environment as it was given when it was created (see also Changing environment variable of a running process. TL;DR you generally can't change a process' environment variables after it has been created).

Note that with macOS and Linux installations, VS Code will try to source an environment from shell-specific user-level configuration files (Ex. ~/.bashrc and ~/.zshrc) even when not manually launched from such shells by creating such a shell and starting itself from that shell. For relevant documentaiton, see here.

Aside from that, what you can also do is the following:

  • Change the environment variables of shell processes launched in the VS Code integrated terminal.

    See the terminal.integrated.env.<platform> settings, where <platform> is one of "windows", "osx", or "linux". The setting's description reads:

    Object with environment variables that will be added to the VS Code process to be used by the terminal on <platform>. Set to null to delete the environment variable.

    See also the related terminal.integrated.inheritEnv setting. Its description reads:

    Whether new shells should inherit their environment from VS Code, which may source a login shell to ensure $PATH and other development variables are initialized. This has no effect on Windows.

  • Change the environment variables of processes run as VS Code tasks.

    Use the task's options field's env field. Similar to the terminal.integrated.env.<platform> setting, this setting's value is a JSON object mapping environment variable names to values. The setting's description reads:

    The environment of the executed program or shell. If omitted the parent process' environment is used.

  • Change the environment variables of run/debug processes defined in VS Code launch configurations.

    Each launch configuration can decide how it wants to allow you to configure this. According to VS Code's documentation, "Many debuggers support some of the following attributes: [...] env - environment variables (the value null can be used to "undefine" a variable)". Other extensions that contribute launch configuration types may use a different schema, such as the cppdbg from the cpptools extension, which uses a format like [ { "name": "config", "value": "Debug" } ].

  • Change the environment of processes related to specific extensions if those extensions contribute settings to do such configuration. For example,

    • The CMake Tools extension contributes various settings, which are documented here, including cmake.environment, cmake.configureEnvironment, cmake.buildEnvironment, and cmake.testEnvironment.
    • The Python extension has the python.envFile setting, and launch configurations of the python type can also have a envFile property. You can find related docs here.

In many of those contexts (not in settings though), you will be able to use VS Code variables, of which the following are notable:

  • variables of the form ${env:<env-var-name>} (to reference environment variables in the VS Code process' environment)

That can be useful to reuse the VS Code process' PATH and append or prepend things to it. Ex. "PATH": "<something>:${env:PATH}. Prepending to the PATH usually means adding something at a higher priority to be found, since most Operating Systems search from first to last and take the first directory where something can be found (see also: How does Unix search for executable files? and What's the relative order with which Windows search for executable files in PATH?). Note that UNIX uses colons to separate entries of PATH, whereas Windows uses semicolons (see also this feature-request issue ticket).

Interestingly, against the current documentation says, at least one setting does support some variable references: terminal.integrated.env.<platform> supports ${env:<env-var-name>}.

starball
  • 20,030
  • 7
  • 43
  • 238
-1

Getting Code to load your existing ~/.bash_profile would be best. I think the docs here are the relevant reference: https://code.visualstudio.com/docs/editor/integrated-terminal#_linux-os-x

Typically $SHELL is your primary shell on Unix-like systems so you probably won't want to change the shell. You can pass arguments to the shell when it is launched.

For example, to enable running bash as a login shell (which runs .bash_profile), pass in the -l argument (with double quotes):

// Linux "terminal.integrated.shellArgs.linux": ["-l"]

// OS X "terminal.integrated.shellArgs.osx": ["-l"]

Although, it looks like that setting is the default on my current VS Code (OS X) setup. Integrated terminal is running my ~/.bash_profile without any changes to the configuration. Perhaps try adding echo Executing .bash_profile... to test if it's running when a new terminal is opened in Code.

  • 4
    I think this only runs `~/.bash_profile` when you run a shell. I want to change the `$PATH` outside of shells as well, so that extensions will be able to call external binaries. – Jo Liss May 23 '17 at 01:24
-1

Add the following to your ~/.bash_profile:

launchctl setenv PATH $HOME/.cargo/bin:$PATH:$HOME/bin

Or run a Bash script when needed, e.g.:

#!/bin/bash
set -Eeuxo pipefail

proj_path=$( cd $( dirname ${BASH_SOURCE[0]} ) && pwd )
launchctl setenv PATH $proj_path/bin:${PATH:-}
Peter Tseng
  • 13,613
  • 4
  • 67
  • 57