Skip to content

Instantly share code, notes, and snippets.

@jefferys
Created January 8, 2024 19:52
Show Gist options
  • Save jefferys/457cf5f67edd387380de6bbe55ca8cdd to your computer and use it in GitHub Desktop.
Save jefferys/457cf5f67edd387380de6bbe55ca8cdd to your computer and use it in GitHub Desktop.
Micromamba local self-contained environment setup using Slurm if avaialble
#!/usr/bin/env bash
#SBATCH --job-name mambaEnv
#SBATCH --cpus-per-task 6
#SBATCH --mem 16G
#SBATCH --partition allnodes
#SBATCH --output ./mambaEnv.%j.out
#SBATCH --error ./mambaEnv.%j.err
# yaml filename is required argument 1
if [ "$#" -ne 1 ]; then
echo "Error: must specify yaml file as the first argument."
exit 1
fi
yaml="$1"
# "yaml" must exist
if [ ! -f "$yaml" ]; then
echo "Error: File $yaml does not exist."
exit 1
fi
# Get path to a "micromamba" directory associated with yaml file
baseDir=$(dirname "$yaml")
baseDir=$(cd "$baseDir" && pwd)
if [ ! -d "$baseDir" ]; then
echo "Error: could not identify the base diectory for the file $yaml"
exit 1
fi
# Set prefix
export MAMBA_ROOT_PREFIX="${baseDir}/micromamba"
# Install mamba if needed
if [ ! -d "$MAMBA_ROOT_PREFIX" ]; then
mkdir "$MAMBA_ROOT_PREFIX"
curl -La https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj -C "$MAMBA_ROOT_PREFIX"
fi
# Create environment from yaml
# TODO - test to see if environment already exists? How do I get the env name
# to check for? Should that be a parameter? Should we default to the yaml file
# name, sans "[.][^.]$" terminal extenstion?
command=$("$MAMBA_ROOT_PREFIX/bin/micromamba" shell hook -s bash)
eval "$command"
micromamba create --yes -f "$yaml"
# List environments available
micromamba env list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment