2020-09-03 17:53:22 +00:00
---
layout: default
title: Basic installation
nav_order: 2
---
2021-07-07 09:58:21 +00:00
# Basic installation
2022-07-12 23:15:48 +00:00
2022-07-28 10:19:30 +00:00
You can choose between four package managers, `poetry` , `nix` and `venv` .
2022-07-12 23:15:48 +00:00
By default, LNbits will use SQLite as its database. You can also use PostgreSQL which is recommended for applications with a high load (see guide below).
2022-09-11 10:06:55 +00:00
## Option 1 (recommended): poetry
2022-07-12 23:15:48 +00:00
2022-09-20 12:08:45 +00:00
If you have problems installing LNbits using these instructions, please have a look at the [Troubleshooting ](#troubleshooting ) section.
2022-07-27 14:01:23 +00:00
```sh
git clone https://github.com/lnbits/lnbits-legend.git
cd lnbits-legend/
2022-10-06 12:25:14 +00:00
# for making sure python 3.9 is installed, skip if installed. To check your installed version: python3 --version
2022-08-02 20:59:55 +00:00
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
2022-08-15 14:22:20 +00:00
sudo apt install python3.9 python3.9-distutils
2022-08-02 20:59:55 +00:00
2022-07-27 14:01:23 +00:00
curl -sSL https://install.python-poetry.org | python3 -
2022-10-06 12:25:14 +00:00
# Once the above poetry install is completed, use the installation path printed to terminal and replace in the following command
export PATH="/home/user/.local/bin:$PATH"
# Next command, you can exchange with python3.10 or newer versions.
# Identify your version with python3 --version and specify in the next line
# command is only needed when your default python is not ^3.9 or ^3.10
poetry env use python3.9
2022-10-05 07:16:54 +00:00
poetry install --only main
2022-07-27 14:01:23 +00:00
2022-08-04 06:05:51 +00:00
mkdir data
2022-08-02 21:00:55 +00:00
cp .env.example .env
2022-10-06 12:25:14 +00:00
# set funding source amongst other options
nano .env
2022-08-04 06:05:51 +00:00
```
2022-07-27 14:01:23 +00:00
#### Running the server
2022-08-03 12:10:32 +00:00
2022-07-27 14:01:23 +00:00
```sh
poetry run lnbits
# To change port/host pass 'poetry run lnbits --port 9000 --host 0.0.0.0'
2022-10-06 12:50:33 +00:00
# adding --debug in the start-up command above to help your troubleshooting and generate a more verbose output
# Note that you have to add the line DEBUG=true in your .env file, too.
2022-07-27 14:01:23 +00:00
```
2022-10-28 09:22:59 +00:00
## Option 2: Nix
> note: currently not supported while we make some architectural changes on the path to leave beta
2022-07-12 23:15:48 +00:00
```sh
git clone https://github.com/lnbits/lnbits-legend.git
cd lnbits-legend/
2022-07-28 10:19:30 +00:00
# Modern debian distros usually include Nix, however you can install with:
# 'sh <(curl -L https://nixos.org/nix/install) --daemon', or use setup here https://nixos.org/download.html#nix-verify-installation
2022-07-12 23:15:48 +00:00
2022-08-03 12:10:32 +00:00
nix build .#lnbits
2022-07-28 10:19:30 +00:00
mkdir data
2022-07-12 23:15:48 +00:00
2022-07-28 10:19:30 +00:00
```
2022-07-12 23:15:48 +00:00
#### Running the server
2022-07-28 10:19:30 +00:00
2022-07-12 23:15:48 +00:00
```sh
2022-07-28 10:19:30 +00:00
# .env variables are currently passed when running
LNBITS_DATA_FOLDER=data LNBITS_BACKEND_WALLET_CLASS=LNbitsWallet LNBITS_ENDPOINT=https://legend.lnbits.com LNBITS_KEY=7b1a78d6c78f48b09a202f2dcb2d22eb ./result/bin/lnbits --port 9000
2022-07-12 23:15:48 +00:00
```
2022-07-27 14:01:23 +00:00
## Option 3: venv
2022-07-15 07:18:07 +00:00
```sh
git clone https://github.com/lnbits/lnbits-legend.git
cd lnbits-legend/
# ensure you have virtualenv installed, on debian/ubuntu 'apt install python3-venv'
python3 -m venv venv
# If you have problems here, try `sudo apt install -y pkg-config libpq-dev`
./venv/bin/pip install -r requirements.txt
# create the data folder and the .env file
mkdir data & & cp .env.example .env
2022-07-28 13:26:43 +00:00
# build the static files
./venv/bin/python build.py
2022-07-15 07:18:07 +00:00
```
2022-07-31 09:22:29 +00:00
#### Running the server
```sh
./venv/bin/uvicorn lnbits.__main__:app --port 5000
```
2022-08-03 12:10:32 +00:00
If you want to host LNbits on the internet, run with the option `--host 0.0.0.0` .
2022-07-31 09:22:29 +00:00
2022-07-30 15:09:14 +00:00
## Option 4: Docker
```sh
git clone https://github.com/lnbits/lnbits-legend.git
cd lnbits-legend
docker build -t lnbits-legend .
cp .env.example .env
mkdir data
docker run --detach --publish 5000:5000 --name lnbits-legend --volume ${PWD}/.env:/app/.env --volume ${PWD}/data/:/app/data lnbits-legend
```
2022-08-19 22:29:32 +00:00
## Option 5: Fly.io
Fly.io is a docker container hosting platform that has a generous free tier. You can host LNBits for free on Fly.io for personal use.
2022-08-19 22:33:17 +00:00
First, sign up for an account at [Fly.io ](https://fly.io ) (no credit card required).
2022-08-19 22:29:32 +00:00
2022-08-21 18:57:28 +00:00
Then, install the Fly.io CLI onto your device [here ](https://fly.io/docs/getting-started/installing-flyctl/ ).
2022-08-19 22:29:32 +00:00
2022-08-21 18:57:28 +00:00
After install is complete, the command will output a command you should copy/paste/run to get `fly` into your `$PATH` . Something like:
```
flyctl was installed successfully to /home/ubuntu/.fly/bin/flyctl
Manually add the directory to your $HOME/.bash_profile (or similar)
export FLYCTL_INSTALL="/home/ubuntu/.fly"
export PATH="$FLYCTL_INSTALL/bin:$PATH"
```
You can either run those commands, then `source ~/.bash_profile` or, if you don't, you'll have to call Fly from `~/.fly/bin/flyctl` .
Once installed, run the following commands.
2022-08-19 22:29:32 +00:00
```
git clone https://github.com/lnbits/lnbits-legend.git
cd lnbits-legend
fly auth login
[complete login process]
2022-08-21 18:57:28 +00:00
fly launch
2022-08-19 22:29:32 +00:00
```
2022-08-21 18:57:28 +00:00
You'll be prompted to enter an app name, region, postgres (choose no), deploy now (choose no).
You'll now find a file in the directory called `fly.toml` . Open that file and modify/add the following settings.
2022-08-19 22:29:32 +00:00
2022-08-21 18:57:28 +00:00
Note: Be sure to replace `${PUT_YOUR_LNBITS_ENV_VARS_HERE}` with all relevant environment variables in `.env` or `.env.example` . Environment variable strings should be quoted here, so if in `.env` you have `LNBITS_ENDPOINT=https://legend.lnbits.com` in `fly.toml` you should have `LNBITS_ENDPOINT="https://legend.lnbits.com"` .
2022-08-21 17:46:11 +00:00
2022-08-19 22:29:32 +00:00
Note: Don't enter secret environment variables here. Fly.io offers secrets (via the `fly secrets` command) that are exposed as environment variables in your runtime. So, for example, if using the LND_REST funding source, you can run `fly secrets set LND_REST_MACAROON=<hex_macaroon_data>` .
```
2022-08-21 18:57:28 +00:00
...
2022-08-19 22:29:32 +00:00
kill_timeout = 30
2022-08-21 18:57:28 +00:00
...
2022-08-19 22:29:32 +00:00
2022-08-21 18:57:28 +00:00
...
2022-08-19 22:29:32 +00:00
[mounts]
2022-08-21 17:46:11 +00:00
source="lnbits_data"
destination="/data"
2022-08-21 18:57:28 +00:00
...
2022-08-19 22:29:32 +00:00
2022-08-21 18:57:28 +00:00
...
2022-08-19 22:29:32 +00:00
[env]
HOST="127.0.0.1"
PORT=5000
LNBITS_FORCE_HTTPS=true
2022-10-26 11:02:35 +00:00
FORWARDED_ALLOW_IPS="*"
2022-08-21 17:39:34 +00:00
LNBITS_DATA_FOLDER="/data"
2022-08-19 22:29:32 +00:00
${PUT_YOUR_LNBITS_ENV_VARS_HERE}
2022-08-21 18:57:28 +00:00
...
2022-08-19 22:29:32 +00:00
2022-08-21 18:57:28 +00:00
...
2022-08-19 22:29:32 +00:00
[[services]]
internal_port = 5000
2022-08-21 18:57:28 +00:00
...
2022-08-19 22:29:32 +00:00
```
2022-08-21 18:57:28 +00:00
Next, create a volume to store the sqlite database for LNBits. Be sure to choose the same region for the volume that you chose earlier.
2022-08-19 22:29:32 +00:00
```
fly volumes create lnbits_data --size 1
```
2022-08-21 18:57:28 +00:00
You're ready to deploy! Run `fly deploy` and follow the steps to finish deployment. You'll select a `region` (up to you, choose the same as you did for the storage volume previously created), `postgres` (choose no), `deploy` (choose yes).
2022-08-19 22:29:32 +00:00
You can use `fly logs` to view the application logs, or `fly ssh console` to get a ssh shell in the running container.
2022-07-12 23:15:48 +00:00
### Troubleshooting
2022-08-03 12:10:32 +00:00
Problems installing? These commands have helped us install LNbits.
2022-07-12 23:15:48 +00:00
```sh
2022-07-23 08:39:58 +00:00
sudo apt install pkg-config libffi-dev libpq-dev
2022-07-12 23:15:48 +00:00
2022-09-20 12:08:45 +00:00
# build essentials for debian/ubuntu
sudo apt install python3.9-dev gcc build-essential
2022-07-12 23:15:48 +00:00
# if the secp256k1 build fails:
2022-07-28 10:43:31 +00:00
# if you used poetry
2022-08-03 12:10:32 +00:00
poetry add setuptools wheel
2022-09-20 12:08:45 +00:00
# if you used venv
./venv/bin/pip install setuptools wheel
2022-07-12 23:15:48 +00:00
```
### Optional: PostgreSQL database
If you want to use LNbits at scale, we recommend using PostgreSQL as the backend database. Install Postgres and setup a database for LNbits:
2022-01-27 20:07:47 +00:00
2021-11-12 11:38:07 +00:00
```sh
# on debian/ubuntu 'sudo apt-get -y install postgresql'
# or follow instructions at https://www.postgresql.org/download/linux/
# Postgres doesn't have a default password, so we'll create one.
2021-11-12 11:44:44 +00:00
sudo -i -u postgres
psql
2021-11-12 11:38:07 +00:00
# on psql
ALTER USER postgres PASSWORD 'myPassword'; # choose whatever password you want
\q
2021-11-12 11:44:44 +00:00
# on postgres user
createdb lnbits
2021-11-12 11:38:07 +00:00
exit
```
2020-09-03 17:53:22 +00:00
2022-07-12 23:15:48 +00:00
You need to edit the `.env` file.
2020-09-03 17:53:22 +00:00
```sh
2021-11-12 11:38:07 +00:00
# add the database connection string to .env 'nano .env' LNBITS_DATABASE_URL=
2022-11-07 10:30:18 +00:00
# postgres://<user>:<myPassword>@<host>:<port>/<lnbits> - alter line bellow with your user, password and db name
LNBITS_DATABASE_URL="postgres://postgres:postgres@localhost:5432/lnbits"
2021-11-12 11:38:07 +00:00
# save and exit
2020-09-03 17:53:22 +00:00
```
2022-07-12 23:15:48 +00:00
# Using LNbits
2022-08-03 12:10:32 +00:00
Now you can visit your LNbits at http://localhost:5000/.
2020-09-03 17:53:22 +00:00
2022-08-03 12:10:32 +00:00
Now modify the `.env` file with any settings you prefer and add a proper [funding source ](./wallets.md ) by modifying the value of `LNBITS_BACKEND_WALLET_CLASS` and providing the extra information and credentials related to the chosen funding source.
2020-09-03 17:53:22 +00:00
2021-06-21 02:33:29 +00:00
Then you can restart it and it will be using the new settings.
2020-09-03 17:53:22 +00:00
2022-08-03 12:10:32 +00:00
You might also need to install additional packages or perform additional setup steps, depending on the chosen backend. See [the short guide ](./wallets.md ) on each different funding source.
2022-07-12 23:15:48 +00:00
Take a look at [Polar ](https://lightningpolar.com/ ) for an excellent way of spinning up a Lightning Network dev environment.
2021-03-19 11:27:41 +00:00
2021-11-18 11:33:26 +00:00
2022-07-12 23:15:48 +00:00
# Additional guides
2022-07-21 12:01:16 +00:00
## SQLite to PostgreSQL migration
2022-07-12 23:15:48 +00:00
If you already have LNbits installed and running, on an SQLite database, we **highly** recommend you migrate to postgres if you are planning to run LNbits on scale.
There's a script included that can do the migration easy. You should have Postgres already installed and there should be a password for the user (see Postgres install guide above). Additionally, your LNbits instance should run once on postgres to implement the database schema before the migration works:
2022-01-27 20:07:47 +00:00
2021-11-18 11:33:26 +00:00
```sh
# STOP LNbits
# add the database connection string to .env 'nano .env' LNBITS_DATABASE_URL=
# postgres://<user>:<password>@<host>/<database> - alter line bellow with your user, password and db name
LNBITS_DATABASE_URL="postgres://postgres:postgres@localhost/lnbits"
# save and exit
2022-06-02 06:51:02 +00:00
# START LNbits
# STOP LNbits
2022-08-17 13:42:01 +00:00
poetry run python tools/conv.py
# or
make migration
2021-11-18 11:33:26 +00:00
```
Hopefully, everything works and get migrated... Launch LNbits again and check if everything is working properly.
2022-07-21 12:01:16 +00:00
## LNbits as a systemd service
2022-01-27 20:07:47 +00:00
Systemd is great for taking care of your LNbits instance. It will start it on boot and restart it in case it crashes. If you want to run LNbits as a systemd service on your Debian/Ubuntu/Raspbian server, create a file at `/etc/systemd/system/lnbits.service` with the following content:
```
# Systemd unit for lnbits
# /etc/systemd/system/lnbits.service
[Unit]
Description=LNbits
2022-06-01 11:25:37 +00:00
# you can uncomment these lines if you know what you're doing
# it will make sure that lnbits starts after lnd (replace with your own backend service)
2022-08-03 12:10:32 +00:00
#Wants=lnd.service
#After=lnd.service
2022-01-27 20:07:47 +00:00
[Service]
2022-06-01 11:25:37 +00:00
# replace with the absolute path of your lnbits installation
2022-08-17 12:25:19 +00:00
WorkingDirectory=/home/lnbits/lnbits-legend
# same here. run `which poetry` if you can't find the poetry binary
ExecStart=/home/lnbits/.local/bin/poetry run lnbits
2022-06-01 11:25:37 +00:00
# replace with the user that you're running lnbits on
2022-08-17 12:25:19 +00:00
User=lnbits
2022-01-27 20:07:47 +00:00
Restart=always
TimeoutSec=120
RestartSec=30
2022-08-03 12:10:32 +00:00
Environment=PYTHONUNBUFFERED=1
2022-01-27 20:07:47 +00:00
[Install]
WantedBy=multi-user.target
```
Save the file and run the following commands:
```sh
sudo systemctl enable lnbits.service
sudo systemctl start lnbits.service
```
2022-09-29 09:37:16 +00:00
## Reverse proxy with automatic https using Caddy
2022-09-29 09:34:03 +00:00
2022-09-29 09:34:53 +00:00
Use Caddy to make your LNbits install accessible over clearnet with a domain and https cert.
2022-09-29 09:31:10 +00:00
Point your domain at the IP of the server you're running LNbits on, by making an `A` record.
Install Caddy on the server
https://caddyserver.com/docs/install#debian-ubuntu-raspbian
```
sudo caddy stop
```
Create a Caddyfile
```
sudo nano Caddyfile
```
2022-09-29 09:37:16 +00:00
Assuming your LNbits is running on port `5000` add:
2022-09-29 09:31:10 +00:00
```
yourdomain.com {
handle /api/v1/payments/sse* {
reverse_proxy 0.0.0.0:5000 {
2022-09-29 09:45:09 +00:00
header_up X-Forwarded-Host yourdomain.com
2022-09-29 09:31:10 +00:00
transport http {
keepalive off
compression off
}
}
}
reverse_proxy 0.0.0.0:5000 {
2022-09-29 09:45:09 +00:00
header_up X-Forwarded-Host yourdomain.com
2022-09-29 09:31:10 +00:00
}
}
```
Save and exit `CTRL + x`
```
sudo caddy start
```
2022-01-27 20:07:47 +00:00
2022-08-13 12:41:23 +00:00
## Running behind an apache2 reverse proxy over https
Install apache2 and enable apache2 mods
```sh
apt-get install apache2 certbot
a2enmod headers ssl proxy proxy-http
```
create a ssl certificate with letsencrypt
```sh
certbot certonly --webroot --agree-tos --text --non-interactive --webroot-path /var/www/html -d lnbits.org
```
create a apache2 vhost at: /etc/apache2/sites-enabled/lnbits.conf
```sh
cat < < EOF > /etc/apache2/sites-enabled/lnbits.conf
< VirtualHost * :443 >
ServerName lnbits.org
SSLEngine On
SSLProxyEngine On
SSLCertificateFile /etc/letsencrypt/live/lnbits.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/lnbits.org/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
LogLevel info
ErrorLog /var/log/apache2/lnbits.log
CustomLog /var/log/apache2/lnbits-access.log combined
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
RequestHeader set "X-Forwarded-SSL" expr=%{HTTPS}
ProxyPreserveHost On
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
< Proxy * >
Order deny,allow
Allow from all
< / Proxy >
< / VirtualHost >
EOF
```
restart apache2
```sh
service restart apache2
```
2022-07-21 12:01:16 +00:00
## Using https without reverse proxy
2022-08-03 12:10:32 +00:00
The most common way of using LNbits via https is to use a reverse proxy such as Caddy, nginx, or ngriok. However, you can also run LNbits via https without additional software. This is useful for development purposes or if you want to use LNbits in your local network.
2022-07-21 12:01:16 +00:00
We have to create a self-signed certificate using `mkcert` . Note that this certiciate is not "trusted" by most browsers but that's fine (since you know that you have created it) and encryption is always better than clear text.
#### Install mkcert
2022-08-03 12:10:32 +00:00
You can find the install instructions for `mkcert` [here ](https://github.com/FiloSottile/mkcert ).
2022-07-21 12:01:16 +00:00
Install mkcert on Ubuntu:
```sh
sudo apt install libnss3-tools
curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64"
chmod +x mkcert-v*-linux-amd64
sudo cp mkcert-v*-linux-amd64 /usr/local/bin/mkcert
```
#### Create certificate
2022-08-10 08:32:43 +00:00
To create a certificate, first `cd` into your LNbits folder and execute the following command on Linux:
```sh
openssl req -new -newkey rsa:4096 -x509 -sha256 -days 3650 -nodes -out cert.pem -keyout key.pem
```
2022-08-13 12:41:23 +00:00
This will create two new files (`key.pem` and `cert.pem ` ).
2022-08-10 08:32:43 +00:00
Alternatively, you can use mkcert ([more info](https://kifarunix.com/how-to-create-self-signed-ssl-certificate-with-mkcert-on-ubuntu-18-04/)):
2022-07-21 12:01:16 +00:00
```sh
# add your local IP (192.x.x.x) as well if you want to use it in your local network
2022-08-03 12:10:32 +00:00
mkcert localhost 127.0.0.1 ::1
2022-07-21 12:01:16 +00:00
```
2022-08-10 08:32:43 +00:00
You can then pass the certificate files to uvicorn when you start LNbits:
2022-07-21 12:01:16 +00:00
```sh
2022-08-10 08:32:43 +00:00
./venv/bin/uvicorn lnbits.__main__:app --host 0.0.0.0 --port 5000 --ssl-keyfile ./key.pem --ssl-certfile ./cert.pem
2022-07-21 12:01:16 +00:00
```
## LNbits running on Umbrel behind Tor
2021-07-07 09:58:21 +00:00
If you want to run LNbits on your Umbrel but want it to be reached through clearnet, _Uxellodunum_ made an extensive [guide ](https://community.getumbrel.com/t/guide-lnbits-without-tor/604 ) on how to do it.
2022-07-21 12:01:16 +00:00
## Docker installation
2021-03-19 11:27:41 +00:00
To install using docker you first need to build the docker image as:
2021-07-07 09:58:21 +00:00
2021-03-19 11:27:41 +00:00
```
2022-08-04 06:05:51 +00:00
git clone https://github.com/lnbits/lnbits-legend.git
cd lnbits-legend
docker build -t lnbits-legend .
2021-03-19 11:27:41 +00:00
```
You can launch the docker in a different directory, but make sure to copy `.env.example` from lnbits there
2021-07-07 09:58:21 +00:00
2021-03-19 11:27:41 +00:00
```
cp < lnbits_repo > /.env.example .env
```
2021-07-07 09:58:21 +00:00
2021-03-19 11:27:41 +00:00
and change the configuration in `.env` as required.
2022-08-04 06:05:51 +00:00
Then create the data directory
2021-03-19 11:27:41 +00:00
```
mkdir data
```
Then the image can be run as:
2021-07-07 09:58:21 +00:00
2021-03-19 11:27:41 +00:00
```
2022-08-04 06:05:51 +00:00
docker run --detach --publish 5000:5000 --name lnbits-legend -e "LNBITS_BACKEND_WALLET_CLASS='FakeWallet'" --volume ${PWD}/.env:/app/.env --volume ${PWD}/data/:/app/data lnbits-legend
2021-06-19 01:41:30 +00:00
```
2021-07-07 09:58:21 +00:00
2021-06-21 02:33:29 +00:00
Finally you can access your lnbits on your machine at port 5000.