Skip to content

Instantly share code, notes, and snippets.

@korakot
Last active April 27, 2023 06:12
Show Gist options
  • Save korakot/132725732d9bef9cf5f97654e1addd1f to your computer and use it in GitHub Desktop.
Save korakot/132725732d9bef9cf5f97654e1addd1f to your computer and use it in GitHub Desktop.
Using Golang kernel in Colab
@mlgitter
Copy link

mlgitter commented Jun 30, 2021

It's not working. Throws an error at the end. So, I modified the script to circumvent the error. This takes care of the error(s). But now I do not get the choice of kernels under runtime selection: there is no drop down to choose Go from. 🔥

# 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
### Alternatively: Using "jupyter kernel install <kernel-dir>"
# ! mkdir -p ./gophernotes
# ! cp $REPO_LOCAL/kernel ./gophernotes
# ! jupyter kernel install ./gophernotes
# then refresh, it will now use gophernotes. Skip to golang in later cells

it's still not working for me for today(

Get:1 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease [15.9 kB]

Hit:2 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]
Hit:5 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Get:6 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:7 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease [15.9 kB]
Get:8 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Ign:9 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease
Get:10 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease [21.3 kB]
Get:11 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease [15.4 kB]
Ign:12 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease
Get:13 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release [697 B]
Hit:14 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release
Get:15 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release.gpg [836 B]
Get:16 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main Sources [1,777 kB]
Get:17 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main amd64 Packages [909 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [506 kB]
Get:19 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,188 kB]
Get:20 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2,656 kB]
Get:21 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 Packages [40.9 kB]
Get:22 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic/main amd64 Packages [41.5 kB]
Get:23 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 Packages [6,007 B]
Ign:25 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages
Get:25 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages [634 kB]
Get:26 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [2,221 kB]
Get:27 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [473 kB]
Get:28 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,418 kB]
Fetched 13.2 MB in 4s (3,384 kB/s)
Reading package lists... Done
Hit:1 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease
Hit:2 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:4 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease
Hit:5 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Hit:6 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:7 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Hit:8 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease
Hit:9 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease
Hit:10 http://security.ubuntu.com/ubuntu bionic-security InRelease
Ign:11 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease
Ign:12 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease
Hit:13 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release
Hit:14 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release
Reading package lists... Done
Building dependency tree
Reading state information... Done
83 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 83 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.5-1longsleep1+focal [14.3 MB]
Get:2 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.16-go amd64 1.16.5-1longsleep1+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 1s (49.0 MB/s)
Selecting previously unselected package golang-1.16-src.
(Reading database ... 160772 files and directories currently installed.)
Preparing to unpack .../golang-1.16-src_1.16.5-1longsleep1+focal_amd64.deb ...
Unpacking golang-1.16-src (1.16.5-1longsleep1+focal) ...
Selecting previously unselected package golang-1.16-go.
Preparing to unpack .../golang-1.16-go_1.16.5-1longsleep1+focal_amd64.deb ...
Unpacking golang-1.16-go (1.16.5-1longsleep1+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.5-1longsleep1+focal) ...
Setting up golang-1.16-go (1.16.5-1longsleep1+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.3
go: downloading github.com/cosmos72/gomacro v0.0.0-20210624153544-b4935e406a41
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.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.1.0
go: downloading github.com/go-zeromq/goczmq v4.1.0+incompatible
go: downloading github.com/mattn/go-runewidth v0.0.12
go: downloading github.com/mattn/go-runewidth v0.0.13
go: downloading golang.org/x/tools v0.1.4
go: downloading github.com/rivo/uniseg v0.1.0
go: downloading golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4
go: downloading golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
go: downloading github.com/rivo/uniseg v0.2.0
go: downloading golang.org/x/mod v0.3.0
go: downloading golang.org/x/mod v0.4.2
go: downloading github.com/gofrs/uuid v1.2.0
cp: cannot stat '/root/go/src/github.com/gopherdata/gophernotes/kernel/*': No such file or directory

@ucalyptus2
Copy link

How do I install go1.15+ on colab? @korakot

@korakot
Copy link
Author

korakot commented Jul 13, 2021

I have corrected the path. It should work now using go1.16

@sugatoray
Copy link

I have corrected the path. It should work now using go1.16

🚀 🚀 I tested it and it works now. 👍

Running the following code block (set as the top most code block) followed by refreshing the browser window/tab with colab-notebook, sets up golang in the notebook environment.

# run this cell first time using python runtime
!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/
!npx degit gopherdata/gophernotes/kernel \
     /usr/local/share/jupyter/kernels/gophernotes
# then refresh, it will now use gophernotes. Skip to golang in later cells

@frohro
Copy link

frohro commented Oct 10, 2022

Do you think something like this could be used to use an octave kernel in Colab? I was able to install octave and the octave-kernel, but don't know how to make Colab start the octave-kernel.
Thanks,
Rob

@cjwomack
Copy link

cjwomack commented Feb 4, 2023

Doesn't seem to work currently. Throws an error around go get -u

go: go.mod file not found in current directory or any parent directory.
	'go get' is no longer supported outside a module.
	To build and install a command, use 'go install' with a version,
	like 'go install example.com/cmd@latest'
	For more information, see https://golang.org/doc/go-get-install-deprecation
	or run 'go help get' or 'go help install'.

@cjwomack
Copy link

cjwomack commented Feb 4, 2023

@frohro You can get octave-kernel to work if you download the .ipynb and change the first bit of the file in a text editor to

{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "octave",
      "display_name": "Octave"
    },
    "language_info": {
      "name": "octave"
    }
  },

and upload this to google colab. Run the first cell. This downloads the octave kernel and installs it. After the first cell is run, then refresh... You should be able to run octave in the cells.

I came across this while trying to get the kotlin kernel to work...

@Yatima-Kagurazaka
Copy link

@cjwomack If you obey the error message, it works.

To build and install a command, use 'go install' with a version,
like 'go install example.com/cmd@latest'

@Yatima-Kagurazaka
Copy link

But... I can't finish reconnecting the runtime after reloading the page.
It is stuck in reconnecting, and I can't run anything.
I terminate other sessions, but nothing changes.

@korakot Could you tell me if you can reconnect it?

@Tylersuard
Copy link

Not working for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment