Set up and Running a Bitcoin Lightning Full Node on Raspberry Pi

Set up and Running a Bitcoin Lightning Full Node on Raspberry Pi

How to run a Bitcoin Lightning Node on Raspberry Pi. This simple guide shows you step by step how to setup, run and monitor your Bitcoin node. To help with scalability and performance of the Bitcoin network they have introduced the Lightning Network. This allows "off chain" transactions. This guide walks you through the steps in running a full Bitcoin Lightning Network Node on a Raspberry Pi. This includes: 1. Setting up you Pi 2. Setting up the Bitcoin Full node, sync the blockchain and add to the network 3. Configure the Lightning node and broadcast 4. Add some funds 5. Monitoring

Raspberry Pi Setup

First you will need a Raspberry Pi with an SD card, then you will need a 1 TB External Hard Drive which will only be used to store the Bitcoin blockchain. Be warned you may need a powered USB hub to power this HDD and possibly most. I spent ages trying to get it to book without but the Pi kept restarting. I bought a 4-port-usb-hub-usb-2-0 with a powered supply Universal Power Supply. This provides the HDD enough power to stop the Pi having to supply anything. First we need to install Raspian Lite and then mount onto the SD card there is a guide here. Now once you have installed it, open it and add a file called ssh (no extension) to the boot partition of the SD card.

Once we are setup turn your Pi on and connect using the HDMI port to a screen. Plug in your Ethernet to get connected to your network. Login using the default Pi credentials. Username is 'pi' and password is 'raspberry'. Now type

ifconfig

and get your ipaddress.

On another machine, I recommend a Windows one install PuTTY enter the IP address into the field and connect. Login using the default credentials.

Let's update the Pi.

pi@raspberrypi:~$ sudo apt-get update
pi@raspberrypi:~$ sudo apt-get upgrade
pi@raspberrypi:~$ sudo apt-get dist-upgrade
pi@raspberrypi:~$ sudo apt-get autoremove

Using the 1TB HDD you have format on the Windows PC to use NTFS. Download Bitcoin Core and sync with the network, this will take a while but quicker than doing it through the Pi. Note: Bitcoin Core opens and starts immediately syncing the blockchain. Unfortunately, we need to set one additional setting in the “bitcoin.conf” file, otherwise the whole blockchain will be useless. Using the menu, open “Settings” / “Options” and click the button “Open Configuration File”. Enter the following line:

txindex=1

Save and close the text file, quit Bitcoin Core using “File” / “Exit” and restart the program. The program will start syncing again. Troubleshooting. When completed look for the folders containing the folders: blocks, chainstate and indexes. Copy these three folders to your HDD in the BitcoinData folder you will create later.

Install NTFS on the Pi:

pi@raspberrypi:~$ sudo apt-get install ntfs-3g

Confirm the HDD is recognised:

sudo fdisk -l

At the bottom you should see something like this:

At the bottom of the partion list, you should see something like this:

Device Start End Sectors Size Type
/dev/sda1 2048 625052127 625670580 1024.1G Microsoft basic data

The '/dev/sda1' is the mount pount for your drive. Now we need to make a symlink:

pi@raspberrypi:~$ mkdir data
pi@raspberrypi:~$ sudo mount /dev/sda1 /home/pi/data

We need to mount this if the Pi is rebooted:

pi@raspberrypi:~$ sudo vi /etc/fstab
//and add this line:
/dev/sda1 /home/pi/data ntfs-3g rw,default 0 0

Now you will be able to see the contents of the drive:

pi@raspberrypi:~$ ls data
drwxrwxrwx 1 root root 0 Mar 18 22:44 $RECYCLE.BIN
drwxrwxrwx 1 root root 0 Mar 18 20:47 System Volume Information

Done!! We now have a fully updated Pi and ready to go.

Sync and broadcast a full node

We need to run the pre-requisites needed to compile and run Bitcoin.

