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

Automatically install latest julia version #2046

Merged
merged 16 commits into from
Dec 4, 2023
2 changes: 2 additions & 0 deletions docs/using/selecting.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ It contains:
- Package managers
- [conda](https://github.com/conda/conda): "cross-platform, language-agnostic binary package manager".
- [mamba](https://github.com/mamba-org/mamba): "reimplementation of the conda package manager in C++". We use this package manager by default when installing packages.
- [plumbum](https://github.com/tomerfiliba/plumbum): "small yet feature-rich library for shell script-like programs in Python".
- Unprivileged user `jovyan` (`uid=1000`, configurable, [see options in the common features section](./common.md) of this documentation) in group `users` (`gid=100`)
with ownership over the `/home/jovyan` and `/opt/conda` paths
- `tini` as the container entry point
Expand Down Expand Up @@ -81,6 +82,7 @@ It contains:
- Common useful utilities like
[curl](https://curl.se),
[git](https://git-scm.com/),
[jq](https://jqlang.github.io/jq/),
[nano](https://www.nano-editor.org/) (actually `nano-tiny`),
[tzdata](https://www.iana.org/time-zones),
[unzip](https://code.launchpad.net/ubuntu/+source/unzip),
Expand Down
2 changes: 1 addition & 1 deletion images/datascience-notebook/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ENV JULIA_DEPOT_PATH=/opt/julia \
JULIA_PKGDIR=/opt/julia

# Setup Julia
RUN /opt/setup-scripts/setup-julia.bash
RUN /opt/setup-scripts/setup-julia.py

USER ${NB_UID}

Expand Down
5 changes: 3 additions & 2 deletions images/docker-stacks-foundation/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ RUN mkdir "/home/${NB_USER}/work" && \
# Similar projects using Micromamba:
# - Micromamba-Docker: <https://github.com/mamba-org/micromamba-docker>
# - repo2docker: <https://github.com/jupyterhub/repo2docker>
# Install Python, Mamba, and jupyter_core
# Install Python, Mamba, jupyter_core, and plumbum
# Cleanup temporary files and remove Micromamba
# Correct permissions
# Do all this in a single RUN command to avoid duplicating all of the
Expand All @@ -114,7 +114,8 @@ RUN set -x && \
--yes \
"${PYTHON_SPECIFIER}" \
'mamba' \
'jupyter_core' && \
'jupyter_core' \
'plumbum' && \
rm micromamba && \
# Pin major.minor version of python
mamba list python | grep '^python ' | tr -s ' ' | cut -d ' ' -f 1,2 >> "${CONDA_DIR}/conda-meta/pinned" && \
Expand Down
2 changes: 1 addition & 1 deletion images/julia-notebook/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ENV JULIA_DEPOT_PATH=/opt/julia \
JULIA_PKGDIR=/opt/julia

# Setup Julia
RUN /opt/setup-scripts/setup-julia.bash
RUN /opt/setup-scripts/setup-julia.py

USER ${NB_UID}

Expand Down
1 change: 1 addition & 0 deletions images/minimal-notebook/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RUN apt-get update --yes && \
# Common useful utilities
curl \
git \
jq \
nano-tiny \
tzdata \
unzip \
Expand Down
35 changes: 17 additions & 18 deletions images/minimal-notebook/setup-scripts/setup-julia.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@ set -exuo pipefail
# - Run as the root user
# - The JULIA_PKGDIR environment variable is set

# Default julia version to install if env var is not set
# Check https://julialang.org/downloads/
JULIA_VERSION="${JULIA_VERSION:-1.9.3}"
# Get the last stable version of Julia
# https://github.com/JuliaLang/www.julialang.org/issues/878#issuecomment-749234813

# Figure out what architecture we are installing in
JULIA_ARCH=$(uname -m)
JULIA_SHORT_ARCH="${JULIA_ARCH}"
if [ "${JULIA_SHORT_ARCH}" == "x86_64" ]; then
JULIA_SHORT_ARCH="x64"
fi

# Figure out Julia Installer URL
JULIA_INSTALLER="julia-${JULIA_VERSION}-linux-${JULIA_ARCH}.tar.gz"
JULIA_MAJOR_MINOR=$(echo "${JULIA_VERSION}" | cut -d. -f 1,2)
JULIA_LATEST_INFO=$(
wget -O - -o /dev/null 'https://julialang-s3.julialang.org/bin/versions.json' |
jq '
with_entries(select(.value.stable==true)) |
to_entries |
max_by(.key).value.files |
.[]
' |
jq "select(.triplet == \"$(uname -m)-linux-gnu\")"
)
JULIA_INSTALLER_URL=$(echo "${JULIA_LATEST_INFO}" | jq --raw-output ".url")
JULIA_VERSION=$(echo "${JULIA_LATEST_INFO}" | jq --raw-output ".version")

# Download and install Julia
cd /tmp
mkdir "/opt/julia-${JULIA_VERSION}"
curl --progress-bar --location --output "${JULIA_INSTALLER}" \
"https://julialang-s3.julialang.org/bin/linux/${JULIA_SHORT_ARCH}/${JULIA_MAJOR_MINOR}/${JULIA_INSTALLER}"
tar xzf "${JULIA_INSTALLER}" -C "/opt/julia-${JULIA_VERSION}" --strip-components=1
rm "${JULIA_INSTALLER}"
curl --progress-bar --location --output /tmp/julia.tar.gz "${JULIA_INSTALLER_URL}"
tar xzf /tmp/julia.tar.gz -C "/opt/julia-${JULIA_VERSION}" --strip-components=1
rm /tmp/julia.tar.gz

# Link Julia installed version to /usr/local/bin, so julia launches it
ln -fs /opt/julia-*/bin/julia /usr/local/bin/julia
Expand Down
75 changes: 75 additions & 0 deletions images/minimal-notebook/setup-scripts/setup-julia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python3
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

# Requirements:
# - Run as the root user
# - The JULIA_PKGDIR environment variable is set

import os
import platform
import shutil
from pathlib import Path

import plumbum
import requests

curl = plumbum.local["curl"]
ln = plumbum.local["ln"]
chown = plumbum.local["chown"]
fix_permissions = plumbum.local["fix-permissions"]


def unify_aarch64(platform: str) -> str:
mathbunnyru marked this conversation as resolved.
Show resolved Hide resolved
return {
"aarch64": "aarch64",
"arm64": "aarch64", # To support local building on aarch64 Macs
"x86_64": "x86_64",
}[platform]


def get_latest_julia_url() -> tuple[str, str]:
# Get the last stable version of Julia
mathbunnyru marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/JuliaLang/www.julialang.org/issues/878#issuecomment-749234813

versions = requests.get(
"https://julialang-s3.julialang.org/bin/versions.json"
).json()
stable_versions = {k: v for k, v in versions.items() if v["stable"]}
latest_version_files = stable_versions[max(stable_versions)]["files"]
triplet = unify_aarch64(platform.machine()) + "-linux-gnu"
file_info = [vf for vf in latest_version_files if vf["triplet"] == triplet][0]
return file_info["url"], file_info["version"]


def download_julia(julia_url: str) -> None:
tmp_file = Path("/tmp/julia.tar.gz")
curl["--progress-bar", "--location", "--output", tmp_file, julia_url] & plumbum.FG
shutil.unpack_archive(tmp_file, "/opt/")
tmp_file.unlink()


def prepare_julia(julia_version: str) -> None:
# Link Julia installed version to /usr/local/bin, so julia launches it
(
ln["-fs", f"/opt/julia-{julia_version}/bin/julia", "/usr/local/bin/julia"]
& plumbum.FG
)

# Tell Julia where conda libraries are
Path("/etc/julia").mkdir()
Path("/etc/julia/juliarc.jl").write_text(
f'push!(Libdl.DL_LOAD_PATH, "{os.environ["CONDA_DIR"]}/lib")\n'
)

# Create JULIA_PKGDIR, where user libraries are installed
JULIA_PKGDIR = Path(os.environ["JULIA_PKGDIR"])
JULIA_PKGDIR.mkdir()
chown[os.environ["NB_USER"], JULIA_PKGDIR] & plumbum.FG
fix_permissions[JULIA_PKGDIR] & plumbum.FG


if __name__ == "__main__":
julia_url, julia_version = get_latest_julia_url()
download_julia(julia_url=julia_url)
prepare_julia(julia_version=julia_version)