当前位置: 首页 > article >正文

Vagrant+VMWare 安装Ubuntu24.04

背景介绍

对于众多 Windows 用户来说, 有时候需要用到 Linux 环境做一些开发或者测试. WSL 目前能覆盖到很大一部分使用场景, 但是仍然有一些场景需要用虚拟机才能解决.

开发者的痛点往往是对于虚拟机环境的配置和管理, 因为手动安装需要很长的时间, 并且每次安装完成之后需要重新配置, 所以需要一个自动化的工具来完成这些工作.

Vagrant 就是一个很好的工具, 它可以自动安装虚拟机, 并且可以自动配置虚拟机, 与 Docker 类似, 它支持将 Vagrantfile 作为配置文件, 并且可以制作一个镜像文件用来共享或者备份, 所以我们可以轻松的完成虚拟机的安装和配置工作.

在 Windows 上安装虚拟机时 VMware 是一个不错的选择, 因为它的性能相比起 VirtualBox 要好很多, 现在 VMWare Workstation Pro 已经对个人用户免费了, 这更是一个利好.

本文将介绍如何使用 Vagrant+VMWare 安装配置 Ubuntu 24.04.

我将分安装和使用两个章节, 方便读者分阶段了解.

演示环境:

  • Vagrant 2.4.3
    • Vagrant VMware Utility 1.0.23
  • VMWare Workstation 17 Pro(17.6.3 build-24583834)
  • Ubuntu 24.04

1. 安装 Vagrant

读者可以选择使用安装包或者用命令行安装:

  1. 下载安装

  2. 打开 Power Shell, 使用 winget 安装

    winget install --id Hashicorp.Vagrant
    

1.1 安装 Vagrant VMware Utility

这个插件是允许 Vagrant 使用 VMware Workstation Pro 作为其虚拟化后端的必要插件.
下载安装

2. 安装 VMWare Workstation 17 Pro

VMWare 目前的下载页面需要注册才能 下载安装. 如果你本地有老版本的可以选择升级.

3. 创建 Ubuntu 24.04 虚拟机

接下来我们使用 vagrant 创建一个 ubuntu 24.04 的虚拟机.

  1. 创建 Vagrantfile, 这个文件是用来配置虚拟机的, 可以参考 官方文档
    打开 Power Shell, 输入:

    vagrant init bento/ubuntu-24.04 --box-version 202502.21.0
    

    输出:

    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.
    
  2. 启动虚拟机

    vagrant up
    

    接下来就是等待虚拟机启动了, 如果是第一次运行, Vagrant 需要下载对应的镜像(Box), 启动需要一些时间.

    输出

    Bringing machine 'default' up with 'vmware_desktop' provider...
    ==> default: Box 'bento/ubuntu-24.04' could not be found. Attempting to find and install...
        default: Box Provider: vmware_desktop, vmware_fusion, vmware_workstation
        default: Box Version: 202502.21.0
    ==> default: Loading metadata for box 'bento/ubuntu-24.04'
        default: URL: https://vagrantcloud.com/api/v2/vagrant/bento/ubuntu-24.04
    ==> default: Adding box 'bento/ubuntu-24.04' (v202502.21.0) for provider: vmware_desktop (amd64)
        default: Downloading: https://vagrantcloud.com/bento/boxes/ubuntu-24.04/versions/202502.21.0/providers/vmware_desktop/amd64/vagrant.box
        default:
    ==> default: Successfully added box 'bento/ubuntu-24.04' (v202502.21.0) for 'vmware_desktop (amd64)'!
    ==> default: Cloning VMware VM: 'bento/ubuntu-24.04'. This can take some time...
    ==> default: Checking if box 'bento/ubuntu-24.04' version '202502.21.0' is up to date...
    ==> default: Verifying vmnet devices are healthy...
    ==> default: Preparing network adapters...
    ==> default: Starting the VMware VM...
    ==> default: Waiting for the VM to receive an address...
    ==> default: Forwarding ports...
        default: -- 22 => 2222
    ==> default: Waiting for machine to boot. This may take a few minutes...
        default: SSH address: 127.0.0.1:2222
        default: SSH username: vagrant
        default: SSH auth method: private key
        default:
        default: Vagrant insecure key detected. Vagrant will automatically replace
        default: this with a newly generated keypair for better security.
        default:
        default: Inserting generated public key within guest...
    ==> default: Machine booted and ready!
    ==> default: Configuring network adapters within the VM...
    ==> default: Waiting for HGFS to become available...
    ==> default: Enabling and configuring shared folders...
        default: -- C:/Users/xuron/Documents/vagrant-tutorial: /vagrant
    
  3. 连接到虚拟机

    vagrant ssh
    

    输出

    Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 6.8.0-53-generic x86_64)
    
    * Documentation:  https://help.ubuntu.com
    * Management:     https://landscape.canonical.com
    * Support:        https://ubuntu.com/pro
    
    System information as of Sun Mar 16 04:47:44 AM UTC 2025
    
      System load:  0.04               Processes:             203
      Usage of /:   13.9% of 30.34GB   Users logged in:       0
      Memory usage: 7%                 IPv4 address for eth0: 192.168.146.128
      Swap usage:   0%
    
    
    This system is built by the Bento project by Chef Software
    More information can be found at https://github.com/chef/bento
    
    Use of this system is acceptance of the OS vendor EULA and License Agreements.
    vagrant@vagrant:~$
    

