LoginSignup
8
4

More than 3 years have passed since last update.

go 言語のインストール・アンインストール on windows, WSL

Last updated at Posted at 2019-10-15

概要

windows, WSL 上に go 言語を導入するときの設定とかの話

準備

WSL インストール

手順

公式のインストール手順に従います

windows MSI (インストーラー)利用

インストーラーダウンロード
「Microsoft Windows」の欄にあるものをダウンロード。古いバージョンがいい場合はページ下にある中から選ぶ。
image.png

ダウンロード中
image.png

インストール
image.png

go のインストールパス(いわゆる GOROOT)を決められる
image.png

インストールしまっせ確認 & 完了
image.png
image.png

インストール確認

C:\Users\sampleuser>go env GOROOT
c:\go

C:\Users\sampleuser>go env GOPATH
C:\Users\sampleuser\go

# 環境変数 に入ってるのは GOPATH のみ(GOROOT は設定されない)
C:\Users\sampleuser>set | findstr -i "GOPATH"
GOPATH=C:\Users\sampleuser\go

C:\Users\sampleuser>set | findstr -i "GOROOT"

# PATH に $GOPATH/bin が追加される
C:\Users\sampleuser>set | findstr -i "PATH"
GOPATH=C:\Users\sampleuser\go

# PATH の最後に追加されている
Path=C:\Users\sampleuser\go\bin

GOPATH ってなに?

外部パッケージがインストールされるディレクトリ。外部パッケージのインストールは go get コマンド。

$GOPATH/bin ってなに?

外部パッケージの実行形式のバイナリが置かれるディレクトリ。
つまりここに PATH を通しておけば、外部パッケージのバイナリが実行できるようになる、という寸法。

実際に外部ライブラリをインストールしてみる

# -u はパッケージとその依存パッケージをアップデートする
C:\Users\sampleuser>go get -u github.com/motemen/gore/cmd/gore
go: finding github.com/motemen/gore v0.4.1
go: downloading github.com/motemen/gore v0.4.1
go: extracting github.com/motemen/gore v0.4.1
go: finding golang.org/x/text v0.3.2
go: finding github.com/motemen/go-quickfix latest
go: finding github.com/peterh/liner v1.1.0
go: finding golang.org/x/tools latest
go: downloading golang.org/x/text v0.3.2
go: finding github.com/mitchellh/go-homedir v1.1.0
go: downloading github.com/mitchellh/go-homedir v1.1.0
go: extracting github.com/mitchellh/go-homedir v1.1.0
go: downloading github.com/peterh/liner v1.1.0
go: extracting github.com/peterh/liner v1.1.0
go: downloading golang.org/x/tools v0.0.0-20191014205221-18e3458ac98b
go: downloading github.com/motemen/go-quickfix v0.0.0-20160413151302-5c522febc679
go: extracting golang.org/x/text v0.3.2
go: extracting github.com/motemen/go-quickfix v0.0.0-20160413151302-5c522febc679
go: extracting golang.org/x/tools v0.0.0-20191014205221-18e3458ac98b
go: downloading github.com/mattn/go-runewidth v0.0.3
go: extracting github.com/mattn/go-runewidth v0.0.3
go: finding github.com/mattn/go-runewidth v0.0.4
go: downloading github.com/mattn/go-runewidth v0.0.4
go: extracting github.com/mattn/go-runewidth v0.0.4

$GOPATH ディレクトリ内部を見ると、bin, pkg 以下にいろいろなファイルが配置されています。
最新の ものでない場合は src というディレクトリが存在する場合もあります。

