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)

Zanna
  • 70,465
wdarking
  • 1,043

3 Answers3

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.

zawata
  • 1,174
  • 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 '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 08 '17 at 22:08
  • 1
    Yes it's possible, open another terminal. – Lucas D.A.W. Dec 01 '17 at 10:26
  • If you just want to be able to save a files which you are disallowed permission to just grant full permission to the project folder: sudo chmod -R 777 <project_dir_name> – n1nsa1d00 Feb 04 '18 at 23:13
  • Please describe me "very dangerous" under context of VS Code. – Teoman shipahi Feb 06 '18 at 16:22
  • 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 '18 at 19:01
  • 3
    @zawata you are perfectly right! Please excuse a novice. Thanks for the heads up! – n1nsa1d00 May 23 '18 at 19:28
  • Great answer! But I am still having a little problem: When I open a file with double click or by enter button directly from folder, the OS opens VS Code in normal mode, not admin. Is it possible change default program to admin too? – Doglas Jun 18 '18 at 17:40
  • @Doglas Visual Studio Code is not going to auto-detect whether it needs to open in root. Especially when running from a file browser. This answer was to notify you on how to open it in root mode manually because it actively attempts to not do so on it's own. If you want the appllication to always open as root you could check this stack overflow answer which should work for most popular desktop environments. But that is a terrible Idea. In modern versions of VSCode you can just see Talha-junaids answer – zawata Jun 20 '18 at 22:56
  • Just FYI the user data dir needs to be outside the home folder, or the command will exit silently and do nothing. – jjxtra Jan 18 '19 at 03:01
  • To sudo or not to sudo is always the question huh :) I have project that requires me to listen on port 21. This fails if you can't run as root because it's a privileged port. I guess the list of reasons for sudoing goes on and on – DerpyNerd Apr 01 '21 at 20:37
  • in my vs code, if I need to change a file not owned by the user e.g. /etc/hosts, it tells me I should try with sudo and shows me authentication dialogue for sudo. So even when I am running vs code as a normal user, for sudo task it prompts. I am not having to run vscode as root. – Kunal Shah Aug 03 '21 at 15:03
  • trying to run a unit test that requires running dpkg install on WSL2/Ubuntu. I get:

    sudo code .

    sudo: code: command not found

    or if I try to be more verbose:

    sudo /home/<myuser>/.vscode-server/bin/f30a9b73e8ffc278e71575118b6bf568f04587c8/bin/code . --user-data-dir=/home/<myuser>/.vscode-server --no-sandbox

    Command is only available in WSL or inside a Visual Studio Code terminal.

    – Mauro Giusti Jan 31 '22 at 18:58
  • This is not really supported. – Veverke Apr 02 '23 at 08:42
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.

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