配置 Vagrantfile

在 Vagrantfile 中配置初始化命令, 可以通过config.vm.provision来实现. Vagrant 支持多种类型的 provisioning(供应), 包括 shell, Ansible, Chef 和 Puppet 等. 这里我们以最常用的 Shell 脚本为例, 介绍如何配置.

使用 Shell 脚本进行初始化

你可以直接在 Vagrantfile 中嵌入 Shell 脚本, 或者指定一个外部的 Shell 脚本文件路径. 下面是一个简单的例子, 演示了如何使用内联的 Shell 脚本来执行一些初始化命令:

Vagrant.configure("2") do |config|
  # ...
  # 使用内联的Shell脚本
  config.vm.provision "shell", inline: <<-SHELL
    echo '开始初始化...'
    sudo apt-get update
    sudo apt-get install -y nginx
    sudo systemctl start nginx
    sudo systemctl enable nginx
    echo '初始化完成!'
  SHELL
end

在这个例子中, 我们首先更新了包列表, 然后安装并启动了 Nginx 服务. 当 Vagrant 启动虚拟机时, 它会自动运行这些命令.

使用外部脚本

如果你希望将初始化命令放在一个单独的脚本文件中, 可以这样做:

  1. 创建一个 shell 脚本文件, 例如bootstrap.sh.
  2. 在 Vagrantfile 中引用这个脚本:
Vagrant.configure("2") do |config|
  # ...

  # 使用外部脚本
  config.vm.provision "shell", path: "bootstrap.sh"
end

这样做的好处是, 你的 Vagrantfile 保持简洁, 而所有的初始化逻辑都封装在了外部脚本中.

通过这种方式, 你可以轻松地为你的 Vagrant 环境添加任何初始化命令. 无论是安装软件, 修改配置还是其他任何你需要的操作, 都可以通过这种方式实现自动化.

配置端口转发

可以通过 config.vm.network 方法在 Vagrantfile 中指定需要转发的端口. 比如在虚拟机中开启了 Nginx 或者其他 Web 服务, 地址为 127.0.0.1:80, 要在客户机访问, 可以使用端口转发功能. 下面的例子就展示了在主机上通过127.0.0.1:8080访问虚拟机中的127.0.0.1:80:

config.vm.network "forwarded_port", guest: 80, host: 8080

配置共享文件夹

在 Vagrant 中配置共享文件夹允许你在主机(你的电脑)和虚拟机之间共享目录. 这对于开发环境尤其有用, 因为它允许你使用主机上的编辑器或其他工具来修改虚拟机内的文件. 共享文件夹的配置是通过 config.vm.synced_folder 指令完成的.

Vagrant.configure("2") do |config|
  # ...

  # 配置共享文件夹
  config.vm.synced_folder "主机路径", "虚拟机路径"
end

http://www.kler.cn/a/596841.html

相关文章:

  • 烧结银技术赋能新能源汽车超级快充与高效驱动
  • C# 调用 VITS,推理模型 将文字转wav音频调试 -数字人分支
  • 电力和冷却管理:如何让数据中心“高效降温”同时节能增效
  • 第三十一篇 数据仓库(DW)与商业智能(BI)架构设计与实践指南
  • 3.22日竞蓝全扫盘
  • #include <hello.h> 与 #include “hello.h“的区别
  • RDMA栈架构
  • 并发和并行、同步和异步、进程和线程的关系
  • RK3568开发笔记-egtouch触摸屏ubuntu系统屏幕校准
  • postgresql 对 lz4 压缩算法的支持
  • 2维压缩感知SL0重构实现算法
  • DFS刷题
  • 树莓派5的供电与启动
  • 强大的AI网站推荐(第三集)—— AskO3
  • MyBatis-Plus 的加载及初始化
  • FastAPI WebSocket 无法获取真实 IP 错误记录
  • 蓝桥杯 劲舞团
  • Springboot之RequestAttributes学习笔记
  • 【深度学习入门_机器学习理论】梯度提升决策树(GBDT)
  • 【算法学习之路】13.BFS