Installing a Bitcoin full node on Alpine Linux

Running your own Bitcoin node offers enhanced privacy and security by allowing independent verification of transactions and blocks. It reduces reliance on third parties, lowers fraud risk, and supports network decentralization.

Installing a Bitcoin full node on Alpine Linux
Photo by Thought Catalog / Unsplash

Running your own Bitcoin node offers several key benefits. It enhances privacy and security by allowing you to verify transactions and blocks independently, eliminating the need to trust third-party services. This ensures that you follow the Bitcoin protocol without reliance on intermediaries, reducing the risk of manipulation or fraud. Additionally, operating a node contributes to the robustness and decentralization of the Bitcoin network, supporting its integrity and resilience. It also provides you with full control over your Bitcoin transactions and the ability to use advanced features. Bitcoin originally would download the entire blockchain and mine coins while you have the wallet application open. This doesn't fit most people use cases so it's common for wallets to run in pruning mode or thin clients.

The first step is to open up your ports. Bitcoin operates on port 8333 by default. You can check that the port is open on https://www.yougetsignal.com/tools/open-ports/. Once you have your node setup you can use https://bitnodes.io/ to check that it is visible on the network. A full node is relatively not demanding at all. It can run with under 1GB of RAM but I would recommend giving it 2GB if you are compiling from source like in this guide.

Bitcoin Node Resources Graphed

Here's a step-by-step guide to what the script does:

Step 1: Update Packages

Update the package list and upgrade all installed packages apk update && apk upgrade

Step 2: Install Necessary Packages

Install the necessary packages for building and running a Bitcoin node:apk add build-base libtool autoconf automake pkgconfig boost-dev libevent-dev wget

Step 3: Add a bitcoin user

if ! id -u bitcoin >/dev/null 2>&1; then
  adduser -D -s /sbin/nologin bitcoin
fi

Step 4: Install Bitcoin Core

# Download a specific version (in this case 26.0)
wget "https://bitcoincore.org/bin/bitcoin-core-26.0/bitcoin-26.0.tar.gz"

# Unpack the source code
tar xvf "bitcoin-26.0.tar.gz"
cd "bitcoin-$bitcoin_version"

# Generate configuration scripts
./autogen.sh

# Configure the installation
./configure --prefix=/usr --disable-wallet --with-incompatible-bdb

# Build and install Bitcoin Core
make && make install

Step 5: Add the Bitcoin Daemon to Startup

if ! grep -q "bitcoind -daemon" /etc/init.d/boot.local; then
  echo "su - bitcoin -c 'bitcoind -daemon'" >> /etc/init.d/boot.local
  chmod +x /etc/init.d/boot.local
  rc-update add /etc/init.d/boot.local default
fi

Step 6: Switch to the bitcoin user and start the daemon

su - bitcoin -c "bitcoind -daemon"

Here is the complete script to install a specific version:

#!/bin/sh

# Update the packages
apk update && apk upgrade

# Install the necessary packages for the Bitcoin node
apk add build-base libtool autoconf automake pkgconfig boost-dev libevent-dev wget

# Add bitcoin user if it doesn't exist
if ! id -u bitcoin >/dev/null 2>&1; then
  adduser -D -s /sbin/nologin bitcoin
fi

# Set the desired Bitcoin Core version
bitcoin_version="27.1"

# Check if bitcoind is already installed
if command -v bitcoind >/dev/null 2>&1; then
  # Get the installed version
  installed_version=$(bitcoind --version | awk '{print $NF}')

  # Debugging: Print installed version
  echo "Installed version: $installed_version"

  # Compare the versions
  if [ "$installed_version" != "$bitcoin_version" ]; then
    echo "A new version of Bitcoin Core is available. Updating to version $bitcoin_version..."

    # Download the desired version
    wget "https://bitcoincore.org/bin/bitcoin-core-$bitcoin_version/bitcoin-$bitcoin_version.tar.gz"

    # Unpack the source code
    tar xvf "bitcoin-$bitcoin_version.tar.gz"
    cd "bitcoin-$bitcoin_version"

    # Generate configuration scripts
    ./autogen.sh

    # Configure the installation
    ./configure --prefix=/usr --disable-wallet --with-incompatible-bdb

    # Build and install Bitcoin Core
    make && make install

    echo "Bitcoin Core has been updated to version $bitcoin_version."
  else
    echo "Bitcoin Core is already up to date (version $installed_version)."
  fi
else
  echo "Bitcoin Core is not installed. Installing version $bitcoin_version..."

  # Download the desired version
  wget "https://bitcoincore.org/bin/bitcoin-core-$bitcoin_version/bitcoin-$bitcoin_version.tar.gz"

  # Unpack the source code
  tar xvf "bitcoin-$bitcoin_version.tar.gz"
  cd "bitcoin-$bitcoin_version"

  # Generate configuration scripts
  ./autogen.sh

  # Configure the installation
  ./configure --prefix=/usr --disable-wallet --with-incompatible-bdb

  # Build and install Bitcoin Core
  make && make install

  echo "Bitcoin Core $bitcoin_version has been installed."
fi

# Switch to bitcoin user and start the Bitcoin daemon
su - bitcoin -c "bitcoind -daemon"

# Add bitcoind to boot.local if it's not already there
if ! grep -q "bitcoind -daemon" /etc/init.d/boot.local; then
  echo "su - bitcoin -c 'bitcoind -daemon'" >> /etc/init.d/boot.local
  chmod +x /etc/init.d/boot.local
  rc-update add /etc/init.d/boot.local default
fi