VagrantでGo環境を作った手順
Goを勉強しようと環境を準備しようと思いました。
自Macにそのままインストールしても良かったのですが、そういえばVagrant触っていなかったので、Vagrantを使った環境にGoをインストールしてみました。
その手順メモになります。
やること
- 仮想環境にGoをインストールする
- VSCodeでリモート接続してGoを動かせるようにする
環境
Vagrant環境準備
前メモってた手順でほぼOKでした。ただ、Vagrantをバージョンアップしたことによって、プラグインのvagrant-vbguest
もアップデートする必要があることに気付かず、ちょっとハマりました。
(元記事に追記した)
最終的なVagrantfileは以下のようになりました。
Vagrant.configure("2") do |config| config.vm.box = "centos/7" config.vm.hostname = "golang" config.vm.synced_folder "/Users/hoge/Vagrant/go", "/home/vagrant/go",create: true, owner: "vagrant", group: "vagrant", mout_options: ['dmode=777','fmode=777'] config.vm.network "forwarded_port", guest: 80, host: 9090 config.vm.network "private_network", ip: "192.168.33.10" end
Goインストール
yum
でインストールをしてみますが、そんなのリポジトリにないよというエラーが。
[vagrant@golang ~]$ sudo yum install golang Failed to set locale, defaulting to C Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: ftp.riken.jp * extras: ftp.riken.jp * updates: ftp.riken.jp No package golang available. Error: Nothing to do
ftp.riken.jp
にはGoのPackageが存在しないようで、epel-release
リポジトリには含まれているようなのでyum install epel-release
します。
Goが含まれているかを確認します。
[vagrant@golang ~]$ yum info golang Loaded plugins: fastestmirror Determining fastest mirrors epel/x86_64/metalink | 5.3 kB 00:00 * base: ftp.riken.jp * epel: nrt.edge.kernel.org * extras: ftp.riken.jp * updates: ftp.riken.jp epel | 4.7 kB 00:00 (1/3): epel/x86_64/group_gz | 95 kB 00:00 (2/3): epel/x86_64/updateinfo | 1.0 MB 00:00 (3/3): epel/x86_64/primary_db | 6.9 MB 00:03 Available Packages Name : golang Arch : x86_64 Version : 1.13.14 Release : 1.el7 Size : 3.2 M Repo : epel/x86_64 Summary : The Go Programming Language URL : http://golang.org/ License : BSD and Public Domain Description : The Go Programming Language.
GoのPackageが含まれていることが確認できました。しかしバージョンが1.13.14
。この時の最新バージョンは1.15.1
でしたので、最新版を入れることにします。
Go公式からダウンロードします。
[vagrant@golang ~]$ wget https://golang.org/dl/go1.15.1.linux-amd64.tar.gz -bash: wget: command not found
・・・wgetをyum install wget
してやり直し、改めてGoをインストールします。
[vagrant@golang ~]$ wget https://golang.org/dl/go1.15.1.linux-amd64.tar.gz [vagrant@golang ~]$ sudo tar -C /usr/local -xzf go1.15.1.linux-amd64.tar.gz [vagrant@golang ~]$ ls /usr/local bin etc games go include lib lib64 libexec sbin share src
Goのためのクラスパスを通します。
(.bash_profile) .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs GOROOT=/usr/local/go GOPATH=$HOME/go PATH=$PATH:$HOME/.local/bin:$HOME/bin:$GOROOT/bin:$GOPATH/bin export=GOROOT export=GOPATH export PATH
通ったかの確認をします。
[vagrant@golang ~]$ go version go version go1.15.1 linux/amd64
ツールをインストールしてみる
gore
というgolangのREPLをインストールしてみます。
[vagrant@golang ~]$ go get github.com/motemen/gore/cmd/gore go: missing Git command. See https://golang.org/s/gogetcmd package github.com/motemen/gore/cmd/gore: exec: "git": executable file not found in $PATH
・・・git
をインストールします。
yumリポジトリにあるgit
のバージョンが1.8.3
とかで古すぎです。
iushttps://repo.ius.io/ius-release-el7.rpm'をインストールして、このリポジトリから
git`をインストールしました。
[vagrant@golang ~]$ sudo yum install git224 --enablerepo=ius
改めて、gore
をインストールします。
[vagrant@golang ~]$ go get github.com/motemen/gore/cmd/gore [vagrant@golang ~]$ ls $GOPATH/bin gore [vagrant@golang ~]$ gore --autoimport gore version 0.5.0 :help for help gore> fmt.Println("Hello World") Hello World 12 nil
gore
がインストールされました。
VSCodeでVagrant環境に接続してGoを実行する
以下のサイトを参考にしています。これでsshの設定含めて大丈夫かと思います。
その他、Goに必要なVSCodeの拡張機能を追加していきます。
Go | Rech Go language support for Visula Studio Code
をインストールすれば、他に必要なツールは適宜教えてくれるのでインストールしていきます。
これで、ローカルのVSCodeからVagrantで動いている仮想環境にアクセスしてGoを書いて実行できました。
おわり
Goのお作法的なところは何も知らないので、今後写経して覚えていきます。 環境作りで手間取ることが多いので、次はDockerで環境を作ってみようと思います。