91

I recently installed Visual Studio Code editor. I downloaded the 32bit .deb package. Installed and run normally, but when i tried to save a file in my project folder, it returns permission denied. So, i re-run code with

sudo code .

and it returns

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.

I looked for how to specify an alternate user data directory with the --user-data-dir argument but I didn't find much.

So, how do I run Visual Studio Code as root in Ubuntu 16.04?

edit: I would prefer a solution that avoids changing directory permissions.

edit2: project directory: /var/www/project (I'm using Apache with vhost)

5
  • 7
    why not just set the permissions on your project folder to allow it to save? Jul 27, 2016 at 1:04
  • 2
    I don't understand why you want to run code as root; this is potentially dangerous. You should figure out the real reasons why you can't save the file.
    – edwinksl
    Jul 27, 2016 at 1:07
  • 2
    Possible duplicate of How to avoid using sudo when working in /var/www? Sep 24, 2016 at 23:45
  • If Visual Studio is a graphical application (non-text based) you should use gksu instead of sudo. Sep 25, 2016 at 1:54
  • 1
    @edwinksl the issue is probably coming from cmake install targets that, when built within vscode, want to write files to various folders. sudo-ing is def. a bad idea, but you can't expect people to start reading build scripts to find out what they have to chown.. building inside a container is one solution, but it's not ideal.
    – StarShine
    Dec 18, 2020 at 9:27

3 Answers 3

105

It is very dangerous to run VScode as root (as it is any other application) however, should you absolutely need to:

You must specify the user data directory to use when running vs code:

sudo code --user-data-dir="~/.vscode-root"

Then from the window you can open your project folder

Again: This is not recommended. Have fun.

Update May 2018

For everyone asking why this isn't recommended, you clearly don't understand why sudo even exists. From this AskUbuntu Question:

It defeats the security model that's been in place for years. Applications are meant to be run with non-administrative security (or as mere mortals) so you have to elevate their privileges to modify the underlying system. For example, you wouldn't want that recent crash of Rhythmbox to wipe out your entire /usr directory due to a bug. Or that vulnerability that was just posted in ProFTPD to allow an attacker to gain a ROOT shell.

It's just good practice on any operating system to run your applications on a user level and leave administrative tasks to the root user, and only on a per-need basis.

I'm not saying that you should never use VSCode as root(though its pretty easy to avoid doing) I'm saying you should be careful when you want to do so and know the risks.

The absolute best thing to do to is make a copy of a restricted file, edit it, and copy it back when you're absolutely sure it's finished.

Also for editing files to which your user does not have permissions, I would now recommend Talha Junaid's answer which asks for permissions every single time you want to save a file. The prompt for root access did not exist when I wrote this answer.

14
  • 8
    why it is not recommended to run VScode as root? How to change something in hosts for example? I will need root permission is not?
    – IgorAlves
    Dec 14, 2016 at 16:59
  • 1
    Thanks it is working: now I have one VSCode window running as root and I want to start another vscode window as root, Is that possible? Mar 8, 2017 at 22:08
  • 1
    Yes it's possible, open another terminal. Dec 1, 2017 at 10:26
  • 5
    @MarcoDufal Absolutely not. This is a horrible idea. Especially when working with OS files you should absolutely not set the permission to 777. that means that everything on the system regardless of who/what started it or what it's allowed to do has permission to modify that set of files and folders. At the very most you should "Own" the files so you are allowed to edit them with something closer to 755 permissions.
    – zawata
    May 23, 2018 at 19:01
  • 3
    @zawata you are perfectly right! Please excuse a novice. Thanks for the heads up!
    – n1nsa1d00
    May 23, 2018 at 19:28
34

Just in case you want make changes to a single file I would prefer the following approach.

  • Run vs-code as regular user.
  • make changes to file
  • File -> Save or press Ctrl + S
  • vs-code will pop up error in right bottom corner saying

    Failed to save 'grub': Insufficient permissions. Select 'Retry as Admin' to retry as administrator

    enter image description here

  • Click on Retry as Admin. It will ask for administrator credentials

    enter image description here

  • Enter credentials and enjoy saving files with vs-code.

9
  • 13
    Is there any way to do this without having to do this every time for every single file? I have hundreds of files I need to make changes to and VS Code keeps asking for my password. I'm about ready to throw my laptop against the wall. Aug 22, 2018 at 15:33
  • 5
    I dont get this option
    – Dawoodjee
    Jul 14, 2021 at 18:22
  • 1
    A way to avoid being asked for the root pw each time would be to temporarily change writing rights of the file(s), do your changes and - and I cannot stress that enough - reset the files rights to its original state thereafter!!! Should also work on remote ssh.
    – Robert
    Oct 8, 2021 at 9:51
  • 3
    I use to have this option, but no more now..
    – themhz
    Jan 4, 2022 at 9:11
  • 3
    Same here option seems to have disappeared :| Feb 8, 2022 at 15:08
8

to open vs-code as root, open terminal and:

sudo code /directory-to-open --user-data-dir='.' --no-sandbox

make sure you check this too: https://stackoverflow.com/a/68637450/10606346

1
  • 5
    For me, this just opens a white screen of VS Code. Nothing more. Mar 3, 2023 at 6:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .