Skip to content

Instantly share code, notes, and snippets.

@christian-korneck
Last active May 30, 2023 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christian-korneck/e35bc00e12026e13f50c83519a32470e to your computer and use it in GitHub Desktop.
Save christian-korneck/e35bc00e12026e13f50c83519a32470e to your computer and use it in GitHub Desktop.
python3.7-go-ubuntu20.04-arm64
# we're on Ubuntu 20.04 on arm64
$ uname -a
...
Linux [...] 5.15.0 #40~20.04.1-Ubuntu [...] aarch64 [...]
...

# install build tools
$ apt-get -y update && apt-get -y install curl build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev pkg-config
# install latest Go
$ curl -L https://golang.org/dl/go1.20.4.linux-arm64.tar.gz -o go.tar.gz && tar -C /usr/local -xzf go.tar.gz && ln -s /usr/local/go/bin/go /usr/local/bin/go

# build and install Python 3.7 to /opt/py
$ curl https://www.python.org/ftp/python/3.7.10/Python-3.7.10.tgz -o python.tgz && tar xf python.tgz
$ cd Python-3.7.10
$ ./configure --enable-shared --prefix=/opt/py
$ make -j `nproc`
$ make install
$ echo /opt/py/lib > /etc/ld.so.conf.d/py.conf && ldconfig
$ /opt/py/bin/python3.7 -m ensurepip
$ export PATH=$PATH:/opt/py/bin 				# to persist, put this i.e. in your bashrc
$ export PKG_CONFIG_PATH=/opt/py/lib/pkgconfig 	# to persist, put this i.e. in your bashrc

# check if pkg-config knows python3
$ pkg-config --list-all
...
python3          Python - Python library
...

# build go/python example
$ cd..
$ mkdir hello
$ cd hello
$ go mod init hello
$ vim main.go
$ cat main.go

package main

import "github.com/go-python/cpy3"

func main() {

  defer python3.Py_Finalize()
  python3.Py_Initialize()
  python3.PyRun_SimpleString("print('hello from python in go')")

}

$ go mod tidy
$ go build .

# run example program
$ ./hello
hello from python in go

# inspect dependencies of example program (linked against python3)
$ ldd hello
...
libpython3.7m.so.1.0 => /opt/py/lib/libpython3.7m.so.1.0 (0x0000ffffa3466000)
...

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