僕の世界観を変えてみる

文系男子が趣味でプログラミングを勉強していくブログです。他にも日常で起きたどうでもいいことや愚痴を書いていきたいです。座右の銘は和を以て貴しとなすです。仲良くやろうよ。

vagrantインストール

windowsやmacOSが起動している状態で仮想的に他のOSを起動させることができるソフトがあります。

たとえばwindows10がインストールされたパソコン(ホストOS)上で仮想化ソフトを起動させLinuxOS(ゲストOS)を使うことができる、といったものです。

いわゆるエミュレータです。

代表的な仮想化ソフトとしてVirtualBox、VMwareが挙げられます。

何に使うのかというと、怪しいソフトを起動させるときなどに使用したり、ウイルスっぽいものを実行したり、

だいたいプログラムを実行させるときに使ったりします。

仮想マシン内であれば、ファイルが削除されたりOSが破壊されても痛手を負わなくて済みます。

また、ホストOSを汚すことなくさまざまなソフトをインストールしたり、実行したりできます。

最初はVirtualBoxなどを起動させるのでもいいのですが、徐々にマウスでの操作が億劫になってくる時期がきます。

VirtualBoxを起動させて、新規→簡単な設定→OS読み込み→初期設定→アップデート→etc...

そう、だるいんです。

Vagrant(ベイグラント)とは仮想環境の設定・構築をいくつかのコマンドで自動的にしてくれるとっても便利なソフトです。

これを使えばたとえ環境がぶっこわれてもすぐに元に戻すことができます。

またエンジニア界隈では開発環境と本番環境の誤差をなくすために仮想環境を使用したりします。

そのため、VagrantやDocker(後述するかもしれないししないかもしれない)といった環境構築を自動化させるソフトの操作は必須と言えます。

Vagrantインストール

※コマンドはすべてpowershellで入力しています。コマンドプロンプトでもできますがコマンドが異なる場合があるので注意してください。

https://www.vagrantup.com/downloads.htmlここからVagrantをダウンロードします。 64bit版をインストールしました。

次へと進んで行くだけです。

次にhttps://www.virtualbox.org/wiki/DownloadsここからVirtualBoxをダウンロードします。

windows hostsからexeをダウンロードし、インストールです。

> vagrant -v

上記のコマンドを実行してバージョンが表示されたら成功です。

Vagrant CloudからOSを取得しインストールする

Vagrant Cloud https://app.vagrantup.com/boxes/searchのページからUbuntuやCentOSなど好きなOSをインストールすることができます。

今回はubuntu/trusty64をインストールしていきたいと思います。

ubuntu/trusty64と書かれた部分をクリックします。

ページが切り替わったらあと、NewタブをクリックするとUbuntuをインストールするためのコマンドが表示されます。

f:id:htmllifehack:20200709001052p:plain
vagrant

Vagrant用のフォルダを作成して、その中で実行します。

$ mkdir Vagrant
$ cd Vagrant
$ vagrant init ubuntu/trusty64

初回はubuntuのインストールで時間がかかりますが簡単に環境構築ができます。

> 結果
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.

と出力されたら完了です。

initとはinitializeの略で初期化するという意味なります。pythonなどのプログラミング言語でも登場する言葉です。

vagrant init でエラーがでたとき

今のVagrantはinitするだけで設定が終わりますが、以前まではvagrant box addというコマンドを使用する必要があったそうです。

が、Windows10でVagrant initをした際は特にエラーもでず vagrant upすることができましたが、MacでVagrant initした際はエラーがでました。

エラーの内容は忘れましたがVagrant cloudからBoxを追加してくれ的な言い回しだったと思います。

なのでもしもvagrant init Box名でうまく行かなかったときは

$ vagrant box add https://vagrantcloud.com/ubuntu/trusty64

としてください。

そのあとにvagrant init ubuntu/trusty64を実行するとうまく行くと思います。

vagrant 起動

初期化ができたのでVagrantを起動させてみます。

$ vagrant up

> 結果
 flag to force provisioning. Provisioners marked to run always will still run.

こんな感じの文言がでてきたら起動されています。

そして気が付けばvagrant initしたフォルダ内にVagrantfileが作成されているはずです。

次はこのVagrantfileにVagrantの設定を記述していきます。

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://vagrantcloud.com/search.
  config.vm.box = "ubuntu/trusty64"

  # 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.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # 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 "./workspace", "/home/vagrant/workspace"

  # 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.

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

