Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install python into a venv #718

Merged
merged 17 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# News

## 2023-12

### Changes in pre-built images

- For images that ship with a python environment installed, the default python
environment will now be a [virtual environment](https://docs.python.org/3/library/venv.html)
created at `/opt/venv`. ([#718](https://github.com/rocker-org/rocker-versioned2/pull/718))

### Changes in rocker_scripts

- `install_python.sh` will now setup a [virtual environment](https://docs.python.org/3/library/venv.html)
under `/opt/venv`, and this will be the default python environment (accomplished via setting
the `$PATH` and `$VIRTUAL_ENV` variables). `/opt/venv` will be owned by the `staff` group so non-root
users can safely install python packages. ([#718](https://github.com/rocker-org/rocker-versioned2/pull/718))

## 2023-11

### Changes in pre-built images
Expand Down
2 changes: 2 additions & 0 deletions dockerfiles/binder_devel.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ LABEL org.opencontainers.image.licenses="GPL-2.0-or-later" \
org.opencontainers.image.authors="Carl Boettiger <cboettig@ropensci.org>"

ENV NB_USER=rstudio
ENV VIRTUAL_ENV=/opt/venv
eitsupi marked this conversation as resolved.
Show resolved Hide resolved
ENV PATH=${VIRTUAL_ENV}/bin:${PATH}

RUN /rocker_scripts/install_jupyter.sh

Expand Down
5 changes: 3 additions & 2 deletions scripts/install_jupyter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ if ! id -u "${NB_USER}" >/dev/null 2>&1; then
/rocker_scripts/default_user.sh "${NB_USER}"
fi

# install python
/rocker_scripts/install_python.sh
# install python & setup venv
# shellcheck source=/dev/null
source /rocker_scripts/install_python.sh

python3 -m pip install --no-cache-dir jupyter-rsession-proxy notebook jupyterlab jupyterhub

Expand Down
27 changes: 23 additions & 4 deletions scripts/install_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,26 @@ apt_install \
python3-venv \
swig

# Setup a virtualenv to install things into

# Put things under /opt/venv, if nothing else is specified
export VIRTUAL_ENV="${VIRTUAL_ENV:=/opt/venv}"
export PATH="${VIRTUAL_ENV}/bin:${PATH}"

# Make sure that Rstudio sees these env vars too
echo "PATH=${PATH}" >>"${R_HOME}/etc/Renviron.site"
cboettig marked this conversation as resolved.
Show resolved Hide resolved
echo "VIRTUAL_ENV=${VIRTUAL_ENV}" >>"${R_HOME}/etc/Renviron.site"

python3 -m venv "${VIRTUAL_ENV}"

# Upgrade version of pip inside the virtualenv
python3 -m pip --no-cache-dir install --upgrade \
pip

# Some TF tools expect a "python" binary
if [ ! -e /usr/local/bin/python ]; then
ln -s "$(which python3)" /usr/local/bin/python
fi
# Make the venv owned by the staff group, so users can install packages
# without having to be root
chown -R root:staff "${VIRTUAL_ENV}"
chmod g+ws "${VIRTUAL_ENV}"

install2.r --error --skipmissing --skipinstalled -n "$NCPUS" reticulate

Expand All @@ -52,6 +65,12 @@ if [ "${UBUNTU_CODENAME}" == "focal" ]; then
fi
fi

# Check that python and python3 point to correct places
echo "Check python, python3 and pip executables point to the correct place..."
echo "python -> $(which python)"
echo "python3 -> $(which python3)"
echo "pip -> $(which pip)"

# Check Python version
echo -e "Check the Python to use with reticulate...\n"

Expand Down
4 changes: 3 additions & 1 deletion stacks/devel.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@
},
"FROM": "rocker/geospatial:devel",
"ENV": {
"NB_USER": "rstudio"
"NB_USER": "rstudio",
"VIRTUAL_ENV": "/opt/venv",
"PATH": "${VIRTUAL_ENV}/bin:${PATH}"
},
cboettig marked this conversation as resolved.
Show resolved Hide resolved
"RUN": [
"/rocker_scripts/install_jupyter.sh"
Expand Down
Loading