Building GDK from source

How to build GDK on Ubuntu 22.04.1 (x86_64)

The following steps will download the GDK source code, install dependencies and build a .whl release file that targets the version of Python you have installed.

The steps are for Ubuntu and have been tested with Ubuntu 22.04.01 (x86_64 with the minimal install option) and Python 3.10.6.

The output is a file named something like greenaddress-0.0.58-cp310-cp310-linux_x86_64.whl, where the cp number refers to the target Python version you have provided.

📘

Install script

If you don't want to step through each command there is a complete shell script at the end of this page you can run which will perform all steps in one pass.

GDK source code

To pull the latest GDK source code, run the following commands in sequence from a command line terminal: You may well already have git installed.

sudo apt install git
git clone https://github.com/Blockstream/gdk.git
cd gdk

Installing dependencies

We will be using the ubuntu_18.04_deps.sh file to install all dependencies, although the file is named 18.04 it will work with later version of Ubuntu as well. There are a few things we need to amend first, as the file was designed to be run within a docker environment, which we are not using here.

Use a file editor to open the ubuntu_18.04_deps.sh file, which you will find in the gdk/tools directory.

Change this line:

pip3 install --require-hashes -r /requirements.txt

So that it reads:

pip3 install --require-hashes -r tools/requirements.txt

Now delete the following lines from the file:

rm /requirements.txt

curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.64.0
source /root/.cargo/env
rustup component add rustfmt clippy

Save and close the file. We can now run the script and it will install the dependencies needed:

sudo ./tools/ubuntu_18.04_deps.sh

Now we will prepare the Rust build manager, cargo:

curl https://sh.rustup.rs> -sSf | sh -s -- -y --default-toolchain 1.64.0  
source ~/.cargo/env  
rustup component add rustfmt clippy

We need to find out what version of python3 is installed and make sure that the python command points to that version. To see if this is already set up correctly run:

python --version

If that returns “Command 'python' not found”, which is does on a default Ubuntu 22.04.1 install, you will need to link python to python3:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10

Now the following should return your python3 version:

python --version

Note the python major and minor version returned and use that as the value for --python-version below (e.g. use 3.10 if the call above returned 3.10.6) and run the build process:

./tools/build.sh --install $PWD/gdk-python --clang --python-version 3.10

When that completes you will find the .whl file in the new gdk/gdk-python/share/python directory!

Using your newly complied GDK wheel file

To install GDK using the output .whl file just copy it to where you want to run it from and then run the following from a Python environment, either a “virtualenv” or your native Python one. If the build above output a file named greenaddress-0.0.58-cp310-cp310-linux_x86_64.whl you would run:

pip3 install greenaddress-0.0.58-cp310-cp310-linux_x86_64.whl

Test the install worked by opening an interactive Python shell from the command line:

python3

Enter the following to check the import works:

import greenaddress

If that didn’t return an error you have GDK installed correctly!

You may then quit the python terminal...

quit()

... and begin using GDK within your Python applications!

GDK Ubuntu build script

The following is just all of the above compressed into one script. Save the following script commands in a file named gdk_ubuntu_build.sh and then run it like this:

bash gdk_ubuntu_build.sh

You will be asked to authenticate as [sudo] and the script will run. Please note the following script assumes you are using Python 3.10.* and do not have the python command mapped to python3 already. If you are using a new install of Ubuntu 22.04.1, leave the line that maps an alternative python to python3 in (as it is not already done by default) and will possibly just have to amend the Python version 3.10 if you have an updated Python that is newer than the one used here. When that completes you will find the .whl file in the new gdk/gdk-python/share/python directory!

#! /usr/bin/env bash
set -e

sudo apt update -y
sudo apt upgrade -y
sudo apt install git curl autoconf pkg-config build-essential libtool virtualenv python3-{pip,yaml} ninja-build clang llvm-dev git swig unzip cmake -y

git clone https://github.com/Blockstream/gdk.git
cd gdk
sudo pip3 install --require-hashes -r tools/requirements.txt
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.64.0
source ~/.cargo/env
rustup component add rustfmt clippy

mkdir ~/tmp
mkdir ~/tmp/protoc
cd ~/tmp/protoc
curl -Ls https://github.com/protocolbuffers/protobuf/releases/download/v3.19.3/protoc-3.19.3-linux-x86_64.zip > protoc.zip
unzip protoc.zip
sudo mv ~/tmp/protoc/bin/protoc /usr/local/bin
sudo rm -rf ~/tmp/protoc

cd ~/gdk
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
./tools/build.sh --install $PWD/gdk-python --clang --python-version 3.10