googlecolab
How to run Golang in Google Colab.
- Run the first code cell, using the python runtime.
- Refresh the page to use the Go kernel (gophernotes).
- Test that everything went well by running the Golang cells below.
!add-apt-repository ppa:longsleep/golang-backports -y
!apt update
!apt install golang-go
%env GOPATH=/root/go
!go get -u github.com/gopherdata/gophernotes
!cp ~/go/bin/gophernotes /usr/bin/
!mkdir /usr/local/share/jupyter/kernels/gophernotes
!cp ~/go/src/github.com/gopherdata/gophernotes/kernel/* \
/usr/local/share/jupyter/kernels/gophernotes
Rf.
https://www.ptrace-security.com/blog/how-to-run-golang-in-google-colab/
log
Ign:1 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease
Ign:2 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease
Get:3 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]
Get:4 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release [697 B]
Hit:5 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release
Get:6 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release.gpg [836 B]
Get:7 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:8 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease [15.9 kB]
Hit:9 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:11 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Ign:12 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages
Get:12 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages [740 kB]
Hit:13 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Get:14 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [395 kB]
Get:15 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease [15.9 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:17 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [2,116 kB]
Hit:18 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease
Get:19 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [24.5 kB]
Get:20 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,410 kB]
Get:21 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease [15.4 kB]
Get:22 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2,546 kB]
Get:23 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main Sources [1,756 kB]
Get:24 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [31.4 kB]
Get:25 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [425 kB]
Get:26 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,181 kB]
Get:27 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main amd64 Packages [899 kB]
Get:28 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 Packages [39.5 kB]
Get:29 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 Packages [6,000 B]
Fetched 12.9 MB in 5s (2,652 kB/s)
Reading package lists... Done
Ign:1 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease
Hit:2 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease
Ign:3 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease
Hit:4 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release
Hit:5 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release
Hit:6 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease
Hit:7 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:8 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:10 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Hit:11 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:13 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:14 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Hit:15 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease
Hit:16 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
79 packages can be upgraded. Run 'apt list --upgradable' to see them.
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
golang-1.16-go golang-1.16-src golang-src
Suggested packages:
bzr | brz mercurial subversion
The following NEW packages will be installed:
golang-1.16-go golang-1.16-src golang-go golang-src
0 upgraded, 4 newly installed, 0 to remove and 79 not upgraded.
Need to get 65.7 MB of archives.
After this operation, 377 MB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.16-src amd64 1.16.3-2longsleep1+focal [14.3 MB]
Get:2 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.16-go amd64 1.16.3-2longsleep1+focal [51.4 MB]
Get:3 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-src amd64 2:1.16-1longsleep1+bionic [3,952 B]
Get:4 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-go amd64 2:1.16-1longsleep1+bionic [23.0 kB]
Fetched 65.7 MB in 6s (10.6 MB/s)
Selecting previously unselected package golang-1.16-src.
(Reading database ... 160983 files and directories currently installed.)
Preparing to unpack .../golang-1.16-src_1.16.3-2longsleep1+focal_amd64.deb ...
Unpacking golang-1.16-src (1.16.3-2longsleep1+focal) ...
Selecting previously unselected package golang-1.16-go.
Preparing to unpack .../golang-1.16-go_1.16.3-2longsleep1+focal_amd64.deb ...
Unpacking golang-1.16-go (1.16.3-2longsleep1+focal) ...
Selecting previously unselected package golang-src.
Preparing to unpack .../golang-src_2%3a1.16-1longsleep1+bionic_amd64.deb ...
Unpacking golang-src (2:1.16-1longsleep1+bionic) ...
Selecting previously unselected package golang-go.
Preparing to unpack .../golang-go_2%3a1.16-1longsleep1+bionic_amd64.deb ...
Unpacking golang-go (2:1.16-1longsleep1+bionic) ...
Setting up golang-1.16-src (1.16.3-2longsleep1+focal) ...
Setting up golang-1.16-go (1.16.3-2longsleep1+focal) ...
Setting up golang-src (2:1.16-1longsleep1+bionic) ...
Setting up golang-go (2:1.16-1longsleep1+bionic) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
env: GOPATH=/root/go
go: downloading github.com/gopherdata/gophernotes v0.7.2
go: downloading github.com/cosmos72/gomacro v0.0.0-20210316200708-0b986404ea46
go: downloading github.com/cosmos72/gomacro v0.0.0-20210413192348-a4790c998211
go: downloading github.com/go-zeromq/zmq4 v0.9.0
go: downloading github.com/gofrs/uuid v3.3.0+incompatible
go: downloading github.com/gofrs/uuid v1.2.0
go: downloading github.com/go-zeromq/zmq4 v0.13.0
go: downloading github.com/gofrs/uuid v4.0.0+incompatible
go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
go: downloading github.com/peterh/liner v1.2.0
go: downloading github.com/peterh/liner v1.2.1
go: downloading github.com/go-zeromq/goczmq/v4 v4.2.2
go: downloading golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
go: downloading golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
go: downloading github.com/go-zeromq/goczmq v4.1.0+incompatible
go: downloading golang.org/x/tools v0.0.0-20201023150057-2f4fa188d925
go: downloading github.com/mattn/go-runewidth v0.0.9
go: downloading github.com/mattn/go-runewidth v0.0.12
go: downloading golang.org/x/tools v0.1.0
go: downloading golang.org/x/mod v0.3.0
go: downloading golang.org/x/mod v0.4.2
go: downloading golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4
go: downloading github.com/rivo/uniseg v0.1.0
go: downloading github.com/rivo/uniseg v0.2.0
go: downloading golang.org/x/sys v0.0.0-20210421221651-33663a62ff08
cp: cannot stat '/root/go/src/github.com/gopherdata/gophernotes/kernel/*': No such file or directory
above section is not working.
このスクリプトだとインストールに失敗します。
!cp ~/go/src/github.com/gopherdata/gophernotes/kernel/*
この箇所でエラーになります。
でも、大きな間違いはなさそう。良いところまできているような感じです。
ディレクトリの指定に間違いがあるのかもしれない、github上のプログラムのバージョンアップで。どこにgithubからcolab上のローカルにコピー(ちょっとややこしい)されたカーネルがあるのか調べる必要がある。
using below scripts.
前のセクションのスクリプトではインストールはエラーになります。
おそらく、
~/go/src/github.com/
というのが存在しないためです。
以下のスクリプトでテストしてみて下さい。インストールには約2分間かかります。
インストールが済めばページをリロードします。
Rf.
https://gist.github.com/korakot/132725732d9bef9cf5f97654e1addd1f#gistcomment-3660446
# run this cell first time using python runtime
## Update package-registry
! add-apt-repository ppa:longsleep/golang-backports -y
! apt update
## Install golang and set env-vars
! apt install golang-go
GOPATH="/root/go"
! export GOPATH
## Install gophernotes
REPO_PACKAGE="github.com/gopherdata/gophernotes"
! go get -u $REPO_PACKAGE
! cp "$GOPATH/bin/gophernotes" /usr/bin/
## Clone gophernotes repo
REPO_REMOTE=f"https://{REPO_PACKAGE}"
REPO_LOCAL=f"{GOPATH}/src/{REPO_PACKAGE}"
! mkdir -p $REPO_LOCAL
! git clone "$REPO_REMOTE" "$REPO_LOCAL" || \
(cd "$REPO_LOCAL" ; git pull)
! cd /content/
## Copy files under kernel directory
GOPHERNOTES_KERNELS="/usr/local/share/jupyter/kernels/gophernotes"
! mkdir -p $GOPHERNOTES_KERNELS
! cp $REPO_LOCAL/kernel/* $GOPHERNOTES_KERNELS
# then refresh, it will now use gophernotes. Skip to golang in later cells
log
Ign:1 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:3 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]
Ign:4 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease
Get:5 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease [15.9 kB]
Hit:6 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:7 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release [697 B]
Hit:8 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release
Get:9 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release.gpg [836 B]
Get:10 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Hit:11 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Get:12 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:13 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease [15.9 kB]
Hit:14 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease
Get:15 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [24.5 kB]
Get:16 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [2,116 kB]
Get:17 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease [15.4 kB]
Get:18 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,410 kB]
Get:19 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [395 kB]
Ign:21 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages
Get:21 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages [739 kB]
Get:22 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main Sources [1,756 kB]
Get:23 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2,546 kB]
Get:24 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main amd64 Packages [899 kB]
Get:25 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [425 kB]
Get:26 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [31.4 kB]
Get:27 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,182 kB]
Get:28 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 Packages [39.5 kB]
Get:29 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 Packages [6,000 B]
Fetched 12.9 MB in 4s (3,319 kB/s)
Reading package lists... Done
Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:2 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease
Ign:3 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease
Hit:4 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease
Ign:5 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease
Hit:6 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release
Hit:7 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:8 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release
Hit:9 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:10 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Hit:11 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:12 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Hit:13 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease
Hit:14 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
78 packages can be upgraded. Run 'apt list --upgradable' to see them.
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
golang-1.16-go golang-1.16-src golang-src
Suggested packages:
bzr | brz mercurial subversion
The following NEW packages will be installed:
golang-1.16-go golang-1.16-src golang-go golang-src
0 upgraded, 4 newly installed, 0 to remove and 78 not upgraded.
Need to get 65.7 MB of archives.
After this operation, 377 MB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.16-src amd64 1.16.3-2longsleep1+focal [14.3 MB]
Get:2 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.16-go amd64 1.16.3-2longsleep1+focal [51.4 MB]
Get:3 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-src amd64 2:1.16-1longsleep1+bionic [3,952 B]
Get:4 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-go amd64 2:1.16-1longsleep1+bionic [23.0 kB]
Fetched 65.7 MB in 4s (16.3 MB/s)
Selecting previously unselected package golang-1.16-src.
(Reading database ... 160983 files and directories currently installed.)
Preparing to unpack .../golang-1.16-src_1.16.3-2longsleep1+focal_amd64.deb ...
Unpacking golang-1.16-src (1.16.3-2longsleep1+focal) ...
Selecting previously unselected package golang-1.16-go.
Preparing to unpack .../golang-1.16-go_1.16.3-2longsleep1+focal_amd64.deb ...
Unpacking golang-1.16-go (1.16.3-2longsleep1+focal) ...
Selecting previously unselected package golang-src.
Preparing to unpack .../golang-src_2%3a1.16-1longsleep1+bionic_amd64.deb ...
Unpacking golang-src (2:1.16-1longsleep1+bionic) ...
Selecting previously unselected package golang-go.
Preparing to unpack .../golang-go_2%3a1.16-1longsleep1+bionic_amd64.deb ...
Unpacking golang-go (2:1.16-1longsleep1+bionic) ...
Setting up golang-1.16-src (1.16.3-2longsleep1+focal) ...
Setting up golang-1.16-go (1.16.3-2longsleep1+focal) ...
Setting up golang-src (2:1.16-1longsleep1+bionic) ...
Setting up golang-go (2:1.16-1longsleep1+bionic) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
go: downloading github.com/gopherdata/gophernotes v0.7.2
go: downloading github.com/cosmos72/gomacro v0.0.0-20210316200708-0b986404ea46
go: downloading github.com/cosmos72/gomacro v0.0.0-20210413192348-a4790c998211
go: downloading github.com/go-zeromq/zmq4 v0.9.0
go: downloading github.com/gofrs/uuid v3.3.0+incompatible
go: downloading github.com/gofrs/uuid v1.2.0
go: downloading github.com/gofrs/uuid v4.0.0+incompatible
go: downloading github.com/go-zeromq/zmq4 v0.13.0
go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
go: downloading github.com/peterh/liner v1.2.0
go: downloading github.com/peterh/liner v1.2.1
go: downloading github.com/go-zeromq/goczmq/v4 v4.2.2
go: downloading golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
go: downloading golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
go: downloading golang.org/x/tools v0.0.0-20201023150057-2f4fa188d925
go: downloading github.com/go-zeromq/goczmq v4.1.0+incompatible
go: downloading github.com/mattn/go-runewidth v0.0.9
go: downloading github.com/mattn/go-runewidth v0.0.12
go: downloading golang.org/x/tools v0.1.0
go: downloading golang.org/x/mod v0.3.0
go: downloading golang.org/x/mod v0.4.2
go: downloading golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4
go: downloading github.com/rivo/uniseg v0.1.0
go: downloading github.com/rivo/uniseg v0.2.0
go: downloading golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988
Cloning into '/root/go/src/github.com/gopherdata/gophernotes'...
remote: Enumerating objects: 3821, done.
remote: Counting objects: 100% (121/121), done.
remote: Compressing objects: 100% (81/81), done.
remote: Total 3821 (delta 75), reused 78 (delta 39), pack-reused 3700
Receiving objects: 100% (3821/3821), 20.68 MiB | 20.09 MiB/s, done.
Resolving deltas: 100% (2006/2006), done.
test golang
上のセクションで、Go言語用のカーネルのインストールがエラーなく済めば、ブラウザでページをリロードして、Go用のカーネルに切り替えて、よくあるGo言語の文法でGoのプログラムをテスト。
import "fmt"
fmt.Println("Hello, Gianni!")
for i := 1; i < 10; i++ {
print(i)
}
環境変数ていうのかな?現在の初期状態を確認します。
$go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.16"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.16/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1038342927=/tmp/go-build -gno-record-gcc-switches"
だいじなことですけど、コードセルの中で bash コマンドには$
が要ります。
たとえば、
$go get github.com/kkdai/youtube/v2
$go env GOPATH
$dir /root/go/
$pwd
など。 python のカーネルの場合は、!
や%
、%%
、%%bash
だった気がしますね。
### Alternatively: Using "jupyter kernel install <kernel-dir>"
# ! mkdir -p ./gophernotes
# ! cp $REPO_LOCAL/kernel ./gophernotes
# ! jupyter kernel install ./gophernotes
注意事項 limitations
https://github.com/gopherdata/gophernotes#limitations
さらに他のカーネルについて知りたい場合は、
jupyter kernel
の情報を調べてみてください。