pi@raspberrypi:~$ sudo apt-get install git build-essential autoconf libssl-dev libboost-dev libboost-chrono-dev libboost-filesystem-dev libboost-program-options-dev libboost-system-dev libboost-test-dev libboost-thread-dev libtool libzmq3-dev libevent-dev libtool libssl-dev libboost-all-dev libminiupnpc-dev qt4-dev-tools libprotobuf-dev protobuf-compiler libqrencode-dev db4.8-util -y

Install Berkely DB:

pi@raspberrypi:~$ wget http://download.oracle.com/berkeley-db/db-4.8.30.NC.tar.gz
pi@raspberrypi:~$ tar -xzvf db-4.8.30.NC.tar.gz
pi@raspberrypi:~$ cd db-4.8.30.NC/build_unix/
pi@raspberrypi:~/db-4.8.30$ ../dist/configure --enable-cxx
pi@raspberrypi:~db-4.8.30/build_unix$ make -j4
pi@raspberrypi:~db-4.8.30/build_unix$ sudo make install
pi@raspberrypi:~db-4.8.30/build_unix$ cd ~

Update the Pi again to make sure we are fully up to date:

pi@raspberrypi:~$ sudo apt-get update
pi@raspberrypi:~$ sudo apt-get upgrade
pi@raspberrypi:~$ sudo apt-get dist-upgrade
pi@raspberrypi:~$ sudo apt-get autoremove

Download Bitcoin and build, this may take a while (maybe 2 hours+), Bitcoin is on 0.18 but change to the latest:

pi@raspberrypi:~$ git clone -b 0.18 https://github.com/bitcoin/bitcoin.git
pi@raspberrypi:~$ cd bitcoin/
pi@raspberrypi:~bitcoin$ ./autogen.sh
pi@raspberrypi:~bitcoin$ ./configure
pi@raspberrypi:~bitcoin$ make
pi@raspberrypi:~bitcoin$ sudo make install

After its downloaded and installed lets set it up:

pi@raspberrypi:~$ cd data
pi@raspberrypi:~/data$ mkdir BitcoinData
pi@raspberrypi:~/data$ cd BitcoinData
pi@raspberrypi:~/data/BitcoinData$ vi bitcoin.conf

Inside add these lines, change the password to your own:

server=1
daemon=1
txindex=1
rpcuser=bitcoinrpc
rpcpassword=isi99js9NNs09znAwP03MN22398j9jwqwjNNNisj
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28332

Add a Bitcoin Symbolic Link for bitcoin-cli commands:

pi@raspberrypi:~/data/BitcoinData$ cd ~
pi@raspberrypi:~$ ln -s /home/pi/data/BitcoinData/ ~/.bitcoin
//Start the Bitcoin daemon
pi@raspberrypi~$ bitcoind -daemon

Check progress by looking at the output for 'progress'. Once it hits 1.0, you are synced!:

pi@raspberrypi~$ tail -n 1 ~/.bitcoin/debug.log
pi@raspberrypi~$ bitcoin-cli getblockchaininfo

Allow the pi user to run tasks at startup:

pi@raspberrypi~$ sudo vi /etc/cron.allow

Add this line:

pi

Create a bash file to start the Bitcoin Daemon on restart:

pi@raspberrypi~$ vi bitcoin-start.sh

Add this:

#!/bin/bash
sleep 10
/home/pi/bitcoin/src/bitcoind -daemon

Add it as a cron job on boot:

pi@raspberrypi~$ chmod +x bitcoin-start.sh
pi@raspberrypi~$ crontab -u pi -e

Add this line to the bottom:

@reboot /home/pi/bitcoin-start.sh

Now when you restart your Pi, bitcoind will startup.

Lets make the node available to the outside world, open and forward both port 8333 (Bitcoin) and port 9735 (Lightning) on your router. I have a tutorial for Golem but it can be used for this and check your ports here here .

