安装准备

1、简介

使用 Vagrant + VirtualBox 的方式管理虚拟机极大方便了多操作系统环境需求的用户。

通过 Vagrant 安装与管理虚拟机,使得我们不用再去网上下载硕大的 ISO 镜像,然后再在虚拟机软件上执行多达 10 多步的设置。然后还得花上至少几十分钟时间等待安装,这样的过程太冗长了。

我们可以通过 Vagrant 快速安装一个无界面的操作系统,甚至只需要两行命令即可。而且无界而操作系统的镜像大小也小了很多。

但是,Vagrant 管理的镜像即使已经很小了,默认从位于国外的服务器下载仍然需要不少时间。

因此,本文介绍了如何使用官方的方式通过 Vagrant 创建 Ubuntu 18 虚拟机,以及如何加速创建虚拟器的过程。

2、安装环境

MacOS 10.15.6
Vagrant 2.2.10
VirtualBox 6.1.12

安装步骤

1、首先去 vagrant boxes 官网搜索需要的 box

https://app.vagrantup.com/boxes/search

点击一个需要使用的 box,这里以 Official Ubuntu 18.04 LTS 为例。点击后,在新页面可以看到如下内容:

Vagrantfile tab 给出了 Vagrantfile 的文件内容,我们可以复制这部分内容到本地的 Vagrantfile 中。

New tab 页给出了使用 vagrant init 命令来初始化 Vagrantfile 以及启动虚拟机的命令。

2、安装虚拟机

这里以使用 vagrant init 初始化 Vagrantfile 为例。

(1) 创建一个 vagrant 虚拟机的目录

➜  vagrant pwd
/Users/jiangzhuolin/Workspace/vagrant
➜  vagrant
➜  vagrant
➜  vagrant mkdir ubuntu_18.04_x64

这个目录可以位于任意当前用户有权限访问的位置

(2) vagrant init

vagrant init ubuntu/bionic64

示例:

➜  vagrant cd ubuntu_18.04_x64
➜  ubuntu_18.04_x64
➜  ubuntu_18.04_x64 pwd
/Users/jiangzhuolin/Workspace/vagrant/ubuntu_18.04_x64
➜  ubuntu_18.04_x64
➜  ubuntu_18.04_x64 vagrant init ubuntu/bionic64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
➜  ubuntu_18.04_x64 ll
total 8
drwxr-xr-x  4 jiangzhuolin  staff   128 Sep 28 01:32 ./
drwxr-xr-x  6 jiangzhuolin  staff   192 Sep 10 03:15 ../
drwxr-xr-x  4 jiangzhuolin  staff   128 Sep  4 13:31 .vagrant/
-rw-r--r--  1 jiangzhuolin  staff  3021 Sep 28 01:32 Vagrantfile

可以看到,执行了 vagrant init 命令之后,该目录下就自动生成了一个 Vagrantfile 的文件。这个文件也可以我们自己创建然后复制上面 vagrant box 网站上给出的 Vagrantfile 内容。

(3) vagrant up

执行 vagrant up 启动虚拟机

示例:

➜  ubuntu_18.04_x64 vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'ubuntu/bionic64' could not be found. Attempting to find and install...default: Box Provider: virtualboxdefault: Box Version: >= 0
==> default: Loading metadata for box 'ubuntu/bionic64'default: URL: https://vagrantcloud.com/ubuntu/bionic64
==> default: Adding box 'ubuntu/bionic64' (v20200922.0.0) for provider: virtualboxdefault: Downloading: https://vagrantcloud.com/ubuntu/boxes/bionic64/versions/20200922.0.0/providers/virtualbox.box
Download redirected to host: cloud-images.ubuntu.com
Progress: 0% (Rate: 14246/s, Estimated time remaining: 5:35:44)

可以看到的是,执行 vagrant up 后,vagrant 还会从远程的 vagrantcloud 中去下载 box,而这个下载速度也着实是感人。

3、加速 vagrant 安装

提升 vagrant 的使用体验有两种,一种是手动离线下载我们需要的 box,另一种则是使用国内镜像源在线下载。

(1) 手动下载 box 文件

https://vagrantcloud.com/ubuntu/boxes/bionic64/versions/20200922.0.0/providers/virtualbox.box

通过某雷下载加速能比直接从官网下载快一些…

还是觉得慢???试试从清华大学镜像源下载

