LoginSignup
11
11

More than 5 years have passed since last update.

【サルが書く】macos上のvagrantでcentos7.1の環境でリポジトリからgit cloneしてくるまでをやりおる

Last updated at Posted at 2018-01-30

vagrant でできること

誤解を恐れずに簡単に言うと、仮想的に環境をさわれるもう一つのPC作っちゃおう
と認識しています。
お使いのPCのローカル環境で開発をしようとすると、いろいろなパッケージを入れないといけないですよね。
複数のアプリケーションを開発しようとすると、phpのバージョンが違ったり、ミドルウェアが違ったりと環境を変えるのがめんどくさいですね〜。
そこで、仮想的にPCを作ることで、実際のローカル環境を汚さずに開発をすることができます。

これは便利!!

Vagrant を使う準備をしよう

Vagrant を使用するためにはOracle VM VirtualBoxをインストールする必要があります。

Oracle VM VirtualBoxをインストール

http://www.oracle.com/technetwork/server-storage/virtualbox/downloads/index.html?ssSourceSiteId=otnjp
※自分の環境に合わせたものをダウンロードしてください。

vagrant をインストール

https://www.vagrantup.com/downloads.html
※自分の環境に合わせたものをダウンロードしてください。

インストールされていることを確認

$ vagrant -v                                                                                                

Vagrant 1.9.1

Vagrantでの設定ファイルを作るためのディレクトリをつくる

# $ cd をしてHOMEにいる状態で
$ mkdir vagrant
$ cd vagrant

OSのイメージを取得する

ここから自分の入れたいOSイメージを選んでいきましょう。
今回入れたいのはcentos7.1なのでbento/centos-7.1を選択しました。
centos/7というOSイメージがありますが、今回はbento/centos-7.1選択しました。

bentoとは?

Bentoは、Vagrantベースボックスを構築するためのPackerテンプレートをカプセル化するプロジェクトです。テンプレートのサブセットが作成され、Vagrant Cloudのbento組織に公開されます。箱はまた、キッチンのためのデフォルトボックスとして機能します。
Packerについてはこちらを見てください。

bento/centos-7.1をインストール


$ vagrant box add bento/centos-7.1     

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.tos-7.1              

==> box: Loading metadata for box 'bento/centos-7.1'
    box: URL: https://atlas.hashicorp.com/bento/centos-7.1
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) parallels
2) virtualbox
3) vmware_desktop

Enter your choice: 2 //virtualbox用なので2を選択

イメージが入っているか確認


$ vagrant box list 
bento/centos-7.1 (virtualbox, 2.2.2)

さきほどのイメージを使用し、vagrant初期化


$ vagrant init bento/centos-7.1              

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.

この時点で今いるディレクトリにVagrantfileがあることを確認しましょう。

ココまで来たら、Vagrantfileのあるディレクトリで仮想マシンを起動します。


$ vagrant up

これで仮想マシンに接続できるようになるのでsshで接続しましょう


$ vagrant ssh

vagrantの環境設定をして、gitをインストール

これからはvagrant内でのコマンドです


# gitを導入する時に必要なパッケージをインストール
$ sudo yum -y install gcc curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker

# wget でgitの最新版のソースをダウンロードしてきます。
$ sudo wget https://www.kernel.org/pub/software/scm/git/git-2.16.1.tar.gz
$ sudo tar xzvf git-2.16.1.tar.gz
$ cd git-2.16.1

最新バージョンはコチラで確認してください。


# gitをmakeコマンドでインストール
$ sudo make prefix=/usr/local all
$ sudo make prefix=/usr/local install

# gitがインストールされているかを確認
$ git --version
$ git version 2.16.1

# 後はcloneしてきたいリポジトリのURLでcloneしてくる
$ git clone https://hogehogehogehoge.git

gitの依存関係を解決するところが少しややこしいですが、エラーが起きたら依存関係を疑ってみるといいかもしれません。また、このOSイメージだと最初からgitはインストールされているので、古いバージョンでもいいならそのままでも使用できますね。

11
11
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
11
11