如何在Linux上安装Vagrant

时间:2020-03-21 11:44:41  来源:igfitidea点击:

在本教程中,我们将看到如何在Linux操作系统上安装Vagrant。
安装Vagrant很容易!我们所要做的就是要头通过Vagrant下载页面,获取适合Linux发行版的合适的预编译二进制版本,并将其安装为我们通常安装其他应用程序的方式。
我们还可以从默认存储库中安装Vagrant。
其中我包含在Linux框中安装Vagrant的所有可能方法。

在Linux上安装Vagrant

VAGRANT可在大多数Linux和UNIX发行版的官方存储库中提供。
所以我们可以使用默认包管理器安装Vagrant 。

例如,运行以下命令在Debian,Ubuntu,Linux Mint上安装Vagrant:

$sudo apt install vagrant

关于Fedora:

$sudo dnf install vagrant

在SUSE/OpenSUSE上:

$sudo zypper install vagrant

存储库中的官方版本可能非常过时。
如果我们想要最新版本,请转到官方VAGRANT下载页面并下载Linux操作系统的最新二进制文件。

例如,如果我们正在使用Debian和其他基于Debian的系统,如Ubuntu,则只需下载.deb版本。
首先是撰写本教程,最新版本为2.2.10.
它适用于32位和64位架构。

下载.deb文件后,转到下载部分并将其安装如下:

$sudo apt install gdebi
$sudo gdebi vagrant_2.2.10_x86_64.deb

其中我使用"GDEBI"的原因是因为它自动安装必要的依赖项。

如果我们已下载.rpm文件,则可以使用命令安装它:

$sudo dnf localinstall vagrant_2.2.10_x86_64.rpm

或者,

$sudo yum localinstall vagrant_2.2.10_x86_64.rpm

安装虚拟机管理程序

安装Vagrant后,我们需要安装任何虚拟化虚拟机管理程序,例如VirtualBox或者KVM。
因为,Vagrant不是独立的虚拟化软件。
它只是一个包装器和其他虚拟化应用程序的前端。
最初为VirtualBox开发了Vagrant 。
自版本1.1以来,Vagrant团队为其他流行的虚拟化软件和服务器环境扩展了支持,包括AWS,Azure,GCE,Docker,KVM,Proxmox,OpenStack,OpenVZ,VMware等。
我们可以在此处查看支持的提供程序/环境的完整列表。

出于本教程的目的,我将使用Oracle VirtualBox。
因此,安装VirtualBox是简单而Vagrant使用VirtualBox作为默认提供程序。

要在借方中安装VirtualBox,Ubuntu,Linux Mint,请运行以下命令:

$sudo apt install virtualbox

在Red Hat,CentOS,Fedora上安装VirtualBox,运行:

$sudo dnf install virtualbox

要在SUSE/OpenSuse上安装VirtualBox,请运行:

$sudo zypper install virtualbox

验证Vagrant安装

要验证是否已正确安装Vagrant ,只需在终端中的任何选项都运行它:

$vagrant

它将返回Vagrant帮助部分。

Usage: vagrant [options] <command> [<args>]
    -h, --help                       Print this help.
Common commands:
     box             manages boxes: installation, removal, etc.
     cloud           manages everything related to Vagrant Cloud
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     upload          upload to machine via communicator
     validate        validates the Vagrantfile
     version         prints current and latest Vagrant version
     winrm           executes commands on a machine via WinRM
     winrm-config    outputs WinRM configuration to connect to the machine
For help on any individual command run `vagrant COMMAND -h`
Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.
        --[no-]color                 Enable or disable color output
        --machine-readable           Enable machine readable output
    -v, --version                    Display Vagrant version
        --debug                      Enable debug output
        --timestamp                  Enable timestamps on log output
        --debug-timestamp            Enable debug output with timestamps
        --no-tty                     Enable non-interactive output

我们还可以-h或者--help参数来调出帮助部分。

$vagrant --help
[Or]
$vagrant -h

要获取个人命令的帮助,请运行:

$vagrant <COMMAND-NAME> -h

要查看Vagrant版本,请运行:

$vagrant version

它将返回当前安装的版本和最新可用版本。

Installed Version: 2.2.10
Latest Version: 2.2.10

You're running an up-to-date version of Vagrant!

或者,

$vagrant -v
Vagrant 2.2.10

升级Vagrant.

如果我们已使用分发包管理器从官方存储库安装了VAGRANT,则可以简单地通过更新系统升级VAGRANT到较新版本。
例如,在基于Debian的系统上,以下命令将更新vagrant到较新版本:

$sudo apt update
$sudo apt upgrade

在基于RPM的系统上,如RHEL,CentOS,DO:

$sudo yum update

或者,

$sudo dnf update

在SUSE/OpenSUSE上:

$sudo zypper update

如果我们已手动下载并安装了VAGRANT,只需从官方VAGRANT下载页面下载新版本,然后按照上面的"安装VAGRANT"部分中的描述进行安装。

新安装程序将正确覆盖和删除旧文件。
建议在升级过程中没有其他VAGRANT进程运行。

故障排除

如果在同一主机上运行多个虚拟机管理程序,则在启动VM时可能会在下面的错误时遇到错误:

There was an error while executing `VBoxManage`, a CLI used by Vagrant for controlling VirtualBox. The command and stderr is shown below.
Command: ["startvm", <ID of the VM>, "--type", "headless"]
Stderr: VBoxManage: error: VT-x is being used by another hypervisor (VERR_VMX_IN_VMX_ROOT_MODE).
VBoxManage: error: VirtualBox can't operate in VMX root mode. Please disable the KVM kernel extension, recompile your kernel and reboot
(VERR_VMX_IN_VMX_ROOT_MODE)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole

出现此错误是因为当前正在使用另一个虚拟机管理程序,例如KVM。
要解决此错误,我们可能需要临时阻止该虚拟机管理程序的列表。

使用命令查找KVM模块:

$lsmod | grep kvm

如果主机系统是英特尔,则会收到以下输出:

kvm_intel             282624  0
kvm                   663552  1 kvm_intel

如果它是AMD,你会得到它:

kvm_intel             282624  0
kvm                   663552  1 kvm_amd

现在通过命令阻止KVM模块:

$echo 'blacklist kvm-intel' >> /etc/modprobe.d/blacklist.conf

如果CPU是AMD,则在上面的命令中键入"kvm-amd"。

重新启动系统,然后重试运行vagrant。
此时VM将在没有任何问题的情况下开始。