The Nextjournal Julia Environment

Default environment for Julia 1.x

This notebook builds a reusable environment for Julia runtimes, based on the minimal Bash environment. Nextjournal's Julia environment runs version

"1.3.0"
, installed from the generic Linux binary packages.

Learn more about environments on Nextjournal.

You can quickly make use of the Julia 1.2 environment by remixing the Nextjournal Julia template, or use these environments with any existing runtime by following these steps (see image below):

  • Activate the runtime settings in the sidebar.

  • Bring up the Environments dropdown.

  • Select Import environment… at the bottom of the list.

  • Search for this notebook, nextjournal/julia-environment.

  • Select it to list all environments within.

  • Select the desired environment.

Showcase

These packages are included in Nextjournal's Julia 1.2 environment.

]st
1.0s
Julia Test (Julia)
Julia

System Packages and Basics

A wide variety of support libraries are installed, as well as gcc v7 and ImageMagick.

Version

"1.0.10"
of the SoftGlobalScope package is available—this adjusts scope-handling to be more like Julia 0.6, providing a more REPL-like interface.

Julia packages are installed normally, using Pkg in a Julia cell. Please refer to the Julia section of Installing Software and Packages for more detailed information.

Plotting

The default environment comes with GR v

"0.44.0"
, PlotlyBase
"0.3.0"
, and PlotlyJS
"0.13.0"
, as well as version
"0.27.1"
of the Plots framework. Makie version
"0.9.10"
is also installed.

Plots

The JuliaPlots framework provides a unified interface to multiple graphical backends. The default backend is GR, which efficiently generates static plots and animations.

using Plots; gr()
# define the Lorenz attractor
mutable struct Lorenz
    dt; σ; ρ; β; x; y; z
end
function step!(l::Lorenz)
    dx = l.σ*(l.y - l.x)       ; l.x += l.dt * dx
    dy = l.x*(l.ρ - l.z) - l.y ; l.y += l.dt * dy
    dz = l.x*l.y - l.β*l.z     ; l.z += l.dt * dz
end
attractor = 
  Lorenz((dt = 0.02, σ = 10., ρ = 28., β = 8//3, x = 1., y = 1., z = 1.)...)
# initialize a 3D plot with 1 empty series
plt = plot3d(1, xlim=(-25,25), ylim=(-25,25), zlim=(0,50),
             title = "Lorenz Attractor", marker = 2)
# build an animated gif by pushing new points to the plot, saving every 10th frame
@gif for i=1:1500
    step!(attractor)
    push!(plt, attractor.x, attractor.y, attractor.z)
end every 10
38.4s
Julia Test (Julia)
Julia

Switching to the Plotly backend adds some interactivity to the output.

plotly(lw=3)
x = -100:100
Plots.plot(x, 100x.^2)
Plots.plot!(x, x.^3 - x.^2)
11.1s
Julia Test (Julia)
Julia

Makie

The WGLMakie package can generate beautiful 3D visualizations.

using WGLMakie, AbstractPlotting, LinearAlgebra
n = 20
set_theme!(resolution = (600, 500))
f   = (x,y,z) -> x*exp(cos(y)*z)
∇f  = (x,y,z) -> Point3f0(exp(cos(y)*z), -sin(y)*z*x*exp(cos(y)*z), x*cos(y)*exp(cos(y)*z))
∇ˢf = (x,y,z) -> ∇f(x,y,z) - Point3f0(x,y,z)*dot(Point3f0(x,y,z), ∇f(x,y,z))
θ = [0;(0.5:n-0.5)/n;1]
φ = [(0:2n-2)*2/(2n-1);2]
x = [cospi(φ)*sinpi(θ) for θ in θ, φ in φ]
y = [sinpi(φ)*sinpi(θ) for θ in θ, φ in φ]
z = [cospi(θ) for θ in θ, φ in φ]
pts = vec(Point3f0.(x, y, z))
∇ˢF = vec(∇ˢf.(x, y, z)) .* 0.1f0
AbstractPlotting.surface(x, y, z)
AbstractPlotting.arrows!(pts, ∇ˢF, arrowsize = 0.03, linecolor = (:white, 0.6), linewidth = 3)
92.8s
Julia Test (Julia)
Julia

VegaLite

VegaLite can also provide interactive plotting.

using VegaLite, VegaDatasets
vega_plot = dataset("cars") |>
@vlplot(
    :point,
    x = :Horsepower,
    y = :Miles_per_Gallon,
    color = :Origin,
    width = 650,
    height = 400
) |> VegaLite.interactive()
30.2s
Julia Test (Julia)
Julia

Data Structures

Several data-related packages are installed in the default environment.

  • For handling their relative datatypes and I/O for associated files, we install a variety of packages, including JLD v

    "0.9.1"
    , CSV v
    "0.5.18"
    , HDF5 v
    "0.12.5"
    , and JSON v
    "0.21.0"
    .

  • The FileIO v

    "1.1.0"
    framework provides a unified interface for loading files of many types via multiple backends, including ImageMagick.

  • DataFrames v

    "0.9.1"
    defines and handles objects similar to those found in R and the Python pandas toolkit, with a comparable interface.

File Handling

The HDF5 package provides a Julia interface to the HDF5 library. It is also used by the JLD and MAT packages. Let's look at some example temperature data.

NEONDSTowerTemperatureData.hdf5
using HDF5
# Some methods to traverse the first path in an h5 file.
dd(node::HDF5File) = dd(node[names(node)[1]])
dd(node::HDF5Group) = dd(node[names(node)[1]])
dd(node::HDF5Dataset) = node
dspath = h5open(
NEONDSTowerTemperatureData.hdf5
) do h5
  name(dd(h5))
end
print("First path: $dspath.")
data = h5read(
NEONDSTowerTemperatureData.hdf5
, dspath)
using Plots
Plots.plot([x.data[1] for x in data],[y.data[3] for y in data],
  xrotation=45,legend=:none)
16.8s
Julia Test (Julia)
Julia

The JLD package provides a type-preserving way to save Julia objects to file.

using JLD
struct testData
  x::Int64
  y::String
end
foo = testData(7,"test")
save("/tmp/blah.jld", "foo", foo)
load("/tmp/blah.jld")
12.5s
Julia Test (Julia)
Julia
Dict{String,Any} with 1 entry: "foo" => testData(7, "test")

FileIO

FileIO provides a unified set of methods to access data in files: query(), load(), and save(), as well as loadstreaming() and savestreaming() for large files. The functions can automatically recognize files using headers or extensions, but you can also provide format information as below, where we load the Iris Dataset from a CSV file.

iris.csv
using FileIO
load(File(format"CSV",
iris.csv
))
16.0s
Julia Test (Julia)
Julia

DataFrames

DataFrame objects represent tabular data as a set of vectors.

using DataFrames
df = DataFrame(A = 1:5, B = 1:2:10, C = ["a","b","c","d","q"])
8.9s
Julia Test (Julia)
Julia
ABC
11a
23b
35c
47d
59q
5 items

Column names are referenced via accesing the fields.

df.A .+ df.B
1.5s
Julia Test (Julia)
Julia
5-element Array{Int64,1}: 2 5 8 11 14

JSON

Import and export JSON using the JSON package, which is always loaded on Nextjournal. In the example below, a Julia data structure input results in JSON output. The change from nothing to null is a clear indicator.

json(["foo", Dict("bar" => ("baz", nothing, 1.0, 2))])
0.8s
Julia Test (Julia)
Julia
"[\"foo\",{\"bar\":[\"baz\",null,1.0,2]}]"

Setup

Julia 1.x

Build a Minimal Julia 1.x Environment

We'll base our environment off of our Minimal Python image, which has a small conda Python setup. Note that the Julia version is set as an environment variable on the runtime.

Minimal Julia
Minimal Julia (Bash)
exporting environment
Type: Nextjournal
Environment:
Resources:
Environment Variables:
PATH/usr/local/julia/bin:/opt/conda/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
JULIA_PATH/usr/local/julia
JULIA_VERSION1.3.0
CONDA_JL_HOME/opt/conda
Download this environment as a Docker image from:

The exact version we're installing is

"1.3.0"
. Here we download the archive and signatures.

tarArch='x86_64'
dirArch='x64'
JULIA_VERSION_SHORT=${JULIA_VERSION/-rc/}
FILENAME="julia-${JULIA_VERSION}-linux-${tarArch}.tar.gz"
FILEURL="https://julialang-s3.julialang.org/bin/linux/${dirArch}/${JULIA_VERSION_SHORT%[.-]*}/${FILENAME}"
echo "Downloading ${FILEURL}."
curl -fL -o julia.sha256 \
  "https://julialang-s3.julialang.org/bin/checksums/julia-${JULIA_VERSION}.sha256"
curl -fL -o julia.tar.gz.asc "${FILEURL}.asc"
curl -fL -o julia.tar.gz "${FILEURL}"
echo `grep $FILENAME julia.sha256 | cut -d " " -f1` > j256sig
1.9s
Minimal Julia (Bash)

Check the signatures to verify our download.

sha256=`cat j256sig`
echo "${sha256} *julia.tar.gz" | sha256sum -c -
export GNUPGHOME="$(mktemp -d)"
JULIA_GPG="3673DF529D9049477F76B37566E3C7DC03D6E495"
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$JULIA_GPG"
gpg --batch --verify julia.tar.gz.asc julia.tar.gz
command -v gpgconf > /dev/null && gpgconf --kill all
rm -rf "$GNUPGHOME" julia.tar.gz.asc julia.sha256
2.2s
Minimal Julia (Bash)

Install and remove the archive.

mkdir -p "$JULIA_PATH"
tar -xzf julia.tar.gz -C "$JULIA_PATH" --strip-components 1
rm julia.tar.gz j256sig
4.7s
Minimal Julia (Bash)

And verify it runs.

julia -v
0.6s
Minimal Julia (Bash)

The JSON package is required to run on Nextjournal.

julia -e 'using Pkg; pkg"up; add JSON; precompile"'
23.9s
Minimal Julia (Bash)

Install Conda.jl, setting it to use the existing Conda installation.

julia -e 'using Pkg; pkg"add Conda; build Conda; precompile"'
10.3s
Minimal Julia (Bash)

Check size.

du -hsx /
2.1s
Minimal Julia (Bash)

Build the Default Julia 1.x Environment

We'll add a number of packages as well as the libraries and support programs required by them. The major packages installed are JuliaPlots along with the GR and PlotlyJS backends, WGLMakie, and the DataFrames, CSV, and HDF5 data-handling packages.

First we'll check that the minimal Julia env works.

"$VERSION"
0.4s
Julia 1.x versionJulia (Julia)
Minimal Julia
"1.3.0"

Install a few system tools and libraries.

apt-get -qq update
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends \
  imagemagick libhdf5-dev hdf5-tools mesa-utils \
  libxt6 libxrender1 libgl1-mesa-glx libqt5widgets5 `# for GR` \
  libhttp-parser2.7.1 `# for PlotlyJS`
apt-get clean
rm -r /var/lib/apt/lists/* # Clear package list so it isn't stale
30.1s
Julia (Bash in Julia)
Minimal Julia

Next the Julia package installs.

]up; add SoftGlobalScope DataFrames JLD CSV CSVFiles Netpbm NRRD MeshIO HDF5 MAT FileIO JSExpr CSSUtil StatsBase StatsPlots Observables Interact WebSockets HTTP Blink WebIO PlotlyBase PlotlyJS RecipesBase GR Plots ImageCore ImageShow ImageMagick Colors BenchmarkTools FixedPointNumbers IJulia
168.4s
Julia (Julia)
Minimal Julia

VegaLite currently requires a little custom NodeJS setup.

]dev VegaLite; add VegaDatasets
56.6s
Julia Test (Julia)
Julia

Install NodeJS from binaries to get a recent version of npm that supports Python 3.

NODE_FN="node-v${NODE_VERSION}-linux-x64"
NODE_URL="https://nodejs.org/dist/v${NODE_VERSION}/${NODE_FN}.tar.xz"
cd /opt
curl $NODE_URL | tar -Jx
ln -s $NODE_FN node
npm config set python /opt/conda/bin/python
npm config set unsafe-perm true # required because we are root
# Install to VegaLite.jl's directory
cd /root/.julia/dev/VegaLite/deps
npm install --scripts-prepend-node-path=true \
  --production --no-package-lock --no-optional
Shift+Enter to run
Minimal Julia (Bash)

Install the experimental WebGL backend for Makie.

]add https://github.com/JuliaPlots/WGLMakie.jl Observables AbstractPlotting#master JSCall#master
17.6s
Julia (Julia)
Minimal Julia

Build all packages.

]build
66.6s
Julia (Julia)
Minimal Julia

Precompile any qualifying packages.

]precompile
432.9s
Julia (Julia)
Minimal Julia

Makie also caches some fonts on first using, so we make sure that they're already cached.

using AbstractPlotting
for res in (AbstractPlotting.High, AbstractPlotting.Low)
  AbstractPlotting.set_glyph_resolution!(res)
  AbstractPlotting.cached_load();
end
28.8s
Julia (Julia)
Minimal Julia

Finally, add font defaults for JuliaPlots and Makie. These named Code Listings will be mounted as files to the runtime's filesystem, and saved with the environment.

PLOTS_DEFAULTS = Dict(:fontfamily => "Open Sans")
.juliarc.jl
Julia
Attributes(font = "Open Sans", resolution = (600, 500))
theme.jl
Julia

Test Default 1.x Env

using SoftGlobalScope, DataFrames, JLD, CSV, CSVFiles, Netpbm, NRRD, MeshIO, HDF5, MAT, FileIO, JSExpr, CSSUtil, StatsBase, StatsPlots, Observables, Interact, WebSockets, HTTP, Blink, WebIO, PlotlyBase, PlotlyJS, RecipesBase, GR, Plots, ImageCore, ImageShow, ImageMagick, Colors, BenchmarkTools, FixedPointNumbers, IJulia, WGLMakie, AbstractPlotting
IJulia.verbose
31.8s
Julia Test (Julia)
Julia
false
"$(Pkg.installed()["SoftGlobalScope"])"
1.7s
SoftGlobalScope versionJulia Test (Julia)
Julia
"1.0.10"
"$(Pkg.installed()["GR"])"
0.2s
GR versionJulia Test (Julia)
Julia
"0.44.0"
"$(Pkg.installed()["PlotlyBase"])"
0.3s
PlotlyBase versionJulia Test (Julia)
Julia
"0.3.0"
"$(Pkg.installed()["PlotlyJS"])"
0.3s
PlotlyJS VersionJulia Test (Julia)
Julia
"0.13.0"
"$(Pkg.installed()["Plots"])"
0.2s
Plots versionJulia Test (Julia)
Julia
"0.27.1"
"$(Pkg.installed()["AbstractPlotting"])"
0.3s
Makie versionJulia Test (Julia)
Julia
"0.9.10"
"$(Pkg.installed()["JLD"])"
0.2s
JLD versionJulia Test (Julia)
Julia
"0.9.1"
"$(Pkg.installed()["CSV"])"
0.2s
CSV versionJulia Test (Julia)
Julia
"0.5.18"
"$(Pkg.installed()["HDF5"])"
0.2s
HDF5 versionJulia Test (Julia)
Julia
"0.12.5"
"$(Pkg.installed()["JSON"])"
0.3s
JSON versionJulia Test (Julia)
Julia
"0.21.0"
"$(Pkg.installed()["FileIO"])"
0.2s
FileIO versionJulia Test (Julia)
Julia
"1.1.0"
"$(Pkg.installed()["DataFrames"])"
0.3s
DataFrames versionJulia Test (Julia)
Julia
"0.19.4"

Appendix

Since the site hosting the static build packages can be slow, we'll download FFmpeg here and save it for repeated uses. Current version is 4.1.3 (released 1 Apr 2019, downloaded 1 May 2019).

TARFILE="ffmpeg-release-amd64-static.tar.xz"
wget --progress=dot:giga \
  https://johnvansickle.com/ffmpeg/releases/${TARFILE}
wget https://johnvansickle.com/ffmpeg/releases/${TARFILE}.md5
md5sum -c ${TARFILE}.md5
cp $TARFILE /results/
8.8s
Download FFmpeg (Bash)
ffmpeg-release-amd64-static.tar.xz
Runtimes (4)