Categories
DevOps

How to use specific Vagrant box version

Use specific Vagrant box version instead of the latest one.

Display Vagrantfile configuration. It will use the latest available box version.

$ cat Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "debian/buster64"

  config.vm.network :private_network, ip: "172.16.0.77"

  config.vm.provider :virtualbox do |v|
    v.memory = 2048
    v.cpus = 2
  end
end

Display current box versions.

$ vagrant box list
centos/7                  (virtualbox, 2004.01)      
debian/bullseye64         (virtualbox, 11.20210228.1)
debian/bullseye64         (virtualbox, 11.20210409.1)
debian/buster64           (virtualbox, 10.20210228.1)
ubuntu/bionic64           (virtualbox, 20210412.0.0)
ubuntu/groovy64           (virtualbox, 20210316.0.0)

Update box version.

$ vagrant box update
==> default: Checking for updates to 'debian/buster64'
    default: Latest installed version: 10.20210228.1
    default: Version constraints:
    default: Provider: virtualbox
==> default: Updating 'debian/buster64' with provider 'virtualbox' from version
==> default: '10.20210228.1' to '10.20210409.1'...
==> default: Loading metadata for box 'https://vagrantcloud.com/debian/buster64'
==> default: Adding box 'debian/buster64' (v10.20210409.1) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/debian/boxes/buster64/versions/10.20210409.1/providers/virtualbox.box
Download redirected to host: vagrantcloud-files-production.s3-accelerate.amazonaws.com
==> default: Successfully added box 'debian/buster64' (v10.20210409.1) for 'virtualbox'!

It will use the latest available box version.

$ vagrant box list 
debian/bullseye64         (virtualbox, 11.20210228.1)
debian/bullseye64         (virtualbox, 11.20210409.1)
debian/buster64           (virtualbox, 10.20210228.1)
debian/buster64           (virtualbox, 10.20210409.1)
ubuntu/bionic64           (virtualbox, 20210412.0.0)
ubuntu/groovy64           (virtualbox, 20210316.0.0)
$ vagrant up                                                                                                                           
Bringing machine 'default' up with 'virtualbox' provider...                                                                            
==> default: Checking if box 'debian/buster64' version '10.20210409.1' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2202.              
==> default: Clearing any previously set network interfaces...                                                                         
==> default: Preparing network interfaces based on configuration... 
    default: Adapter 1: nat                                                                                                            
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2202 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...      
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2202          
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!

But you can define a specific box version to use.

$ cat Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box         = "debian/buster64"
  config.vm.box_version = "10.20210228.1"

  config.vm.network :private_network, ip: "172.16.0.77"

  config.vm.provider :virtualbox do |v|
    v.memory = 2048
    v.cpus = 2
  end
end
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'debian/buster64' version '10.20210228.1' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2202.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2202 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2202
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!

This is useful at times, as things can go haywire after update.

Additional notes

More details about versioning.