准备材料

按住 Ctrl 点击链接可在新页打开, 先安装 VirtualBox 和 Vagrant
VirtualBox
Vagrant
Homestead 虚拟机盒子 下载密码: zkmw
Homestead 管理脚本

开始安装

以下命令在 git-bash (Windows) 或 Terminal (macOS) 执行
下载虚拟机盒子并解压, 进入虚拟机盒子所在目录

# Windows 使用 git-bash /e/XXX 代表 E:\XXX 目录
> cd /e/homestead-virtualbox-6.0.0
> vagrant box add metadata.json
添加 Vagrant box 成功

克隆管理脚本到用户目录

> cd ~
> git clone https://github.com/laravel/homestead.git Homestead
> cd Homestead
# 初始化脚本
> bash init.sh
# 启动虚拟机
> vagrant up
# 此处 Windows 下可能出现未知的问题, 解决方法见FAQ

Homestead 配置

查看效果

FAQ

问题(1) 启动时报错但虚拟机运行正常?
==> homestead-7: Setting hostname...
==> homestead-7: Configuring and enabling network interfaces...
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

/sbin/ifdown 'eth1' || true
/sbin/ip addr flush dev 'eth1'
# Remove any previous network modifications from the interfaces file
sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre
sed -ne '/^#VAGRANT-END/,$ p' /etc/network/interfaces | tac | sed -e '/^#VAGRANT-END/,$ d' | tac > /tmp/vagrant-network-interfaces.post

cat \
  /tmp/vagrant-network-interfaces.pre \
  /tmp/vagrant-network-entry \
  /tmp/vagrant-network-interfaces.post \
  > /etc/network/interfaces

rm -f /tmp/vagrant-network-interfaces.pre
rm -f /tmp/vagrant-network-entry
rm -f /tmp/vagrant-network-interfaces.post

/sbin/ifup 'eth1'

Stdout from the command:

Stderr from the command:

bash: line 5: /sbin/ifdown: No such file or directory
bash: line 21: /sbin/ifup: No such file or directory
解决(1)
  1. 打开 VirtualBox
  2. 找到 homestead-7 右击显示
  3. 用户名 vagrant 密码 vagrant 登录进入Ubuntu虚拟机
    # 安装 ifupdown 软件
    $ sudo apt-get install ifupdown
    # 安装完成关闭虚拟机
    $ shutdown -h now
    
  4. 回到本地环境重启虚拟机
    > cd ~/Homestead && vagrant up
    
问题(2) windows 下 vagrant ssh 命令不能使用?
> vagrant.exe ssh
Welcome to Ubuntu 18.04 LTS (GNU/Linux 4.15.0-20-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu May 10 06:15:35 UTC 2018

  System load:  0.0               Processes:           124
  Usage of /:   8.2% of 61.80GB   Users logged in:     0
  Memory usage: 19%               IP address for eth0: 10.0.2.15
  Swap usage:   0%                IP address for eth1: 192.168.10.10

 * Meltdown, Spectre and Ubuntu: What are the attack vectors,
   how the fixes work, and everything else you need to know
   - https://ubu.one/u2Know

0 packages can be updated.
0 updates are security updates.

卡在这里不动了!! 再见!!!
解决(2)
ssh vagrant@192.168.10.10  

把homestead加入hosts文件: 192.168.10.10 homestead 后可使用下面方法连接

ssh vagrant@homestead
问题(3) windows 下 yarn install 执行最终报错?
> yarn install
yarn install v1.6.0
info No lockfile found.
[1/4] Resolving packages...
warning laravel-mix > img-loader > imagemin-gifsicle > gifsicle > bin-build > download > gulp-decompress > gulp-util@3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
[2/4] Fetching packages...
info fsevents@1.2.4: The platform "linux" is incompatible with this module.
info "fsevents@1.2.4" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
error An unexpected error occurred: "EPROTO: protocol error, symlink '../acorn/bin/acorn' -> '/home/vagrant/Code/laravel/node_modules/acorn-dynamic-import/node_modules/.bin/acorn'".
info If you think this is a bug, please open a bug report with the information provided in "/home/vagrant/Code/laravel/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

又是在Windows上的问题, 问我已经是在Ubuntu虚拟机了为什么还会这样. 我也不知道...
解决(3)
yarn install --no-bin-links

package.jsoncross-env 修改为 node_modules/cross-env/dist/bin/cross-env.js (大概4处)

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "axios": "^0.17",
        "bootstrap-sass": "^3.3.7",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^1.0",
        "lodash": "^4.17.4",
        "vue": "^2.5.7"
    }
}