I have downloaded the laravel/homestead box manually from here.

我成功地添加了该框:

vagrant box add file:///path/to/the/laravel/homestead.box --name 'laravel/homestead'

但当我运行vagrant up时,它会显示:Box 'laravel/homestead' could not be found,尽管vagrant box list显示框.

下载页面显示运行vagrant init laravel/homestead生成vagrantfile,但是laravel/homestead存储库本身提供vagrantfile.

我可以用vagrant init laravel/homestead生成的vagrantfile替换vagrant up,但它缺少laravel/homestead存储库vagrantfile中的基本配置.

这是生成的vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "laravel/homestead"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   sudo apt-get update
  #   sudo apt-get install -y apache2
  # SHELL
end

它有以下设置:

Vagrant.configure(2) do |config|  
    config.vm.box = "laravel/homestead"
end

我试图将其添加到默认的laravel/homesteadvagrantfile中,但不起作用.

require 'json'
require 'yaml'

VAGRANTFILE_API_VERSION = "2"
confDir = $confDir ||= File.expand_path("~/.homestead")

homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
aliasesPath = confDir + "/aliases"

require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    if File.exists? aliasesPath then
        config.vm.provision "file", source: aliasesPath, destination: "~/.bash_aliases"
    end

    if File.exists? homesteadYamlPath then
        Homestead.configure(config, YAML::load(File.read(homesteadYamlPath)))
    elsif File.exists? homesteadJsonPath then
        Homestead.configure(config, JSON.parse(File.read(homesteadJsonPath)))
    end

    if File.exists? afterScriptPath then
        config.vm.provision "shell", path: afterScriptPath
    end

    ## HERE I added the setting ############################################
    config.vm.box = "laravel/homestead"
    ########################################################################
end

What should I do?

推荐答案

The Vagrantfile provided by the laravel/homstead project is more advanced than a generic Vagrantfile that is generated by vagrant init

The Vagrantfile provided by the laravel/homstead project uses some ruby code to assist in setting up the vagrant environment. What we can see from the homestead ruby code is that it is checking that you have a box with a version greater than or equal to 0.4.0:

config.vm.box_version = settings["version"] ||= ">= 0.4.0"

As you added the box manually you will see that it is present on your local machine:

$ vagrant box list
laravel/homestead               (virtualbox, 0)

Note however the number next to the provider is 0. That number is the box version. As the box was added manually the box metadata was not available and by default you will get a version of 0.

When you now do a vagrant up the code is checking if you have a box >= 0.4.0 which you don't have so is why you are getting Box 'laravel/homestead' could not be found. It would then attempt to download the box at the minimal version required.

为了避免这种情况,您可以创建一个元数据.json文件与下载的box文件位于同一目录中.例如:

{
    "name": "laravel/homestead",
    "versions": [{
        "version": "0.4.0",
        "providers": [{
            "name": "virtualbox",
            "url": "file:///path/to/homestead.box"
        }]
    }]
}

然后跑vagrant box add metadata.json

This will install the box with a version and can be confirmed by:

$ vagrant box list
laravel/homestead               (virtualbox, 0.4.0)

现在,您可以使用本地框执行vagrant up次.

Laravel相关问答推荐

Inertia React中的错误文件上传更新

如何使中继器项目计数动态Laravel Filament V3?

使用Laravel判断其他数据库中是否存在记录

如何在 Laravel 中使用 return 停止 Trait php 的执行

Livewire 基于下拉 Select 自动填充输入框

如何解决此 Backblaze B2 S3 兼容 API 错误?

Laravel 9 上的数组差异助手

使用Laravel Blade Formatter和PHP Intelephense进行 VSCode 格式化

Laravel 5 - 手动分页

具有实时和 WebSockets 的 Angular2 + Laravel

使用 Laravel 限制多态多对多关系中的相关记录

413请求实体在laravel homestead for windows中太大的nginx服务器

用于集合的 Laravel 5.5 API 资源

Laravel 事件、监听器、作业(job)、队列之间的区别

Laravel 表:只能有一个自动列,并且必须定义为键

如何卸载 Laravel Passport

Laravel php artisan 产生错误

在 laravel Passport 客户端应用程序中使用访问令牌获取用户数据

如何在特定项目中禁用初始化 JS/TS 语言功能?

如何访问 Laravel 集合中的第 n 个项目?