https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/20200922/bionic-server-cloudimg-amd64-vagrant.box

这样的速度你开心了吗

将下载的 box 文件添加到 vagrant box 中

vagrant box add --name {box_name} box_file_path

注:

(1) box_name 是自定义的名称,可自己定义,但不能与已有的 box 重复。可使用 vagrant box list 查看已有的 box name

➜  ubuntu_18.04_x64 vagrant box list
centos/7      (virtualbox, 2004.01)
ubuntu_bionic (virtualbox, 0)

(2) box_file_path 是下载的 box 文件的路径加名称。

示例:

➜  ubuntu_18.04_x64 pwd
/Users/jiangzhuolin/Workspace/vagrant/ubuntu_18.04_x64
➜  ubuntu_18.04_x64 vagrant box add --name ubuntu_bionic ~/Downloads/bionic-server-cloudimg-amd64-vagrant.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu_bionic' (v0) for provider:box: Unpacking necessary files from: file:///Users/jiangzhuolin/Downloads/bionic-server-cloudimg-amd64-vagrant.box
==> box: Successfully added box 'ubuntu_bionic' (v0) for 'virtualbox'!

创建 vagrant 虚拟机

➜  ubuntu_18.04_x64 rm Vagrantfile
➜  ubuntu_18.04_x64 vagrant init ubuntu_bionic
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
➜  ubuntu_18.04_x64 vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu_bionic'...
==> default: Matching MAC address for NAT networking...
config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> default: Clearing any previously set network interfaces...default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations..
...default: Guest Additions Version: 5.2.42default: VirtualBox Version: 6.1
==> default: Mounting shared folders...default: /vagrant => /Users/jiangzhuolin/Workspace/vagrant/ubuntu_18.04_x64
➜  ubuntu_18.04_x64

注: vagrant init 后面的名称与上面 vagrant box add –name {box_name} 这个名称必须一致。

(2) 使用镜像地址初始化

初始化:

➜  ubuntu_18.04_x64 vagrant init ubuntu_bionic https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box
...
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

运行:

➜  ubuntu_18.04_x64 vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu_bionic'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntu_1804_x64_default_1601230235185_9559
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...default: Adapter 1: nat
==> default: Forwarding ports...default: 22 (guest) => 2222 (host) (adapter 1)default: SSH auth method: private key
...default: Guest Additions Version: 5.2.42default: VirtualBox Version: 6.1
==> default: Mounting shared folders...default: /vagrant => /Users/jiangzhuolin/Workspace/vagrant/ubuntu_18.04_x64

注: 即使是在线下载 box 的方式,也只需要第一次会从远程库进行下载,如果再使用 Ubuntu 18.04,则会从本地的缓存中加载,也方便了我们使用 Vagrant。

运行验证

使用 vagrant ssh 可进入 Ubuntu 虚拟机环境,可查看系统信息。

➜  ubuntu_18.04_x64 vagrant ssh
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-118-generic x86_64)
* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantageSystem information as of Sun Sep 27 18:11:34 UTC 2020System load:  0.33              Processes:             100Usage of /:   10.5% of 9.63GB   Users logged in:       0Memory usage: 12%               IP address for enp0s3: 10.0.2.15Swap usage:   0%
0 packages can be updated.
0 updates are security updates.
vagrant@ubuntu-bionic:~$
vagrant@ubuntu-bionic:~$ cat /etc/issue
Ubuntu 18.04.5 LTS \n \l

也是能从 VirtualBox 中找到已经在运行的 Ubuntu 18.04 的虚拟机的!

附录

Vagrant Official Website

https://www.vagrantup.com/

Vagrant boxes website

https://app.vagrantup.com/boxes/search

清华大学镜像站

https://mirrors.tuna.tsinghua.edu.cn/

参考:

https://blog.csdn.net/dafei1288/article/details/105828516/
https://blog.csdn.net/weixin_44631631/article/details/102587491

