Skip to content

Instantly share code, notes, and snippets.

@peterroelants
Last active January 23, 2021 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterroelants/b8c453217ba2c8bdc0cbb4763ebc6c6a to your computer and use it in GitHub Desktop.
Save peterroelants/b8c453217ba2c8bdc0cbb4763ebc6c6a to your computer and use it in GitHub Desktop.
Install conda, install environment, and export environment with conda-pack. (Intended for Docker containers)
#!/usr/bin/env bash
# Use Bash strict mode
# Exit immediately if command or pipe fails, and treat unset variables as an error.
# More info:
# - https://linuxcommand.org/lc3_man_pages/seth.html
# - http://redsymbol.net/articles/unofficial-bash-strict-mode/
##############################################################################
set -euo pipefail
# vars
##############################################################################
export PYTHON_ENV_PATH=/opt/venv
export TMP_RESOURCE_DIR=/tmp/
export CONDA_ENV_YML=/tmp/conda_env.yml
export CONDA_PATH=/opt/conda
export CONDA_VERSION=py38_4.9.2
export CONDA_SHA256=1314b90489f154602fd794accfc90446111514a5a72fe1f71ab83e07de9504a7
echo "Install conda in '${CONDA_PATH}'"
echo "Using temporary resource directory at '${TMP_RESOURCE_DIR}'"
echo "Export environment in ${PYTHON_ENV_PATH}"
# Install Conda Dependencies
#
# See also:
# - https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#run
# - https://pythonspeed.com/articles/system-packages-docker/
##############################################################################
# Tell apt-get we're never going to be able to give manual feedback
export DEBIAN_FRONTEND=noninteractive
apt-get update -q
apt-get install -qy --no-install-recommends \
ca-certificates \
wget \
tar
# Install Miniconda
#
# For example Conda Docker installs see:
# - https://github.com/ContinuumIO/docker-images/blob/master/miniconda3/debian/Dockerfile
# - https://hub.docker.com/r/continuumio/miniconda3/dockerfile
#
# More info:
# - https://docs.conda.io/projects/conda/en/latest/user-guide/install/linux.html
# - https://docs.conda.io/en/latest/miniconda.html
##############################################################################
. ${TMP_RESOURCE_DIR}/conda_version.env
echo "Install conda version=${CONDA_VERSION}"
export TMP_DOWNLOAD_CONDA=${TMP_RESOURCE_DIR}/miniconda_download
mkdir -p ${TMP_DOWNLOAD_CONDA}
# Download miniconda installer
wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-${CONDA_VERSION}-Linux-x86_64.sh \
-O ${TMP_DOWNLOAD_CONDA}/miniconda.sh
# Check hash of download
if ! echo "${CONDA_SHA256} ${TMP_DOWNLOAD_CONDA}/miniconda.sh" | sha256sum --check --status
then
echo "SHA256 Hash does not match miniconda installer script!"
exit 1
fi
# Run installer
sh ${TMP_DOWNLOAD_CONDA}/miniconda.sh -b -p ${CONDA_PATH}
echo "Conda install finished at '${CONDA_PATH}'"
# Disable conda default in bash && Add `conda` to path
#
# Don't load conda by default. Conda will be properly loaded by the `entrypoint`.
# Disabling this avoids having to run `conda init`, and setting bashrc and profile.d files.
#
##############################################################################
${CONDA_PATH}/bin/conda config --set auto_activate_base false
chmod +x /opt/conda/etc/profile.d/conda.sh
# Source conda to add to current PATH
. /opt/conda/etc/profile.d/conda.sh
echo "Conda version: $(conda --version)"
# Setup Conda environment
#
# We install into the base environment so we can have the base environment active by default and to
# avoid having multiple environments.
#
# More info:
# - https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
##############################################################################
conda env create --quiet --name conda_env --file ${CONDA_ENV_YML}
# Export environment with conda-pack
# - https://conda.github.io/conda-pack/
##############################################################################
# Install conda-pack into base environment
conda activate base
echo "Install conda-pack"
conda install --quiet --yes conda-pack=0.6.0
# Export environment
conda pack --quiet --name conda_env --output conda_env.tar.gz
echo "conda environment exported to '$(realpath ./conda_env.tar.gz)'"
# Disable base conda environment
conda deactivate
# Move environment to final destination
mkdir -p ${PYTHON_ENV_PATH}
tar -xzf conda_env.tar.gz -C ${PYTHON_ENV_PATH}
echo "PYTHON_ENV_PATH=${PYTHON_ENV_PATH}"
# Activate the environment
source ${PYTHON_ENV_PATH}/bin/activate
# Cleanup prefixes from in the active environment.
conda-unpack
rm -rf $(which conda-unpack)
# Test environment
echo "Python binary at '$(which python)' with version: $(python --version)"
echo "Prefect at '$(which prefect)' with version: $(prefect version)"
echo "Dask version: $(python -c 'import dask; print(dask.__version__)')"
# Cleanup
conda clean --all --force-pkgs-dirs --quiet --yes
find ${PYTHON_ENV_PATH} -follow -type f -name '*.a' -delete
find ${PYTHON_ENV_PATH} -follow -type f -name '*.js.map' -delete
find ${PYTHON_ENV_PATH} -follow -type f -name '*.py[co]' -delete
find ${PYTHON_ENV_PATH} -follow -type d -name '__pycache__' -delete
rm -rf ~/.cache/pip/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment