Skip to content

Instantly share code, notes, and snippets.

@beauwilliams
Last active May 1, 2024 17:24
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save beauwilliams/69eabc42e540a309f53d55c4b8e6ffe3 to your computer and use it in GitHub Desktop.
Save beauwilliams/69eabc42e540a309f53d55c4b8e6ffe3 to your computer and use it in GitHub Desktop.
Run x86 VM's on Mac M1 arm using vagrant with qemu hypervisor
brew install vagrant qemu
#Due to dependency errors, we must install vbguest first..
vagrant plugin install vagrant-vbguest
vagrant plugin install vagrant-qemu
#cd to working dir you like to keep your vagrant files
cd ~/VM-and-containers/VagrantMachines/M1-vagrantfiles/ubuntu18-generic-64/
#Create a vagrant file
$EDITOR Vagrantfile
#Paste into the vagrantfile the recipe below used to launch the VM
#Setting qemu as hypervisor and importing a ubuntu image with support for libvrt
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu1804"
config.vm.provider "qemu" do |qe|
qe.arch = "x86_64"
qe.machine = "q35"
qe.cpu = "max"
qe.net_device = "virtio-net-pci"
end
end
#Save ./Vagrantfile
#Launch the VM, note, you can specify provider if using a Vagrantfile with mulltiple providers with vagrant up --provider qemu
vagrant up
#Get a shell inside the VM
vagrant ssh
#Check your VM is running x86
vagrant@ubuntu-1204:~$ uname -a
Linux ubuntu1804.localdomain 4.15.0-193-generic #204-Ubuntu SMP Fri Aug 26 19:20:21 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
#Success!
#FAQ
#ERROR
#If you face this error after running vagrant up
Vagrant SMB synced folders require the account password to be stored
in an NT compatible format. Please update your sharing settings to
enable a Windows compatible password and try again.
#SOLUTION
# Visit System Preferences -> Sharing -> File Sharing -> Options
# Tick to enable file sharing, see here -> https://i.ibb.co/F4ZwF4V/Screen-Shot-2022-09-29-at-10-49-21-am.png
#ERROR
#If you face this error after running vagrant up
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection reset. Retrying...
default: Warning: Remote connection disconnect. Retrying...
#SOLUTION
#You can hit ctrl+c here, and attempt to run vagrant ssh and it should work despite the apparent error.
@Obasoro
Copy link

Obasoro commented Oct 25, 2023

Thanks for this. I have UTM installed, can the vagrant up spin up a virtual machine in UTM

@jairenee
Copy link

I received the first error, the one about the password being in an NT-compatible format. I did exactly as was told, and matched the screenshot. Still receiving the error. I can't get past it no matter what I do. Is there an alternate way to troubleshoot that error? I need this up by TOMORROW, or I won't be ready for the biggest interview of my life, they sprung this on me.

@jovyllebermudez
Copy link

How's your interview @jairenee

@electropolis
Copy link

Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 50022 is already in use
on the host machine.

To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 22, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.

Nah every time it tries to that port

@arrold
Copy link

arrold commented Apr 17, 2024

@electropolis I had this, you can try vagrant global-status and then vagrant destroy 7f0829b or whatever ID's you find, but it didn't work for me, there's a bunch of stuff in ~/.vagrant.d/boxes and some of those have includes that pull from other files, the port is likely being reserved from these files.

If you're precious about what's in ~/.vargant.d then you may need to go through them, try to find what it is that's blocking that port from being used.

I'm just messing with building things so I just blew the whole directory away and it worked for me.

@zharling
Copy link

Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 50022 is already in use
on the host machine.

To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 22, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.

Nah every time it tries to that port

Add qe.ssh_port to your provider conifg.

eg:

config.vm.provider "qemu" do |qe|
    qe.ssh_port = "54321"
    qe.arch = "x86_64"
    qe.machine = "q35"
    qe.cpu = "max"
    qe.net_device = "virtio-net-pci"
  end

@szilvesztercsab
Copy link

Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 50022 is already in use
on the host machine.

To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 22, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.

Nah every time it tries to that port

Add qe.ssh_port to your provider conifg.

eg:

config.vm.provider "qemu" do |qe|
    qe.ssh_port = "54321"
    qe.arch = "x86_64"
    qe.machine = "q35"
    qe.cpu = "max"
    qe.net_device = "virtio-net-pci"
  end

That doesn't seem to help at all. Whatever port I set it's the same error just with a different port.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment