How to open Visual Studio Code with admin privileges to make effect of the installed extensions

63,423

Solution 1

To run with superuser:

$ sudo code --user-data-dir=~/root

By the way you will be able to run without setting params in the upcoming patch 1.5.0.

Solution 2

You can achieve this in either of following ways :

  • Run vscode as superuser:
    $ sudo code --user-data-dir=~/root
    This will open vscode without your previous settings (fresh) with superuser privileges and you can install your extensions.

OR

  • Follow these steps :
  1. sudo chown -R <user> <path_to_your_vscode_installation_directory>
  2. Hit follwing in terminal
    to check the current user on your machine :
    whoami
    for e.g. john
  3. You can find path of vscode directory using following command :
    whereis code
    e.g. in my case path is : /usr/share/code
  4. Now run :
    sudo chown -R john /usr/share/code
    This will run vscode with admin privilege
  5. Now install your extensions
  6. After reset owner back to root
    sudo chown -R root /usr/share/code

Solution 3

My simple solution is:

sudo code --verbose --user-data-dir --no-sandbox

(not as root but as sodoer user)

Solution 4

You can press Shift+Cmd+P in visual studio code and type:

shell command

and press "install" in the PATH.

Then in your terminal go to the folder that you want to open and type "code ." It'll open the project in visual studio code as root.

Solution 5

To able to run vs code as superuser: open code file with a text editor

sudo nano /usr/bin/code

then comment on the following line of codes :

if [ "$(id -u)" = "0" ]; then
    for i in "$@"
    do
        case "$i" in
            --user-data-dir | --user-data-dir=* | --file-write )
                CAN_LAUNCH_AS_ROOT=1
            ;;
        esac
    done
    if [ -z $CAN_LAUNCH_AS_ROOT ]; then
        echo "You are trying to start Visual Studio Code as a super user which isn't recommended. If this was intended, please specify an alternate user data directory using the \`--user-data-dir\` argument." 1>&2
        exit 1
    fi
fi

to

#if [ "$(id -u)" = "0" ]; then
#   for i in "$@"
#   do
#       case "$i" in
#           --user-data-dir | --user-data-dir=* | --file-write )
#               CAN_LAUNCH_AS_ROOT=1
#           ;;
#       esac
#   done
#   if [ -z $CAN_LAUNCH_AS_ROOT ]; then
#       echo "You are trying to start Visual Studio Code as a super user which isn't recommended. If this was intended, please specify an alternate user data directory using the \`--user-data-dir\` argument." 1>&2
#       exit 1
#   fi
#fi
Share:
63,423

Related videos on Youtube

Anargu
Author by

Anargu

Updated on July 10, 2022

Comments

  • Anargu
    Anargu almost 2 years

    I have Ubuntu 16.04 and I have to download the C# extension for Visual Studio Code, once I installed it, It doesn't make effect. Then, vscode give me and advice that I should open vscode with admin privileges to make effect of the extensions installed, so I wrote on my terminal:

    sudo code .

    but it doesn't work, the terminal throws me:

    It is recommended to start vscode as a normal user. To run as root, you must specify an alternate user data directory with the --user-data-dir argument.

    but I don't know how to specify an alternate user data directory. I was searching how to do that in visual studio code docs but there is not a reference for this issue. If you know how to open with admin privileges in linux please help me.

    • Michael
      Michael almost 3 years
      When running as root this worked for me. code --user-data-dir="~/.vscode-root" --no-sandbox You can also add this to your .bashrc, .zshrc etc. to simplify it. Then you can just run vscode command from the terminal: alias vscode='code --user-data-dir="~/.vscode-root" --no-sandbox'
  • Anargu
    Anargu almost 8 years
    Do you know when this patch will be able to get it?
  • Dauren Akilbekov
    Dauren Akilbekov almost 8 years
    code.visualstudio.com/insiders - here you can already have a look, for beta version. VSCode now updates monthly, so the stable 1.5.0, around second week of September.
  • jrefior
    jrefior over 3 years
    "You are trying to start Visual Studio Code as a super user which isn't recommended. If this was intended, please specify an alternate user data directory using the --user-data-dir argument" Still waiting for that change in October 2020
  • Veverke
    Veverke almost 3 years
    Thanks for that. It does not solve my problem, but at least sheds more light into it.
  • Payedimaunt
    Payedimaunt over 2 years
    This doesn't resolve. The message obviously doesn't appears but code don't starts
  • Payedimaunt
    Payedimaunt over 2 years
    for me doesn't works
  • motizukilucas
    motizukilucas over 2 years
    When I run this command nothing happens
  • toha
    toha over 2 years
    What is Cmd key?