【原创】MacOS 上使用 Vagrant + VirtualBox 安装 Ubuntu 18.04相关推荐

  1. 苹果系统 macOS Mojave 10.14.4上安装 Ubuntu 18.04 双系统

    macOS Mojave 10.14.4 安装Ubuntu 18.04 双系统 0. 准备工作 & 系统情况 1. 制作Ubuntu安装盘(U盘) 2. 硬盘分区 3. 安装Ubuntu 4. ...

  2. 苹果系统 macOS Catalina 10.15.7 安装 Ubuntu 20.04 双系统 采用rEFInd引导

    苹果系统 macOS Catalina 10.15.7 安装 Ubuntu 20.04 双系统 采用rEFInd引导 0. 前言 1. 准备工作 & 系统情况 2. 制作Ubuntu安装盘(U ...

  3. 台式机下成功在windows10的基础上安装Ubuntu 18.04 LTS 系统(详细教程)

    台式机成功在windows10的基础上安装Ubuntu 18.04 LTS 系统 1 前言 因为工作缘故经常需要使用windows和ubuntu两个不同的计算机系统,因此想把实验室的台式机安装成双系统 ...

  4. 安装Ubuntu 18.04后的一些操作

    安装Ubuntu 18.04后的一些操作 1.设置root密码 sudo passwd 2.登陆root(输入密码后看到井号就是切换成为root账户) su 3.删除预装 apt-get remove ...

  5. WSL下安装ubuntu 18.04 +meep进行FDTD仿真计算

    WSL下安装ubuntu 18.04 +meep进行FDTD仿真计算 前言 WSL 安装过程 打开虚拟环境 下载Ubuntu并修改安装路径 更改软件源 Meep VScode远程访问 测试程序 前言 ...

  6. Win10 环境下安装Ubuntu 18.04过程记录

    Win10 环境下安装Ubuntu 18.04过程记录 步骤1:下载镜像文件 从Ubuntu官网https://www.ubuntu.com/download/server下载系统的iso镜像文件 , ...

  7. Dell Alienware 13 R3 win10 安装 ubuntu 18.04 总结

    以下部分是自己在Alienware 上安装ubuntu 18.04的总结,带序号的部分是自己的亲身经历和自己认为比较靠谱的安装方式,其他方式没有尝试.如果有任何问题,欢迎留言讨论. 1.修改硬盘模式从 ...

  8. 双系统下卸载ubuntu 20.04再安装ubuntu 18.04的相关方法、软件以及出现ACPI Error错误和解决办法

    文章目录 前言 一.删除原来的ubuntu 20.04系统和开机引导 二.制作ubuntu18.04系统盘 三.安装ubuntu 18.04(已经有一个windows 10系统情况下) 四.安装完重启 ...

  9. ThinkPad X1 Carbon 安装Ubuntu 18.04到移动硬盘 教程指南

    ThinkPad X1 Carbon 安装Ubuntu 18.04到移动硬盘 教程指南 安装准备 步骤 下载ubuntu 制作启动盘 关闭Win系统快速启动 设置移动硬盘 设置BIOS 安全启动设置 ...

最新文章

  1. LeetCode实战:二叉树中的最大路径和
  2. java初学者必看——J2SE小结
  3. 智能网联技术 英文_智能网联技术是什么?有什么好推荐学习地方?
  4. 用forif循环测量minst0-6的特征迭代次数曲线
  5. 几种搜索引擎算法的研究
  6. 手机连接服务器传输文件,手机云服务器传输文件
  7. Hi3516A开发--编译整个osdrv目录所遇到的问题总结
  8. JZOJ 5425. 【NOIP2017提高A组集训10.25】数论
  9. AIRAVATA:量化机器学习中的参数泄露
  10. 网管的一天——按小时记录生活
  11. mac java maven 设置
  12. java short float_Java Short floatValue()用法及代码示例
  13. 教育真的可以改变命运?
  14. nlog自定义文件名
  15. solr 使用记录 - solr dataimporter 定时任务
  16. Linux之find xargs
  17. java指定一个具体日期
  18. 微信小程序 post git 的请求坑
  19. FPGA-Verilog实例----线性反馈移位寄存器(LFSR)
  20. 博客群建软件有什么功能?

热门文章

  1. 叮,关于NPL与深度学习的“干货”请查收!
  2. C++ Primer 0x0C 学习笔记
  3. keepalived工作原理和配置说明
  4. 相似度计算——欧氏距离、汉明距离、余弦相似度
  5. jQuery (JavaScript 脚本库)
  6. python模拟登陆 pixiv
  7. 什么计算机玩游戏好,电脑玩游戏什么配置好
  8. SAP PM 入门系列9 – PM模块的User-Exits BADIs
  9. Python OpenCV开发MR智能人脸识别打卡系统(三、工具模块设计)
  10. 投影仪和电视哪个更适合家庭用?买当贝投影好么?