Skip to content

Commit 7f89926

Browse files
authored
Merge pull request #1062 from wolfv/micromamba
bootstrap base env with micromamba
2 parents cbc7699 + 25bc273 commit 7f89926

File tree

7 files changed

+125
-114
lines changed

7 files changed

+125
-114
lines changed

repo2docker/buildpacks/conda/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def get_build_env(self):
5555
("NPM_DIR", "${APP_BASE}/npm"),
5656
("NPM_CONFIG_GLOBALCONFIG", "${NPM_DIR}/npmrc"),
5757
("NB_ENVIRONMENT_FILE", self._nb_environment_file),
58+
("MAMBA_ROOT_PREFIX", "${CONDA_DIR}"),
59+
("MAMBA_EXE", "/tmp/bin/micromamba"),
5860
]
5961
if self._nb_requirements_file:
6062
env.append(("NB_REQUIREMENTS_FILE", self._nb_requirements_file))
@@ -98,7 +100,7 @@ def get_build_scripts(self):
98100
99101
All scripts here should be independent of contents of the repository.
100102
101-
This sets up through `install-miniforge.bash` (found in this directory):
103+
This sets up through `install-base-env.bash` (found in this directory):
102104
103105
- a directory for the conda environment and its ownership by the
104106
notebook user
@@ -115,8 +117,8 @@ def get_build_scripts(self):
115117
"root",
116118
r"""
117119
TIMEFORMAT='time: %3R' \
118-
bash -c 'time /tmp/install-miniforge.bash' && \
119-
rm -rf /tmp/install-miniforge.bash /tmp/env
120+
bash -c 'time /tmp/install-base-env.bash' && \
121+
rm -rf /tmp/install-base-env.bash /tmp/env
120122
""",
121123
),
122124
(
@@ -146,7 +148,7 @@ def get_build_script_files(self):
146148
147149
"""
148150
files = {
149-
"conda/install-miniforge.bash": "/tmp/install-miniforge.bash",
151+
"conda/install-base-env.bash": "/tmp/install-base-env.bash",
150152
"conda/activate-conda.sh": "/etc/profile.d/activate-conda.sh",
151153
}
152154
py_version = self.python_version
@@ -337,9 +339,9 @@ def get_env_scripts(self):
337339
"${NB_USER}",
338340
r"""
339341
TIMEFORMAT='time: %3R' \
340-
bash -c 'time mamba env update -p {0} -f "{1}" && \
341-
time mamba clean --all -f -y && \
342-
mamba list -p {0} \
342+
bash -c 'time ${{MAMBA_EXE}} install -p {0} -f "{1}" && \
343+
time ${{MAMBA_EXE}} clean --all -y && \
344+
${{MAMBA_EXE}} list -p {0} \
343345
'
344346
""".format(
345347
env_prefix, environment_yml
@@ -356,9 +358,9 @@ def get_env_scripts(self):
356358
(
357359
"${NB_USER}",
358360
r"""
359-
mamba install -p {0} r-base{1} r-irkernel=1.2 r-devtools -y && \
360-
mamba clean --all -f -y && \
361-
mamba list -p {0}
361+
${{MAMBA_EXE}} install -p {0} r-base{1} r-irkernel=1.2 r-devtools -y && \
362+
${{MAMBA_EXE}} clean --all -y && \
363+
${{MAMBA_EXE}} list -p {0}
362364
""".format(
363365
env_prefix, r_pin
364366
),
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# enable conda and activate the notebook environment
2-
CONDA_PROFILE="${CONDA_DIR}/etc/profile.d/conda.sh"
2+
/tmp/bin/micromamba shell init -s bash -p ${CONDA_DIR}
3+
export MAMBA_ROOT_PREFIX="/srv/conda"
4+
CONDA_PROFILE="${CONDA_DIR}/etc/profile.d/mamba.sh"
35
test -f $CONDA_PROFILE && . $CONDA_PROFILE
46
if [[ "${KERNEL_PYTHON_PREFIX}" != "${NB_PYTHON_PREFIX}" ]]; then
57
# if the kernel is a separate env, stack them
68
# so both are on PATH, notebook first
7-
conda activate ${KERNEL_PYTHON_PREFIX}
8-
conda activate --stack ${NB_PYTHON_PREFIX}
9+
micromamba activate ${KERNEL_PYTHON_PREFIX}
10+
micromamba activate --stack ${NB_PYTHON_PREFIX}
911

1012
# even though it's second on $PATH
1113
# make sure CONDA_DEFAULT_ENV is the *kernel* env
@@ -14,5 +16,5 @@ if [[ "${KERNEL_PYTHON_PREFIX}" != "${NB_PYTHON_PREFIX}" ]]; then
1416
# which only contains UI when the two are different
1517
export CONDA_DEFAULT_ENV="${KERNEL_PYTHON_PREFIX}"
1618
else
17-
conda activate ${NB_PYTHON_PREFIX}
19+
micromamba activate ${NB_PYTHON_PREFIX}
1820
fi
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
# This downloads and installs a pinned version of micromamba
3+
# and sets up the base environment
4+
set -ex
5+
6+
cd $(dirname $0)
7+
8+
export MAMBA_VERSION=0.19.1
9+
export CONDA_VERSION=4.11.0
10+
11+
URL="https://anaconda.org/conda-forge/micromamba/${MAMBA_VERSION}/download/linux-64/micromamba-${MAMBA_VERSION}-0.tar.bz2"
12+
13+
# make sure we don't do anything funky with user's $HOME
14+
# since this is run as root
15+
unset HOME
16+
mkdir -p ${CONDA_DIR}
17+
18+
time wget -qO- ${URL} | tar -xvj bin/micromamba
19+
chmod 0755 /tmp/bin/micromamba
20+
21+
export MAMBA_ROOT_PREFIX=${CONDA_DIR}
22+
export MAMBA_EXE="/tmp/bin/micromamba"
23+
24+
eval "$(/tmp/bin/micromamba shell hook -p ${CONDA_DIR} -s posix)"
25+
26+
micromamba activate
27+
28+
export PATH="${PWD}/bin:$PATH"
29+
30+
cat <<EOT >> ${CONDA_DIR}/.condarc
31+
channels:
32+
- conda-forge
33+
- defaults
34+
auto_update_conda: false
35+
show_channel_urls: true
36+
update_dependencies: false
37+
# channel_priority: flexible
38+
EOT
39+
40+
micromamba install conda=${CONDA_VERSION} mamba=${MAMBA_VERSION} -y
41+
42+
echo "installing notebook env:"
43+
cat "${NB_ENVIRONMENT_FILE}"
44+
45+
46+
time micromamba create -p ${NB_PYTHON_PREFIX} -f "${NB_ENVIRONMENT_FILE}"
47+
48+
if [[ ! -z "${NB_REQUIREMENTS_FILE:-}" ]]; then
49+
echo "installing pip requirements"
50+
cat "${NB_REQUIREMENTS_FILE}"
51+
${NB_PYTHON_PREFIX}/bin/python -mpip install --no-cache --no-deps -r "${NB_REQUIREMENTS_FILE}"
52+
fi
53+
# empty conda history file,
54+
# which seems to result in some effective pinning of packages in the initial env,
55+
# which we don't intend.
56+
# this file must not be *removed*, however
57+
echo '' > ${NB_PYTHON_PREFIX}/conda-meta/history
58+
59+
if [[ ! -z "${KERNEL_ENVIRONMENT_FILE:-}" ]]; then
60+
# install kernel env and register kernelspec
61+
echo "installing kernel env:"
62+
cat "${KERNEL_ENVIRONMENT_FILE}"
63+
time micromamba create -p ${KERNEL_PYTHON_PREFIX} -f "${KERNEL_ENVIRONMENT_FILE}"
64+
65+
if [[ ! -z "${KERNEL_REQUIREMENTS_FILE:-}" ]]; then
66+
echo "installing pip requirements for kernel"
67+
cat "${KERNEL_REQUIREMENTS_FILE}"
68+
${KERNEL_PYTHON_PREFIX}/bin/python -mpip install --no-cache --no-deps -r "${KERNEL_REQUIREMENTS_FILE}"
69+
fi
70+
71+
${KERNEL_PYTHON_PREFIX}/bin/ipython kernel install --prefix "${NB_PYTHON_PREFIX}"
72+
echo '' > ${KERNEL_PYTHON_PREFIX}/conda-meta/history
73+
micromamba list -p ${KERNEL_PYTHON_PREFIX}
74+
fi
75+
76+
# Clean things out!
77+
time micromamba clean --all -y
78+
79+
# Remove the pip cache created as part of installing micromamba
80+
rm -rf /root/.cache
81+
82+
chown -R $NB_USER:$NB_USER ${CONDA_DIR}
83+
84+
micromamba list -p ${NB_PYTHON_PREFIX}
85+
86+
# Set NPM config
87+
${NB_PYTHON_PREFIX}/bin/npm config --global set prefix ${NPM_DIR}

repo2docker/buildpacks/conda/install-miniforge.bash

Lines changed: 0 additions & 96 deletions
This file was deleted.

tests/conda/binder-dir/verify

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@ from subprocess import check_output
44

55
assert sys.version_info[:2] == (3, 5), sys.version
66

7-
out = check_output(["conda", "--version"]).decode("utf8").strip()
8-
assert out == "conda 4.9.2", out
7+
out = check_output(["/tmp/bin/micromamba", "--version"]).decode("utf8").strip()
8+
assert (
9+
out
10+
== """micromamba: 0.19.1
11+
libmamba: 0.19.1"""
12+
), out
13+
14+
out = check_output(["mamba", "--version"]).decode("utf8").strip()
15+
assert (
16+
out
17+
== """mamba 0.19.1
18+
conda 4.11.0"""
19+
), out
920

1021
import numpy

tests/conda/simple-py2/verify

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ assert sorted(specs) == ["python2", "python3"], specs.keys()
1414
import json
1515
from subprocess import check_output
1616

17-
envs = json.loads(check_output(["conda", "env", "list", "--json"]).decode("utf8"))
17+
envs = json.loads(
18+
check_output(["/tmp/bin/micromamba", "env", "list", "--json"]).decode("utf8")
19+
)
1820
assert envs == {
1921
"envs": ["/srv/conda", "/srv/conda/envs/kernel", "/srv/conda/envs/notebook"]
2022
}, envs
2123

2224
pkgs = json.loads(
23-
check_output(["conda", "list", "-n", "kernel", "--json"]).decode("utf8")
25+
check_output(["/tmp/bin/micromamba", "list", "-n", "kernel", "--json"]).decode(
26+
"utf8"
27+
)
2428
)
2529
pkg_names = [pkg["name"] for pkg in pkgs]
2630
assert "ipykernel" in pkg_names, pkg_names
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dependencies:
2+
- pip
23
- pip:
34
- pypi-pkg-test

0 commit comments

Comments
 (0)