Jo Liss
Jo Liss

Reputation: 33114

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

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.)

Upvotes: 91

Views: 320929

Answers (13)

starball
starball

Reputation: 52010

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. or more precisely, only a process itself can change its own environment (see for example, setenv)).

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.

Also related: https://stackoverflow.com/a/76880129/11107541.

In many of the contexts in the above linked post (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).

Upvotes: 4

Jo Liss
Jo Liss

Reputation: 33114

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.

Upvotes: 121

holzkohlengrill
holzkohlengrill

Reputation: 1242

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"

Upvotes: 37

Victor S. HUANG
Victor S. HUANG

Reputation: 1

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.

Upvotes: 0

Kaan G&#252;ven
Kaan G&#252;ven

Reputation: 45

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.

Upvotes: 1

ForOhFor
ForOhFor

Reputation: 796

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!

Upvotes: 6

Ivan Š
Ivan Š

Reputation: 1032

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 )

Upvotes: 11

Ciradil
Ciradil

Reputation: 41

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.

Upvotes: 4

Paolo
Paolo

Reputation: 329

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}"
},

Upvotes: 23

russes
russes

Reputation: 1130

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.

Upvotes: 9

Peter Tseng
Peter Tseng

Reputation: 14003

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:-}

Upvotes: -1

mikemaal
mikemaal

Reputation: 345

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.

Upvotes: 0

Kurt MacDonald
Kurt MacDonald

Reputation: 89

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.

Upvotes: -1

Related Questions