tree 構造(長いです)。パッケージのバージョンごとに配置されてるのが見て取れます。
C:\Users\sampleuser>tree go
C:\USERS\SAMPLEUSER\GO
├─bin
└─pkg
    ├─mod
    │  ├─cache
    │  │  └─download
    │  │      ├─github.com
    │  │      │  ├─mattn
    │  │      │  │  └─go-runewidth
    │  │      │  │      └─@v
    │  │      │  ├─mitchellh
    │  │      │  │  └─go-homedir
    │  │      │  │      └─@v
    │  │      │  ├─motemen
    │  │      │  │  ├─go-quickfix
    │  │      │  │  │  └─@v
    │  │      │  │  └─gore
    │  │      │  │      └─@v
    │  │      │  └─peterh
    │  │      │      └─liner
    │  │      │          └─@v
    │  │      ├─golang.org
    │  │      │  └─x
    │  │      │      ├─crypto
    │  │      │      │  └─@v
    │  │      │      ├─net
    │  │      │      │  └─@v
    │  │      │      ├─sync
    │  │      │      │  └─@v
    │  │      │      ├─sys
    │  │      │      │  └─@v
    │  │      │      ├─text
    │  │      │      │  └─@v
    │  │      │      ├─tools
    │  │      │      │  └─@v
    │  │      │      └─xerrors
    │  │      │          └─@v
    │  │      └─sumdb
    │  │          └─sum.golang.org
    │  │              ├─lookup
    │  │              │  ├─github.com
    │  │              │  │  ├─mattn
    │  │              │  │  ├─mitchellh
    │  │              │  │  ├─motemen
    │  │              │  │  └─peterh
    │  │              │  └─golang.org
    │  │              │      └─x
    │  │              └─tile
    │  │                  └─8
    │  │                      ├─0
    │  │                      │  └─x001
    │  │                      │      └─230.p
    │  │                      ├─1
    │  │                      │  └─004.p
    │  │                      └─2
    │  │                          └─000.p
    │  ├─github.com
    │  │  ├─mattn
    │  │  │  ├─go-runewidth@v0.0.3
    │  │  │  └─go-runewidth@v0.0.4
    │  │  ├─mitchellh
    │  │  │  └─go-homedir@v1.1.0
    │  │  ├─motemen
    │  │  │  ├─go-quickfix@v0.0.0-20160413151302-5c522febc679
    │  │  │  │  ├─cmd
    │  │  │  │  │  └─goquickfix
    │  │  │  │  └─testdata
    │  │  │  │      ├─general
    │  │  │  │      ├─importname
    │  │  │  │      ├─rangestmt
    │  │  │  │      └─revert
    │  │  │  └─gore@v0.4.1
    │  │  │      ├─cli
    │  │  │      ├─cmd
    │  │  │      │  └─gore
    │  │  │      ├─doc
    │  │  │      └─gocode
    │  │  └─peterh
    │  │      └─liner@v1.1.0
    │  │          └─.github
    │  └─golang.org
    │      └─x
    │          ├─text@v0.3.2
    │          │  ├─cases
    │          │  ├─cmd
    │          │  │  └─gotext
    │          │  │      └─examples
    │          │  │          ├─extract
    │          │  │          │  └─locales
    │          │  │          │      ├─de
    │          │  │          │      ├─en-US
    │          │  │          │      └─zh
    │          │  │          ├─extract_http
    │          │  │          │  ├─locales
    │          │  │          │  │  ├─de
    │          │  │          │  │  ├─en
    │          │  │          │  │  ├─en-US
    │          │  │          │  │  └─zh
    │          │  │          │  └─pkg
    │          │  │          └─rewrite
    │          │  ├─collate
    │          │  │  ├─build
    │          │  │  └─tools
    │          │  │      └─colcmp
    └─sumdb
        └─sum.golang.org

C:\Users\sampleuser\go\bin>dir
2019/10/15  17:01         9,094,656 gore.exe

アンインストール

通常のプログラムのアンインストールと同じです。
image.png

アンインストール確認

C:\Users\sampleuser>go env GOPATH
'go' は、内部コマンドまたは外部コマンド、
操作可能なプログラムまたはバッチ ファイルとして認識されていません。

GOPATH や PATH に追加された環境変数も消されます。

C:\Users\sampleuser>set | findstr -i "GOPATH"

WSL へのインストール

Linux から選択。
image.png

ダウンロード
image.png

/usr/local へ解凍します。

unix_user:~$ sudo tar -C /usr/local -xzf /mnt/c/Users/sampleuser/go1.13.1.linux-amd64.tar.gz
[sudo] password for unix_user:

# 確認
unix_user:~$ ls -la /usr/local/go
total 408
drwxr-xr-x 1 root root  4096 Sep 26 03:53 .
drwxr-xr-x 1 root root  4096 Oct 15 17:57 ..
-rw-r--r-- 1 root root 55389 Sep 26 03:52 AUTHORS
-rw-r--r-- 1 root root  1339 Sep 26 03:52 CONTRIBUTING.md
-rw-r--r-- 1 root root 84339 Sep 26 03:52 CONTRIBUTORS
-rw-r--r-- 1 root root  1479 Sep 26 03:52 LICENSE
-rw-r--r-- 1 root root  1303 Sep 26 03:52 PATENTS
-rw-r--r-- 1 root root  1607 Sep 26 03:52 README.md
-rw-r--r-- 1 root root   397 Sep 26 03:52 SECURITY.md
-rw-r--r-- 1 root root     8 Sep 26 03:53 VERSION
drwxr-xr-x 1 root root  4096 Sep 26 03:53 api
drwxr-xr-x 1 root root  4096 Sep 26 03:55 bin
drwxr-xr-x 1 root root  4096 Sep 26 03:53 doc
-rw-r--r-- 1 root root  5686 Sep 26 03:52 favicon.ico
drwxr-xr-x 1 root root  4096 Sep 26 03:53 lib
drwxr-xr-x 1 root root  4096 Sep 26 03:53 misc
drwxr-xr-x 1 root root  4096 Sep 26 03:56 pkg
-rw-r--r-- 1 root root    26 Sep 26 03:52 robots.txt
drwxr-xr-x 1 root root  4096 Sep 26 03:53 src
drwxr-xr-x 1 root root  4096 Sep 26 03:53 test

PATH の追加

unix_user:~$ cat ~/.profile

# 最後に追記。上の $GOROOT/bin は go 自体の実行のため?
export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:$HOME/go/bin

インストール確認

unix_user:~$ go env GOPATH
/home/unix_user/go

unix_user:~$ go env GOROOT
/usr/local/go

