Vagrant error installing nginx

How to install Nginx in Vagrant when getting an error setting ownership of '/var/www/html.dpkg-new': Operation not permitted

Trying to install nginx in Vagrant should be an easy task. A simple sudo apt-get install nginx -y and you're good to go.

Except when you encounter an error.

error setting ownership of '/var/www/html.dpkg-new': Operation not permitted

There are many reasons why this can happen in a UNIX environment. However, in Vagrant, it's most likely you have something mounted there.

There are two fixes:

Change your Vagrantfile

Change your Vagrantfile to match this format to mount the directory:

config.vm.synced_folder "/home/rohara/Workspace", "/var/www", id: "v-root", mount_options: ["rw", "tcp", "nolock", "noacl", "async"], type: "nfs", nfs_udp: false

Do it live

Ensure you have no active sessions (tmux, screen, etc.) or processes running out of the directory in question (/var/www) and unmount it:

sudo umount /var/www

The installation of nginx will now complete after running the fix-broken flag, and we'll move the web files it installed, then mount the directory back:

sudo apt-get install --fix-broken
sudo mv /var/www{,.bak}
sudo mount -a

Afterwards you just need a vagrant reload from the host and you can mount to /var/www as expected.

Note: This can apply to other directories or applications; a web server installation can easily conflict with mounted directories.