Lightning Network

Download Golang

pi@raspberrypi~$ wget https://dl.google.com/go/go1.12.3.linux-armv6l.tar.gz
pi@raspberrypi~$ sudo tar -C /usr/local -xzf go1.12.3.linux-armv6l.tar.gz
pi@raspberrypi~$ export PATH=$PATH:/usr/local/go/bin
pi@raspberrypi~$ mkdir gocode
pi@raspberrypi~$ vi .bashrc

//Add the following lines:
export GOPATH=~/gocode
export PATH=$PATH:$GOPATH/bin

//Run your .bashrc
pi@raspberrypi~$ source .bashrc

//Close and get another dependency
pi@raspberrypi~$ go get -u github.com/golang/dep/cmd/dep

Now the bit we've been waiting for, install Lighting:

sudo nano $HOME/.profile
//Add this line
export PATH=$PATH:/usr/local/go/bin
//Close the window and reopen it. then 
cd $GOPATH/src/github.com/lightningnetwork/lnd
go get -d github.com/lightningnetwork/lnd
cd $GOPATH/src/github.com/lightningnetwork/lnd
dep ensure
dep init
make && make install
//Here is an alternative to install lnd
pi@raspberrypi~$ //Check your version of Pi by using cat /proc/cpuinfo ( change the armv6 to the version)
pi@raspberrypi~$ wget https://github.com/lightningnetwork/lnd/releases/download/v0.6.1-beta/lnd-linux-armv6-v0.6.1-beta.tar.gz
pi@raspberrypi~$ wget https://github.com/lightningnetwork/lnd/releases/download/v0.6.1-beta/manifest-v0.6.1-beta.txt
pi@raspberrypi~$ wget https://github.com/lightningnetwork/lnd/releases/download/v0.6.1-beta/manifest-v0.6.1-beta.txt.sig
pi@raspberrypi~$ wget https://keybase.io/roasbeef/pgp_keys.asc
pi@raspberrypi~$ tar -xzf lnd-linux-armv6-v0.6.1-beta.tar.gz
pi@raspberrypi~$ sudo install -m 0755 -o root -g root -t /usr/local/bin lnd-linux-armv6-v0.6.1-beta/*
pi@raspberrypi~$ lnd --version
lnd version 0.6.1-beta commit=v0.6.1-beta


Lets create a lnd.conf. Replace the x.x.x.x below with your public IP address. Doing this will broadcast you have a Lightning Node to other nodes, and auto-connect to available channels:

pi@raspberrypi:~$ cd data
pi@raspberrypi:~/data$ mkdir LightningData
pi@raspberrypi:~/data$ cd LightningData
pi@raspberrypi:~/data/LightningData$ vi lnd.conf

//Add these lines:
[Application Options]
debuglevel=debug
debughtlc=true
maxpendingchannels=10
noencryptwallet=1
externalip=x.x.x.x

[Bitcoin]
bitcoin.active=1
bitcoin.mainnet=1
bitcoin.node=bitcoind

[Autopilot]
autopilot.active=1
autopilot.maxchannels=10
autopilot.allocation=1.0

Add a symbolic link for ~/.lnd:

pi@raspberrypi:~/data/LightningData$ cd ~
pi@raspberrypi:~$ ln -s /home/pi/data/LightningData/ ~/.lnd

//Create a bash file to start it up:

pi@raspberrypi~:~$ vi lightning-start.sh

With these lines:

#!/bin/bash
sleep 20
/home/pi/gocode/bin/lnd

Now time to set it up to run at boot time:

pi@raspberrypi:~$ chmod +x lightning-start.sh
pi@raspberrypi:~$ crontab -u pi -e

//Add this line to the bottom, under the bitcoin-start.sh line:

@reboot /home/pi/lightning-start.sh

You can restart your Raspberry Pi now, and both bitcoind and lnd will start up. For now, let us just start it manually:

pi@raspberrypi:~$ /home/pi/lightning-start.sh &

Finished

Wow that was quite long wasn't it but we are still in the early stages. Now lets get the final parts in place:

pi@raspberrypi:~$ lncli create

//Get a new receiving address:
pi@raspberrypi:~$ lncli newaddress p2wkh

//Send a small amount of Bitcoin to your Lightning Node. Be careful, and just send a small amount of BTC (read the disclaimer in the footer).
//Go to Coinbase (use this link for $10 free) or use Binance.
//Run these to see some diagnostic info:

pi@raspberrypi:~$ lncli getinfo
pi@raspberrypi:~$ lncli listpeers
pi@raspberrypi:~$ lncli listchannels
pi@raspberrypi:~$ lncli listpayments

Troubleshooting

Virtual memory exhausted: Cannot allocate memory

Virtual memory exhausted: Cannot allocate memory
Makefile:7495: recipe for target 'libbitcoin_server_a-init.o' failed

How to change Raspberry Pi's Swapfile Size on Raspbian

sudo nano /etc/dphys-swapfile

The default value in Raspbian is:

CONF_SWAPSIZE=100

Change this to:

CONF_SWAPSIZE=1024

Stop and start the service that manages the swapfile own Rasbian:

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

Verify the amount of memory + swap by issuing the following command:

free -m
total        used        free      shared  buff/cache   available
Mem:            433          25         368           5          39         361
Swap:          1023           0        1023

Issues adding HDD to Pi

I was having no end of trouble with the HDD when connecting it to the Pi. It turned out to be the powered USB not having enough power (I've listed the items I had to buy at the top) but for future debugging reference you can see the current log of the Pi by typing the following. Using this when you plug the HDD into the Pi can help show errors or config issues.

tail -f /var/log/messages

If you want to see what disks are on the Pi, type the following and it should be at the bottom.

sudo fdisk -l

To see what USB's are connected to the Pi type the following, this will list all the USB's connected to the Pi.

lsusb

bitcoind: error while loading shared libraries: libdb_cxx-4.8.so

sudo apt-get install software-properties-common

net/http: TLS handshake timeout)

This was hell.

net/http: TLS handshake timeout)

I couldn't find any information on this but I received this by using the WiFi dongle connected to my Pi. I resolved it by using the Ethernet cable to connect to my router. To speed check:

sudo apt update && sudo apt install speedtest-cli -y
speedtest-cli

Extra Commands

Here are some commands that may be important during the set up.

To properly shutdown the Pi

sudo shutdown -h now

Restart your Pi Remotely

ssh pi@192.168.0.XXX 'echo raspberry | sudo reboot -r now'

[lncli] open /home/pi/.lnd/tls.cert: no such file or directory

pi@raspberrypi:~$ cd .lnd
pi@raspberrypi:~$ openssl ecparam -genkey -name prime256v1 -out tls.key
pi@raspberrypi:~$ openssl req -new -sha256 -key tls.key -out csr.csr -subj '/CN=localhost/O=lnd'
pi@raspberrypi:~$ openssl req -x509 -sha256 -days 36500 -key tls.key -in csr.csr -out tls.cert
pi@raspberrypi:~$ rm csr.csr

To Stop your HDD being corrupted by disconnecting

Always run

sudo shutdown -h now

How to stop/shutdown Bitcoind server

bitcoin-cli stop

Monitoring

Using the command line can get a little bit tedious. So I have built and open source website to monitor your Bitcoin Node. This is in very early alpha and use it at your own risk. If you see any errors or feel you can add to it send me a pull request. You can find it at Github. The page will allow you to see the status of your node, monitor the wallet and create addresses. Also see your logs in an easy to view place. Have full monitoring of your node over your network. Will allow you to see your BTC node status on your mobile, tablet and PC just using your Raspberry Pi to host.

Categories: Posts