Vagrantfileを開くとこんな感じになっていると思います。

いくつかコメントアウトを外してください。

たとえばconfig.vm.network "private_network", ip: "192.168.33.10"はVagrant上でネットワークに接続するさいに必要な設定です。

先頭の#を消してコードを有効にします。

ホストOSとゲストOSでファイル共有する

ホストOSとゲストOS間でファイル共有ができないと不便です。

VirutualBoxをGUI操作した方ならわかると思いますが、仮想化ソフトにはファイル共有機能が備わっています。

VagrantではVagrantfileに共有したいファイル名を記述し、Vagrantのプラグインをインストールすることでファイル共有ができます。

Vagrantfile内の ‘config.vm.synced_folder "../data", "/vagrant_data"`の部分が共有するファイルの記述です。

.../dataがホストOS、/vagrant_dataがゲストOS側です。

僕はC:\users\owner\document\vagrantvagrantfileが形成されているのでvagrantfileと同じ階層にworkspaceという名前のフォルダを作成しました。

なので../dataの部分を./workspaceに書き換えます。

次にVagrant側ですが、まずはVagrantに接続してフォルダを作成しましょう。

Vagrantの接続はvagrant sshで行います。

$ vagrant ssh

vagrant@vagrant-ubuntu-trusty-64:~$に切り替われば接続OKです。

Vagrantに接続後、僕はルートディレクトリにworkspaceという名前のフォルダを作成しました。

vagrant@vagrant-ubuntu-trusty-64:~$ mkdir workspace

なので、/vagrant_data/home/vagrant/workspaceに書き換えます。

これだけではファイル共有されないのでプラグインのインストールを行います。

Vagrantから抜け出すにはexitと入力します。

vagrant@vagrant-ubuntu-trusty-64:~$ exit

これでpowershellに戻ってきました。

powershellで下記のコマンドを実行します。

$ vagrant plugin install vagrant-vbguest

> 結果
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Fetching micromachine-3.0.0.gem
Fetching vagrant-vbguest-0.24.0.gem
Installed the plugin 'vagrant-vbguest (0.24.0)'!

インストールが終わったらVagrantを再起動します。

Vagrantの再起動にはvagrant reloadコマンドを使います。

$ vagrant reload

> 結果
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/owner/Documents/vagrant
    default: /home/vagrant/workspace => C:/Users/owner/Documents/vagrant/workspace

再起動させると最後のほうに上記のような文言が表示されていると思います。

これでファイル共有の設定ができたはずです。

ためしにvagrantに接続して適当なファイルを作成してみます。

vagrant@vagrant-ubuntu-trusty-64:~$ touch test.txt

Vagrant側でtest.txtというテキストファイルを作成しました。

ホスト側のエクスプローラーでC:\users\owner\document\vagrant\workspaceにアクセスするとtest.txt`の存在が確認できました。

これでファイル共有の設定は終了です。

Vagrantで使うコマンド

Vagrant環境を作るための初期化

$ vagrant init {Box名}

Vagrant起動

$ vagrant up

Vagrantから抜け出す

vagrant@vagrant-ubuntu-trusty-64:~$ exit

Vagrant停止

$ vagrant halt

Vagrant再起動

$ vagrant reload

Vagrant設定の削除

$ vagrant destroy

まとめ

Vagrant上でGo言語をインストールしようと試みたところCould not resolve 'archive.ubuntu.com'というエラーが表示されました。

エラーの最後にUnable to fetch some archives, maybe run apt-get update or try with --fix-missing?と表示されていたのでsudo apt-get updateをかけたのですが、同じようにCould not resolve 'archive.ubuntu.com'と出ました。

むむ、とググってみたところパッケージの初期化や仮想マシンのDNSを変更するといった記事をみつけたので、Vagrantfileを開いてみたらIPアドレスがコメントアウトされていたのに気づきました。

まあ、意外と簡単なところでつまずくんですよね笑

Goも無事インストールできたのでさっそくさわってみます!

そして学習が進んで行ったらVagrantにapacheを入れてサーバを立てようと思います!

それではごきげんよう。

参考にさせていただいた記事一覧

Vagrant ファイル共有 決定版 - Qiita

【まとめ】Vagrant コマンド一覧 - Qiita

・いちばん参考にしていたQiitaの記事を見失いました...