59

Is there some way to automatically install VS Code extensions when opening a project (like package.json but for vscode?)

If not, is there some way to install extensions at the command line?

1
  • hi all i love github very much Commented Nov 1 at 10:56

8 Answers 8

70

The recommended way of doing this is through workspace recommended extensions, this is a .vscode/extensions.json in your project that will prompt the user to install them when they first open the folder, the file looks like this:

{
    "recommendations": [
        "eg2.tslint",
        "dbaeumer.vscode-eslint",
        "msjsdiag.debugger-for-chrome"
    ]
}

You can also try setting up a bash/bat script or some other automation to directly install the extensions using the CLI as suggested by parsley72 in the other answer. You will likely annoy users by doing this though, unless this is in a personal dotfiles project or something similar.

See nybon's answer for configuring recommendations in the UI.

Sign up to request clarification or add additional context in comments.

3 Comments

Not sure if this is related - any time I open a folder with VSCode for the first time, I get prompted to reinstall extensions I've already added. Is this expected? How can I have these included implicitly?
@LanceHarper maybe you didn't add some of them? I wouldn't expect that if all the ones in extensions.json are installed.
Thanks Daniel, your answer was great at the time, but at this point would you mind modifying it to defer to nybon's instead?
30

Thanks to Daniel's suggestion, and I find if you want to keep a list of vscode extensions and install all of them in single click, you can try this approach:

  1. Create the list of extensions you want using the workspace recommended extensions
  • Use the Configure Recommended Extensions command to create the extensions.json file Configure Recommended Extensions You will create a file like this:
{
  // See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
  // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
  // List of extensions which should be recommended for users of this workspace.
  "recommendations": [
    "ms-vscode.cpptools",
    "file-icons.file-icons",
    "shd101wyy.markdown-preview-enhanced",
    "sagebind.mathpad",
    "johnpapa.vscode-peacock"
  ],
  // List of extensions recommended by VS Code that should not be recommended for users of this workspace.
  "unwantedRecommendations": []
}

  • Add your favorite vscode extension id into the list in JSON
  1. Re-launch vscode to open the workspace folder (the parent folder for .vscode folder)
  2. Navigate to "Extensions", filter extensions using "Recommended" filter filter extensions
  3. A list of extensions will be shown in "Workspace Recommendations"
  4. Click the ☁️ button to install all extensions at once enter image description here

3 Comments

You can sink your extensions using a Github account as well.
@thoroc : Can you explain what do you mean by sink your extensions using a Github account as well?
Apologies, It looks like a typo: sync as in synchronise instead of sink as a ship going under water.
21

The issue that @daniel-imms created was resolved in June 2017. You can see this in the latest version:

$ code -h
Visual Studio Code 1.16.0

Usage: code [options] [paths...]

Options:
  --extensions-dir <dir>                                       Set the root path for extensions.
  --list-extensions                                            List the installed extensions.
  --show-versions                                              Show versions of installed extensions, when using --list-extension.
  --install-extension (<extension-id> | <extension-vsix-path>) Installs an extension.
  --uninstall-extension <extension-id>                         Uninstalls an extension.
  --enable-proposed-api <extension-id>                         Enables proposed api features for an extension.
  --disable-extensions                                         Disable all installed extensions.

1 Comment

Note that with vs codium it's codium -h
14

See https://code.visualstudio.com/docs/editor/extension-gallery#_command-line-extension-management

I wrote this Makefile to automatically install extensions

freeze-extensions:
    code --list-extensions > extensions.txt

install-extensions:
    cat extensions.txt | xargs -L 1 code --install-extension

Comments

3

I have developed a task that do the right job for you, gathering all the answers of this thread and my own powershell experience:

You need to create a .vscode/tasks.json file into your workspace wich contains this:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Install All Recommended Extensions",
            "type": "shell",
            "windows": {
                "command": "foreach ($ext in (Get-Content -Raw .vscode/extensions.json | ConvertFrom-Json).recommendations) { Write-Host Installing $ext; code --install-extension $ext; }"
            },
            "linux": {
                "command": "cat .vscode/extensions.json | jq .recommendations[] | xargs -n 1 code . --install-extension"
            },
            "runOptions": {
                "runOn": "folderOpen"
            },
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher" : []
        },
    ]
}

This will parse your .vscode/extensions.json and will install your extensions for you!

It works on linux and windows. Linux users needs to install jq before using it:

 sudo apt-get update && sudo apt-get install jq

Comments

2

You can create a VS Code task using the .runOptions.runOn == "folderOpen" option to install them when opening the workspace.

Create (or update) file in .vscode/tasks.json with the following. This example assumes jq is installed and the code CLI command is available.

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Install All Recommended Extensions",
      "type": "shell",      
      "command": "jq -r '.recommendations[]' ./.vscode/extensions.json | xargs -L 1 code --install-extension",
      "runOptions": {
        "runOn": "folderOpen"
      },
      "presentation": {
        "reveal": "silent"
      }
    }
  ]
}

1 Comment

This is purely devilish! I would recommend you check with your team before adding such tasks!
1

For projects where Node.JS/npm is available, this one-liner(-ish) will install all recommended extensions for the workspace via the command line in a single step:

npx json5 .vscode/extensions.json | \
npx json-cli-tool --path=recommendations --output=newline | \
xargs -L 1 code --install-extension

You could imagine documenting this step in a README's local development setup instructions.

Breakdown of the command:

It uses npx, which will install the json-cli-tool and json5 packages from npm if they're not already installed.

# Compile the extensions.json file from JSON (with comments) to valid JSON
# That's because VS Code's setting files often contain comments
# If yours doesn't, you can skip this step
npx json5 .vscode/extensions.json | \

# Traverse the JSON and extract all extension names
# on a new line from the "recommendations" array
npx json-cli-tool --path=recommendations --output=newline | \

# Which should output:
# "dbaeumer.vscode-eslint"
# "stylelint.vscode-stylelint"

# Pass each line from the command above to VS Code's CLI
# as seen in [@weaming's solution][2]
xargs -L 1 code --install-extension

Comments

1

one-liner to install extensions.json recommendations IF you have jq installed:

cat .vscode/extensions.json | jq .recommendations[] | xargs -n 1 code --install-extension

or with --force (which will install latest version of each extension if they are already installed):

cat .vscode/extensions.json | jq .recommendations[] |xargs -I {} echo "code --install-extension {} --force" | bash

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.