How to Update Self-Host n8n in 1 Click
I’ve wrote a blog recently about How to Update n8n in Docker in a Few Simple Steps. However, I am tired of updating n8n manually using those steps. So, I decided to make a script to update n8n in just 1 simple click.
In this blog, I’ll show you how to create an upgrade script and update your self-host n8n in just 1 click.
Create your upgrade script
This is a script created in Ubuntu OS. You might need to modify the script on other Linux OS.
sudo nano update-n8n.sh
Once you’re inside update-n8n.sh, paste the below commands inside of the script file:
#!/usr/bin/env bash
# Update/recreate n8n Docker container: stop -> remove -> delete old image -> pull fresh -> run
set -euo pipefail
CONTAINER_NAME="n8n"
IMAGE_REPO="docker.n8n.io/n8nio/n8n"
IMAGE_TAG="latest"
PORT_MAPPING="5678:5678"
VOLUME_NAME="n8n_data"
# ---- Environment (customize these) ----
WEBHOOK_URL="your domain name"
GENERIC_TIMEZONE="Australia/Melbourne"
N8N_PUSH_BACKEND="websocket"
# --------------------------------------
# Stop container if running
if docker ps --format '{{.Names}}' | grep -qx "${CONTAINER_NAME}"; then
echo "==> Stopping running container ${CONTAINER_NAME}..."
docker stop "${CONTAINER_NAME}"
else
echo "==> No running container named ${CONTAINER_NAME}."
fi
# Remove container if exists (stopped or exited)
if docker ps -a --format '{{.Names}}' | grep -qx "${CONTAINER_NAME}"; then
echo "==> Removing existing container ${CONTAINER_NAME}..."
docker rm "${CONTAINER_NAME}"
else
echo "==> No existing container named ${CONTAINER_NAME} to remove."
fi
# Delete any local images that match repo:tag BEFORE pulling
OLD_IMG_IDS=$(docker image ls --format '{{.Repository}} {{.Tag}} {{.ID}}' \
| awk -v repo="${IMAGE_REPO}" -v tag="${IMAGE_TAG}" '$1==repo && $2==tag {print $3}')
if [[ -n "${OLD_IMG_IDS}" ]]; then
echo "==> Removing old image(s):"
for id in ${OLD_IMG_IDS}; do
echo " - ${id}"
docker image rmi "${id}" || true
done
else
echo "==> No local ${IMAGE_REPO}:${IMAGE_TAG} image to remove."
fi
echo "==> Pulling fresh image ${IMAGE_REPO}:${IMAGE_TAG}..."
docker pull "${IMAGE_REPO}:${IMAGE_TAG}"
# Ensure named volume exists
if ! docker volume ls --format '{{.Name}}' | grep -qx "${VOLUME_NAME}"; then
echo "==> Creating volume ${VOLUME_NAME}..."
docker volume create "${VOLUME_NAME}" >/dev/null
fi
echo "==> Starting new ${CONTAINER_NAME} container..."
docker run -d \
--name "${CONTAINER_NAME}" \
-p "${PORT_MAPPING}" \
-v "${VOLUME_NAME}:/home/node/.n8n" \
-e "WEBHOOK_URL=${WEBHOOK_URL}" \
-e "N8N_PUSH_BACKEND=${N8N_PUSH_BACKEND}" \
-e "GENERIC_TIMEZONE=${GENERIC_TIMEZONE}" \
"${IMAGE_REPO}:${IMAGE_TAG}"
echo "==> Done. Current container status:"
docker ps --filter "name=${CONTAINER_NAME}"
Then press Ctrl + X and Y to save and exit.
Make your script executable
Run this command to make your script executable
sudo chmod +x update-n8n.sh
Execute and Upgrade your n8n
Run the script
sudo ./update-n8n.sh
Bravo
Your self-host n8n is now up-to-date