Plex Media Server – Installation & Auto-Update 🚀


This script automates the installation and configuration of Plex Media Server on Debian-based systems. It not only installs Plex, but also configures all required dependencies, enables Plex as a system service, and sets up an automatic update check so your media server stays current without the need for manual intervention. 📽️

The built-in update mechanism checks for new Plex releases every 24 hours. Once a new version is found, it automatically downloads and installs the update. This way, you can focus on enjoying your media instead of worrying about software updates. 🛠️


How It Works

The script performs the following actions:

  • Updates your system package lists and installs necessary dependencies (such as curl, wget, jq, and systemctl support).
  • Downloads the latest Plex Media Server package directly from Plex’s official repository.
  • Installs the downloaded package using dpkg and starts the Plex service immediately.
  • Enables the Plex service at system boot, ensuring Plex is always running after a restart.
  • Creates a daily cron job that checks for new versions of Plex and automatically upgrades if a new version is available.
This comprehensive approach ensures that Plex is properly installed, configured, and maintained with minimal manual effort. 🔄


Plex Media Server Installation Script

Copy the following bash script, save it as /usr/local/sbin/plex_install.sh on your Debian system, and then make it executable. Running this script as root will install Plex and set up the automatic update job.


#!/bin/bash
# plex_install.sh – Install and auto-update Plex Media Server on Debian
#
# Description:
#   - Installs Plex Media Server from the official repository.
#   - Configures Plex to run as a system service.
#   - Sets up a cron job for daily update checks and auto-installation of new releases.
#   - Logs actions to help with troubleshooting.
#
# This script is designed for Debian-based systems. Customize as needed.

###############################
# Configuration
###############################
LOGFILE="/var/log/plex_install.log"
PLEX_URL="https://downloads.plex.tv/plex-media-server-new/"
PLEX_DOWNLOAD_PAGE="https://plex.tv/downloads"
CRON_JOB="/etc/cron.daily/plex_update"

###############################
# Functions
###############################

# Logs messages with a timestamp.
log() {
    echo "$(date '+%F %T') - $1" >> "$LOGFILE"
}

# Updates the system package lists and installs required dependencies.
install_dependencies() {
    log "Updating system and installing dependencies..."
    apt update && apt install -y curl wget systemctl jq
    log "System dependencies installed."
}

# Downloads and installs the latest Plex Media Server package.
download_plex() {
    log "Checking latest Plex version..."
    LATEST_VERSION=$(curl -s $PLEX_DOWNLOAD_PAGE | grep -oP '(?<=PlexMediaServer-\d+\.\d+\.\d+-)\d+' | head -1)
    
    if [ -z "$LATEST_VERSION" ]; then
        log "Failed to determine latest Plex version!"
        exit 1
    fi
    
    PLEX_DEB="plexmediaserver_${LATEST_VERSION}_amd64.deb"
    DOWNLOAD_URL="${PLEX_URL}${LATEST_VERSION}/$PLEX_DEB"
    
    log "Downloading Plex Media Server version: $LATEST_VERSION"
    wget -O "/tmp/$PLEX_DEB" "$DOWNLOAD_URL"
    
    dpkg -i "/tmp/$PLEX_DEB"
    log "Plex Media Server installed."
}

# Enables the Plex service and starts it immediately.
setup_service() {
    systemctl enable plexmediaserver.service
    systemctl start plexmediaserver.service
    log "Plex service started and enabled at boot."
}

# Sets up a daily cron job to automatically check for and install Plex updates.
setup_auto_update() {
    log "Configuring automatic updates..."
    
    cat < "$CRON_JOB"
#!/bin/bash
PLEX_URL="https://downloads.plex.tv/plex-media-server-new/"
PLEX_DOWNLOAD_PAGE="https://plex.tv/downloads"
LOGFILE="/var/log/plex_update.log"

LATEST_VERSION=\$(curl -s \$PLEX_DOWNLOAD_PAGE | grep -oP '(?<=PlexMediaServer-\d+\.\d+\.\d+-)\d+' | head -1)

if [ -z "\$LATEST_VERSION" ]; then
    echo "\$(date '+%F %T') - Failed to check latest Plex version." >> "\$LOGFILE"
    exit 1
fi

CURRENT_VERSION=\$(dpkg -l | grep plexmediaserver | awk '{print \$3}' | cut -d '-' -f1)

if [ "\$LATEST_VERSION" != "\$CURRENT_VERSION" ]; then
    PLEX_DEB="plexmediaserver_\$LATEST_VERSION_amd64.deb"
    DOWNLOAD_URL="\${PLEX_URL}\${LATEST_VERSION}/\$PLEX_DEB"
    
    wget -O "/tmp/\$PLEX_DEB" "\$DOWNLOAD_URL"
    dpkg -i "/tmp/\$PLEX_DEB"
    echo "\$(date '+%F %T') - Plex updated to \$LATEST_VERSION." >> "\$LOGFILE"
else
    echo "\$(date '+%F %T') - No update needed (current: \$CURRENT_VERSION)." >> "\$LOGFILE"
fi
EOF

    chmod +x "$CRON_JOB"
    log "Automatic update mechanism configured."
}

###############################
# Main Execution
###############################

install_dependencies
download_plex
setup_service
setup_auto_update

log "Plex installation and auto-update setup completed successfully."
      

Installation & Usage Instructions

Step 1: Save this script as /usr/local/sbin/plex_install.sh on your Debian system. 📁

Step 2: Make the script executable by running: chmod +x /usr/local/sbin/plex_install.sh 🔧

Step 3: Execute the script as root (or using sudo) with: sudo /usr/local/sbin/plex_install.sh 🚀

Step 4: The script will install Plex Media Server, enable its service at boot, and set up a daily cron job that checks for updates automatically.

Step 5: Check the log files (/var/log/plex_install.log and /var/log/plex_update.log) for details on the installation and any subsequent updates.


With this automated setup, your Plex Media Server will always remain up to date. You can now enjoy your media collection without worrying about manual updates or configuration tasks! 🎬