From 71426d200e24785089b98bbdb974961ecabb6c71 Mon Sep 17 00:00:00 2001 From: cmdr2 Date: Wed, 12 Oct 2022 16:38:06 +0530 Subject: [PATCH] 1-click installer using micromamba to install git and python into a contained environment (if necessary) before running the normal installation script --- installer/How to create the installers.md | 51 ++++++++++++++ installer/install.bat | 80 +++++++++++++++++++++ installer/install.sh | 85 +++++++++++++++++++++++ invoke.bat | 29 ++++++++ invoke.sh | 27 +++++++ update.bat | 11 +++ update.sh | 10 +++ 7 files changed, 293 insertions(+) create mode 100644 installer/How to create the installers.md create mode 100644 installer/install.bat create mode 100644 installer/install.sh create mode 100644 invoke.bat create mode 100644 invoke.sh create mode 100644 update.bat create mode 100644 update.sh diff --git a/installer/How to create the installers.md b/installer/How to create the installers.md new file mode 100644 index 0000000000..7089f7929a --- /dev/null +++ b/installer/How to create the installers.md @@ -0,0 +1,51 @@ +The installer zip contains two files: the script, and the micromamba binary. + +Micromamba is a single ~8mb binary file, that acts like a package manager (drop-in replacement for conda). + +# Download micromamba from: +* Windows x64: `curl -Ls https://micro.mamba.pm/api/micromamba/win-64/latest | tar -xvj bin/micromamba_win_x64.exe` + +* Linux x64: `curl -Ls https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj bin/micromamba_linux_x64` +* Linux arm64: `curl -Ls https://micro.mamba.pm/api/micromamba/linux-aarch64/latest | tar -xvj bin/micromamba_linux_arm64` + +* Mac x64: `curl -Ls https://micro.mamba.pm/api/micromamba/osx-64/latest | tar -xvj bin/micromamba_mac_x64` +* Mac arm64 (M1/Apple Silicon): `curl -Ls https://micro.mamba.pm/api/micromamba/osx-arm64/latest | tar -xvj bin/micromamba_mac_arm64` + +The download link provides tar.bz2 files. + +(source https://mamba.readthedocs.io/en/latest/installation.html) + +# Create the installer +Create the following folder structure, and zip it up. + +For Linux/Mac: Make sure the `chmod u+x` permission is granted to `installer.sh` and the corresponding `micromamba` binary. + +### Windows x64: +``` +.\installer.bat +.\installer_files\micromamba_win_x64.exe +``` + +### Linux x64: +``` +.\installer.sh +.\installer_files\micromamba_linux_x64 +``` + +### Linux arm64: +``` +.\installer.sh +.\installer_files\micromamba_linux_arm64 +``` + +### Mac x64: +``` +.\installer.sh +.\installer_files\micromamba_mac_x64 +``` + +### Mac arm64 (M1/Apple Silicon): +``` +.\installer.sh +.\installer_files\micromamba_mac_arm64 +``` \ No newline at end of file diff --git a/installer/install.bat b/installer/install.bat new file mode 100644 index 0000000000..806e6ea745 --- /dev/null +++ b/installer/install.bat @@ -0,0 +1,80 @@ +@echo off + +@rem This script will install git and python (if not found on the PATH variable) +@rem using micromamba (an 8mb static-linked single-file binary, conda replacement). +@rem For users who already have git and python, this step will be skipped. + +@rem Next, it'll checkout the project's git repo, if necessary. +@rem Finally, it'll create the conda environment and preload the models. + +@rem This enables a user to install this project without manually installing python and git. + +@rem prevent the window from closing after an error +if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit ) + +@rem config +set MAMBA_ROOT_PREFIX=%cd%\installer_files\mamba +set INSTALL_ENV_DIR=%cd%\installer_files\env +set MICROMAMBA_BINARY_FILE=%cd%\installer_files\micromamba_win_x64.exe +set PATH=%PATH%;%INSTALL_ENV_DIR%;%INSTALL_ENV_DIR%\Library\bin;%INSTALL_ENV_DIR%\Scripts + +@rem initialize micromamba +if not exist "%MAMBA_ROOT_PREFIX%" ( + mkdir "%MAMBA_ROOT_PREFIX%" + copy "%MICROMAMBA_BINARY_FILE%" "%MAMBA_ROOT_PREFIX%\micromamba.exe" + + @rem test the mamba binary + echo Micromamba version: + call "%MAMBA_ROOT_PREFIX%\micromamba.exe" --version + + @rem run the shell hook, otherwise activate will fail + if not exist "%MAMBA_ROOT_PREFIX%\Scripts" ( + call "%MAMBA_ROOT_PREFIX%\micromamba.exe" shell hook --log-level 4 -s cmd.exe + ) +) + +call "%MAMBA_ROOT_PREFIX%\condabin\mamba_hook.bat" + +@rem figure out whether git and python needs to be installed +set PACKAGES_TO_INSTALL= + +call python --version "" >tmp/stdout.txt 2>tmp/stderr.txt +if "%ERRORLEVEL%" NEQ "0" set PACKAGES_TO_INSTALL=%PACKAGES_TO_INSTALL% python + +call git --version "" >tmp/stdout.txt 2>tmp/stderr.txt +if "%ERRORLEVEL%" NEQ "0" set PACKAGES_TO_INSTALL=%PACKAGES_TO_INSTALL% git + +@rem (if necessary) install git and python into a contained environment +if "%PACKAGES_TO_INSTALL%" NEQ "" ( + echo "Packages to install: %PACKAGES_TO_INSTALL%" + + @rem install git and python into the installer env + if not exist "%INSTALL_ENV_DIR%" ( + call micromamba create -y --prefix "%INSTALL_ENV_DIR%" + ) + + call micromamba install -y --prefix "%INSTALL_ENV_DIR%" -c conda-forge %PACKAGES_TO_INSTALL% + + @rem activate + call micromamba activate "%INSTALL_ENV_DIR%" +) + +@rem get the repo (and load into the current directory) +if not exist ".git" ( + call git init + call git remote add origin https://github.com/cmdr2/InvokeAI.git + call git fetch + call git checkout origin/main -ft +) + +@rem create the environment +call micromamba create -f environment.yml +call micromamba activate invokeai + +@rem preload the models +call python scripts\preload_models.py + +@rem make the models dir +mkdir models\ldm\stable-diffusion-v1 + +pause diff --git a/installer/install.sh b/installer/install.sh new file mode 100644 index 0000000000..569a4fc47a --- /dev/null +++ b/installer/install.sh @@ -0,0 +1,85 @@ +#!/bin/bash + +# This script will install git and python (if not found on the PATH variable) +# using micromamba (an 8mb static-linked single-file binary, conda replacement). +# For users who already have git and python, this step will be skipped. + +# Next, it'll checkout the project's git repo, if necessary. +# Finally, it'll create the conda environment and preload the models. + +# This enables a user to install this project without manually installing python and git. + +OS_NAME=$(uname -s) +case "${OS_NAME}" in + Linux*) OS_NAME="linux";; + Darwin*) OS_NAME="mac";; + *) echo "Unknown OS: $OS_NAME! This script runs only on Linux or Mac" && exit +esac + +OS_ARCH=$(uname -m) +case "${OS_ARCH}" in + x86_64*) OS_ARCH="x64";; + arm64*) OS_ARCH="arm64";; + *) echo "Unknown system architecture: $OS_ARCH! This script runs only on x86_64 or arm64" && exit +esac + +# config +export MAMBA_ROOT_PREFIX="$(pwd)/installer_files/mamba" +INSTALL_ENV_DIR="$(pwd)/installer_files/env" +MICROMAMBA_BINARY_FILE="$(pwd)/installer_files/micromamba_${OS_NAME}_${OS_ARCH}" +if [ -e "$INSTALL_ENV_DIR" ]; then export PATH="$PATH;$INSTALL_ENV_DIR/bin"; fi + +# initialize micromamba +mkdir -p "$MAMBA_ROOT_PREFIX" +cp "$MICROMAMBA_BINARY_FILE" "$MAMBA_ROOT_PREFIX/micromamba" + +# test the mamba binary +echo Micromamba version: +"$MAMBA_ROOT_PREFIX/micromamba" --version + +# run the shell hook, otherwise activate will fail +eval "$($MAMBA_ROOT_PREFIX/micromamba shell hook -s posix)" + +# figure out what needs to be installed +PACKAGES_TO_INSTALL="" + +if ! hash "python" &>/dev/null; then PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL python"; fi +if ! hash "git" &>/dev/null; then PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL git"; fi + +# install git and python into a contained environment (if necessary) +if [ "$PACKAGES_TO_INSTALL" != "" ]; then + echo "Packages to install: $PACKAGES_TO_INSTALL" + + # install git and python into the installer env + if [ ! -e "$INSTALL_ENV_DIR" ]; then + micromamba create -y --prefix "$INSTALL_ENV_DIR" + fi + + micromamba install -y --prefix "$INSTALL_ENV_DIR" -c conda-forge $PACKAGES_TO_INSTALL + + # activate + micromamba activate "$INSTALL_ENV_DIR" +fi + +# get the repo (and load into the current directory) +if [ ! -e ".git" ]; then + git init + git remote add origin https://github.com/cmdr2/InvokeAI.git + git fetch + git checkout origin/main -ft +fi + +# create the environment +if [ "$OS_NAME" == "mac" ]; then + PIP_EXISTS_ACTION=w CONDA_SUBDIR=osx-arm64 micromamba create -f environment-mac.yml +else + micromamba create -f environment.yml +fi + +micromamba activate invokeai + +# preload the models +python scripts/preload_models.py + +# make the models dir +mkdir -p models/ldm/stable-diffusion-v1 diff --git a/invoke.bat b/invoke.bat new file mode 100644 index 0000000000..ac5bcc4500 --- /dev/null +++ b/invoke.bat @@ -0,0 +1,29 @@ +@echo off + +@rem prevent the window from closing after running the commands +if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit ) + +@rem check if conda exists, otherwise use micromamba +set CONDA_COMMAND=conda + +call conda --version "" >tmp/stdout.txt 2>tmp/stderr.txt +if "%ERRORLEVEL%" NEQ "0" set CONDA_COMMAND=micromamba + +@rem initialize micromamba, if using that +if "%CONDA_COMMAND%" EQU "micromamba" ( + set MAMBA_ROOT_PREFIX=%cd%\installer_files\mamba + set INSTALL_ENV_DIR=%cd%\installer_files\env + + if not exist "%MAMBA_ROOT_PREFIX%\condabin" ( + echo "Have you run install.bat?" + exit /b + ) + + call "%MAMBA_ROOT_PREFIX%\condabin\mamba_hook.bat" + + call micromamba activate "%INSTALL_ENV_DIR%" +) + +call %CONDA_COMMAND% activate invokeai + +pause diff --git a/invoke.sh b/invoke.sh new file mode 100644 index 0000000000..e999923f40 --- /dev/null +++ b/invoke.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +if [ "$0" == "bash" ]; then + # check if conda exists, otherwise use micromamba + CONDA_COMMAND="conda" + + if ! hash "conda" &>/dev/null; then CONDA_COMMAND="micromamba"; fi + + # initialize micromamba, if using that + if [ "$CONDA_COMMAND" == "micromamba" ]; then + export MAMBA_ROOT_PREFIX="$(pwd)/installer_files/mamba" + INSTALL_ENV_DIR="$(pwd)/installer_files/env" + + if [ ! -e "$MAMBA_ROOT_PREFIX" ]; then + echo "Have you run install.sh?" + exit + fi + + eval "$($MAMBA_ROOT_PREFIX/micromamba shell hook -s posix)" + + micromamba activate "$INSTALL_ENV_DIR" + ) + + $CONDA_COMMAND activate invokeai +else + bash --init-file invoke.sh +fi diff --git a/update.bat b/update.bat new file mode 100644 index 0000000000..f346dce3e6 --- /dev/null +++ b/update.bat @@ -0,0 +1,11 @@ +@echo off + +set INSTALL_ENV_DIR=%cd%\installer_files\env +set PATH=%PATH%;%INSTALL_ENV_DIR%;%INSTALL_ENV_DIR%\Library\bin;%INSTALL_ENV_DIR%\Scripts + +@rem update the repo +if exist ".git" ( + call git pull +) + +pause \ No newline at end of file diff --git a/update.sh b/update.sh new file mode 100644 index 0000000000..7ec5bfcb8e --- /dev/null +++ b/update.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +INSTALL_ENV_DIR="$(pwd)/installer_files/env" + +if [ -e "$INSTALL_ENV_DIR" ]; then export PATH="$PATH;$INSTALL_ENV_DIR/bin"; fi + +# update the repo +if [ -e ".git" ]; then + git pull +fi