LoginSignup
4
2

More than 5 years have passed since last update.

VagrantとVirtual Boxを使って、Mac内にローカル開発環境ubuntu16.04を作る & Goの最新版のインストール

Last updated at Posted at 2018-06-19

概要

  • Macで、Virtual BoxとVagrantを使って、仮想環境を構築します
  • ubuntu16.04にGoの最新版をインストールします

動作環境

Vagrant Version: 2.1.1
Virtual box version 5.2.12

Vagrantfileの生成とssh接続

好きな作業用ディレクトリに移動します。
以下のサイトから、最新のubuntuのboxを取得し、Vagrantfileを生成します。
https://app.vagrantup.com/ubuntu/

フォルダ内にVagrantfileが生成されたのを確認し、今回は、IPの割り当てなどは気にせずにそのまま立ち上げます。

~/MyVagrant/MyProject
$ vagrant init ubuntu/xenial64 \ --box-version 20180615.0.0
$ vagrant up

ここで、他のVagrantによるVirtual Boxが起動していた場合、以下のようなエラーが出る場合がありますので、その時は、次のコマンドで再起動すると良いです。

~/MyVagrant/MyProject
$ vagrant up
...
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 2222 is already in use
on the host machine.

To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 22, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.
~/MyVagrant/MyProject
$ vagrant reload

Vagrantがうまく起動した場合、sshでvagrant内のubuntuの環境にアクセスします。

~/MyVagrant/MyProject
$ vagrant ssh
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-128-generic x86_64)
...略...

無事ubuntu環境の構築ができました。
AWSを使ってサーバーを立てるよりも、無料&手軽に開発環境を構築できるので、VagrantとVirtual Boxの組み合わせはかなりおすすめです。

ubuntu16.04にGoをインストールする

まず、全てのアプリケーションをアップデートします。

~/
$ sudo apt-get update

以下のGo公式ホームページから、Linux用のGoをダウンロードします。
https://golang.org/dl/

~/
$ wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz

フォルダを解凍して、/usr/local下に置きます。

~/
$ sudo tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz

この時、/usr/local/goが、$GOROOTにデフォルトで設定されます。

Goの作業用ディレクトリを$HOME下に作り、PATHを通します。

~/
$ mkdir gocode
$ vi ~/.profile

~/.profile内に、以下のPATHを記述します。

~/.profile
export PATH="$PATH:/usr/local/go/bin"
export GOPATH="$HOME/gocode"
export PATH="$PATH:$GOPATH/bin"

記述後、以下のコマンドで設定を更新します。

~/
$ source ~/.profile

うまくPATHを設定できたか確認します。

~/
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/vagrant/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/vagrant/gocode"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build287593741=/tmp/go-build -gno-record-gcc-switches"

以上で、GOPATHとGOROOTの設定がいまく言っていることがわかると思います。
GOPATHはGo関連のアプリケーションをインストールする場所で、GOPATHは作業用ディレクトリを示す場所になります。

4
2
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
4
2