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)

  • 6
    why not just set the permissions on your project folder to allow it to save? – TheWanderer Jul 27 '16 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 '16 at 1:07
  • 1
    Where is your project folder – Nuwan Thisara Jul 27 '16 at 2:17
  • 1
    @Nuwan /var/www/project (im using Apache with vhost) – wdarking Jul 27 '16 at 2:22
  • 2
    Possible duplicate of How to avoid using sudo when working in /var/www? – David Foerster Sep 24 '16 at 23:45

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.

| improve this answer | |
  • 4
    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? – zwitterion Dec 14 '16 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? – Rayee Roded Mar 8 '17 at 22:08
  • 1
    Yes it's possible, open another terminal. – Lucas D.A.W. Dec 1 '17 at 10:26
  • 4
    @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 '18 at 19:01
  • 3
    @zawata you are perfectly right! Please excuse a novice. Thanks for the heads up! – Marco Dufal May 23 '18 at 19:28

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.

| improve this answer | |
  • 4
    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. – Joshua Kemmerer Aug 22 '18 at 15:33
  • Then you can follow the accepted answer given above. askubuntu.com/a/820205/367228 – Talha Junaid Aug 22 '18 at 16:51
  • This is great unless you're working on remote SSH – Seth Painter Mar 31 at 8:01
  • Best answer fot me! Thanks! – lukasz Jun 1 at 6:52

Giving root privilege to the VS Code or any other user-level application is not a great idea. Instead of giving root privilege to the VS Code, giving permission to your project folder is a much better idea.

You can use sudo chmod -R 777, but honestly, I would not recommend it, because it gives the full access to your project directory to all users on the machine.

You can use chown on the project directory to own it only for yourself

cd /location

sudo chown your_username your_project_directory


let's say I have a directory called project1 in my /var/opt location and my user name is mir

cd /var/opt

sudo chown mir project1

Now launch your VS Code and try to create some file, modify and save it, you will not ask any root permission again

However, if you already created some file before applying chown, don't forget to change their permission also

sudo chown your_username your_project_directory/your_file_name


sudo chown mir project1/file1.txt
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.