怒Mは思いつきでモノを言う

やったことメモなどなど

VagrantでGo環境を作った手順

Goを勉強しようと環境を準備しようと思いました。
Macにそのままインストールしても良かったのですが、そういえばVagrant触っていなかったので、Vagrantを使った環境にGoをインストールしてみました。 その手順メモになります。

やること

  1. 仮想環境にGoをインストールする
  2. VSCodeでリモート接続してGoを動かせるようにする

環境

Vagrant環境準備

前メモってた手順でほぼOKでした。ただ、Vagrantをバージョンアップしたことによって、プラグインvagrant-vbguestもアップデートする必要があることに気付かず、ちょっとハマりました。 (元記事に追記した)

do-m-gatoru.hatenablog.com

最終的な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

・・・wgetyum 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がインストールされました。

VSCodeVagrant環境に接続してGoを実行する

以下のサイトを参考にしています。これでsshの設定含めて大丈夫かと思います。

Visual Studio Code - "Remote Development" を使って Docker Container on "Vagrant + VirtualBox" のファイルを編集する - Qiita

その他、Goに必要なVSCode拡張機能を追加していきます。 Go | Rech Go language support for Visula Studio Codeをインストールすれば、他に必要なツールは適宜教えてくれるのでインストールしていきます。 これで、ローカルのVSCodeからVagrantで動いている仮想環境にアクセスしてGoを書いて実行できました。

f:id:do_m_gatoru:20200919163115p:plain

おわり

Goのお作法的なところは何も知らないので、今後写経して覚えていきます。 環境作りで手間取ることが多いので、次はDockerで環境を作ってみようと思います。

参考

Go言語のインストール - Qiita

最新版のGo言語をLinuxにインストールする手順

CentOS 7にyumでGoを入れてHello Worldするまで - Qiita

The Go Programming Language

CentOS7 yum updateでエラーが出るとき - Qiita