LoginSignup
2
4

More than 1 year has passed since last update.

【Ubuntu 20.04】VirtualBox+Vagrantを使用したインストール

Last updated at Posted at 2022-01-23

目的

仮想ソフトウェア(VirtualBox)を使用して、Linux ゲスト OS(Ubuntu)を導入します。
仮想ソフトウェアの環境、およびゲストOSのインストール手順は、以下をベースに行います。

必要なツールおよびソフトウェア

作業ステップ

  • ステップ①:vagrant box フィアルの検索
  • ステップ②:Ubuntu Linux ゲストOSのインストールと初期設定

ステップ①:vagrant box ファイルの検索

  1. Vagrant Cloud のサイト( https://app.vagrantup.com/boxes/search )から「Ubuntu 20.04」のvagrant box ファイルを検索します。
  2. 検索結果から、vagrant box ファイルのURLを取得します。

ステップ②:Ubuntu Linux ゲストOSのインストールと初期設定

Ubuntu vagrant box ファイルのダウンロードとインストール

「vagrant box add」コマンドを実行して、Ubuntu の vagrant box ファイルのダウンロードとローカルファイルシステムへのインストールを行います。

  • 第1引数:vagrant box イメージの登録名 ⇒ "ubuntu-20.04"
  • 第2引数:vagrant box ファイルのURL ⇒ ステップ①で取得した値
$ vagrant box add ubuntu-20.04 https://app.vagrantup.com/ubuntu/boxes/focal64/versions/20211026.0.0/providers/virtualbox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu-20.04' (v0) for provider:
    box: Downloading: https://app.vagrantup.com/ubuntu/boxes/focal64/versions/20211026.0.0/providers/virtualbox.box
Download redirected to host: cloud-images.ubuntu.com
    box:
==> box: Successfully added box 'ubuntu-20.04' (v0) for 'virtualbox'!

「vagrant box list」コマンドで、インストールされたことを確認できます。

$ vagrant box list
・・・
ubuntu-20.04                 (virtualbox, 0)

Ubuntu ゲスト OS のインストール

「vagrant init」コマンドを実行して、ゲストOS用の Vagrantfile を作成します。
ここでは、インストールする作業ディレクトリを"ubuntu2004"にしています。

$ mkdir ubuntu2004
$ cd ubuntu2004/
$ vagrant init ubuntu-20.04
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.

仮想マシンとゲスト OS の起動

作業ディレクトリで「vagrant up」コマンドを実行し、Ubuntu ゲスト OS を起動します。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu-20.04'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntu2004_default_1642920054750_69272
==> 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: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> 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: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    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: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
 (中略)
==> default: Mounting shared folders...
    default: /vagrant => D:/Vagrant/ubuntu2004

ゲスト OS へのログイン以降の作業

ベースとした記事と同じ作業を実施します。
ただ、jq コマンドのインストール方法が、snap コマンドに変わっていたので、以下に記載します。

  • jq コマンドのインストール
~$ snap find jq
Name                           Version     Publisher     Notes    Summary
jq                             1.5+dfsg-1  mvo           -        The jq command
jq-core18                      1.5+dfsg-1  canonical✓    -        The jq command
jq-cprov                       1.5         cprov         classic  jq is a lightweight and flexible command-line JSON processor.
jq-ltrager                     1.5         ltrager       -        jq is a lightweight and flexible command-line JSON processor.
jq-core20                      1.6-1       mvo           -        The jq command
exploding-kittens-in-electron  1.3.0       nerbler09     -        Fan port of card game, Exploding Kittens to the desktop
oq                             1.3.2       blacksmoke16  -        A performant, and portable jq wrapper to support formats other than JSON
yq                             4.16.2      mikefarah     -        A lightweight and portable command-line YAML processor
~$
~$ sudo snap install jq
jq 1.5+dfsg-1 from Michael Vogt (mvo) installed
~$
~$ jq --help
jq - commandline JSON processor [version 1.5-1-a5b5cbe]
Usage: /snap/jq/6/usr/bin/jq [options] <jq filter> [file...]

        jq is a tool for processing JSON inputs, applying the
        given filter to its JSON text inputs and producing the
        filter's results as JSON on standard output.
        The simplest filter is ., which is the identity filter,
        copying jq's input to its output unmodified (except for
        formatting).
        For more advanced filters see the jq(1) manpage ("man jq")
        and/or https://stedolan.github.io/jq

        Some of the options include:
         -c             compact instead of pretty-printed output;
         -n             use `null` as the single input value;
         -e             set the exit status code based on the output;
         -s             read (slurp) all inputs into an array; apply filter to it;
         -r             output raw strings, not JSON texts;
         -R             read raw strings, not JSON texts;
         -C             colorize JSON;
         -M             monochrome (don't colorize JSON);
         -S             sort keys of objects on output;
         --tab  use tabs for indentation;
         --arg a v      set variable $a to value <v>;
         --argjson a v  set variable $a to JSON value <v>;
         --slurpfile a f        set variable $a to an array of JSON texts read from <f>;
        See the manpage for more options.
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4