# 環境変数には設定されていない
unix_user:~$ printenv | grep GO
unix_user:~$

実際に外部ライブラリをダウンロードしてみる

unix_user:~$ go get -u github.com/motemen/gore/cmd/gore

unix_user:~$ tree ~/go
/home/unix_user/go
├── bin
│   └── gore
└── src
    ├── github.com
    │   ├── mattn
    │   │   └── go-runewidth
    │   │       ├── LICENSE
    │   │       ├── README.mkd
    │   │       ├── benchmark_test.go
    │   │       ├── go.mod
    │   │       ├── runewidth.go
    │   │       ├── runewidth_appengine.go
    │   │       ├── runewidth_js.go
    │   │       ├── runewidth_posix.go
    │   │       ├── runewidth_posix_test.go
    │   │       ├── runewidth_test.go
    │   │       └── runewidth_windows.go
    │   ├── mitchellh
    │   │   └── go-homedir
    │   │       ├── LICENSE
    │   │       ├── README.md
    │   │       ├── go.mod
    │   │       ├── homedir.go
    │   │       └── homedir_test.go
    │   ├── motemen
    │   │   ├── go-quickfix
    │   │   │   ├── LICENSE
    │   │   │   ├── README.adoc
    │   │   │   ├── cmd
    │   │   │   │   └── goquickfix
    │   │   │   │       └── main.go
    │   │   │   ├── quickfix.go
    │   │   │   ├── quickfix_test.go
    │   │   │   └── testdata
    │   │   │       ├── general
    │   │   │       │   └── general.go
    │   │   │       ├── importname
    │   │   │       │   └── importname.go
    │   │   │       ├── rangestmt
    │   │   │       │   └── rangestmt.go
    │   │   │       └── revert
    │   │   │           └── revert.go
    │   │   └── gore
    │   │       ├── LICENSE
    │   │       ├── Makefile
    │   │       ├── README.md
    │   │       ├── cli
    │   │       │   ├── cli.go
    │   │       │   ├── cli_test.go
    │   │       │   └── run.go
    │   │       ├── cmd
    │   │       │   └── gore
    │   │       │       └── main.go
    │   │       ├── command_name.go
    │   │       ├── command_name_test.go
    │   │       ├── commands.go
    │   │       ├── commands_test.go
    │   │       ├── complete.go
    │   │       ├── complete_test.go
    │   │       ├── debug.go
    │   │       ├── doc
    │   │       │   └── screencast.gif
    │   │       ├── errfilter.go
    │   │       ├── errfilter_test.go
    │   │       ├── go.mod
    │   │       ├── go.sum
    │   │       ├── gocode
    │   │       │   ├── gocode.go
    │   │       │   └── gocode_test.go
    │   │       ├── gore.go
    │   │       ├── liner.go
    │   │       ├── log.go
    │   │       ├── node.go
    │   │       ├── node_test.go
    │   │       ├── nodebug.go
    │   │       ├── option.go
    │   │       ├── quickfix.go
    │   │       ├── session.go
    │   │       ├── session_test.go
    │   │       ├── terminal_unix.go
    │   │       ├── terminal_windows.go
    │   │       ├── utils.go
    │   │       └── wercker.yml
    │   └── peterh
    │       └── liner
    │           ├── COPYING
    │           ├── README.md
    │           ├── bsdinput.go
    │

アンインストール

GOPATH に解凍したファイルたちを削除します。

unix_user:~$ sudo rm -rf /usr/local/go

アンインストール確認

unix_user:~$ go env GOPATH

Command 'go' not found, but can be installed with:

sudo apt install golang-go
sudo apt install gccgo-go

# GOPATH にダウンロードしたライブラリは残っている
unix_user:~$ ls -la ~/go
total 0
drwxrwxrwx 1 unix_user unix_user 4096 Oct 15 18:07 .
drwxr-xr-x 1 unix_user unix_user 4096 Oct 15 18:14 ..
drwxrwxrwx 1 unix_user unix_user 4096 Oct 15 18:07 bin
drwxrwxrwx 1 unix_user unix_user 4096 Oct 15 18:06 src

# ダウンロードしたライブラリとかも手動で消す
unix_user:~$ sudo rm -rf ~/go
unix_user:~$ ls -la ~/go
ls: cannot access '/home/unix_user/go': No such file or directory

apt だとどうなの?

バージョンが古い。。。

unix_user:~$ sudo apt install golang-go
[sudo] password for unix_user:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfreetype6
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  golang-1.10-go golang-1.10-race-detector-runtime golang-1.10-src golang-race-detector-runtime golang-src pkg-config
Suggested packages:
  bzr mercurial subversion
The following NEW packages will be installed:
  golang-1.10-go golang-1.10-race-detector-runtime golang-1.10-src golang-go golang-race-detector-runtime golang-src pkg-config
0 upgraded, 7 newly installed, 0 to remove and 243 not upgraded.
Need to get 40.3 MB of archives.
After this operation, 225 MB of additional disk space will be used.
Do you want to continue? [Y/n] N
Abort.
8
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
8
4