sdbuildR supports simulating stock-and-flow models with Julia as the
backend. Julia is a modern, open-source programming language that
reaches performance comparable to lower-level languages like C while
maintaining higher-level syntax similar to R and Python. To simulate
with Julia, sdbuildR translates R to Julia code and uses JuliaConnectoR
to call Julia from R, so that users may benefit from Julia’s
computational speed without interacting with Julia directly.This guide
will help you install Julia and configure it to work with sdbuildR.
Step 1: Install Julia
Download and install Julia from https://julialang.org/install/. If you already have Julia installed, go to the next step.
We recommend using juliaup, Julia’s official version manager, which makes it easy to install and switch between Julia versions. Alternatively, you can download a standalone Julia installer from https://julialang.org/downloads/.
Step 2: Check if Julia is accessible
After installing Julia, try to start a Julia session:
juliaSetupOk()
#> Starting Julia ...
#> [1] TRUE
juliaEval("1+1")
#> [1] 2
stopJulia()If this throws an error, go to the section Troubleshooting below.
Step 3: Using Julia with sdbuildR
After installing Julia, you need to set up the Julia environment for sdbuildR:
Note that this may take 10-25 minutes the first time as Julia downloads and compiles packages.
Step 4: Verify Julia environment setup
Start a Julia session and activate the Julia environment for sdbuildR:
use_julia()
#> ℹ Activating Julia environment for sdbuildR at
#> /home/runner/.local/share/R/sdbuildR/julia...
#> ✔ Julia environment ready.This needs to be done in each new R session.
To close the Julia session:
use_julia(stop = TRUE)
#> ✔ Closed Julia session.Troubleshooting
Julia not found
If JuliaConnectoR cannot find Julia, i.e., this evaluates to
FALSE:
juliaSetupOk()
#> Starting Julia ...
#> [1] TRUEFind your Julia installation path
From Julia
If you can start Julia from your terminal/command prompt, run this inside Julia:
This returns something like:
- Windows:
"C:\\Users\\YourName\\.julia\\juliaup\\julia-1.11.3+0.x64.w64.mingw32\\bin\\julia.exe" - macOS:
"/Applications/Julia-1.11.app/Contents/Resources/julia/bin/julia" - Linux:
"/usr/bin/julia"
Important: You need the bin directory containing the
executable (remove julia.exe or julia from the
end).
Add Julia to your PATH permanently
To make Julia accessible to R across all sessions, add it to your
.Renviron file.
Open .Renviron
Run this in R to open your .Renviron file:
# Install usethis if needed
if (!require("usethis")) install.packages("usethis")
# Open .Renviron for editing
usethis::edit_r_environ()This will open .Renviron in your text editor. If the
file doesn’t exist, it will be created.
Add Julia to PATH
Add one of the following lines to .Renviron, replacing
the path with your actual Julia bin directory:
Windows (use forward slashes for the path, semicolon as separator):
PATH="C:/Users/YourName/.julia/juliaup/julia-1.11.3+0.x64.w64.mingw32/bin;${PATH}"
macOS (use : as separator):
PATH="/Applications/Julia-1.11.app/Contents/Resources/julia/bin:${PATH}"
Linux (use : as separator):
PATH="/usr/bin:${PATH}"
Replace the path before the separator with your actual Julia
bin directory. Be sure to keep ${PATH} at the
end, such that the Julia path is appended to the PATH.
If using juliaup (recommended), you can point to the juliaup directory instead of a specific version:
Windows:
PATH="C:/Users/YourName/.julia/juliaup;${PATH}"
macOS/Linux:
PATH="~/.julia/juliaup/bin:${PATH}"
This allows juliaup to manage which Julia version is used.
Save and restart R.
- Save the
.Renvironfile - Restart R (Session → Restart R in RStudio, or close and reopen R)
If Julia is still not found
JuliaConnectoR tries to find Julia in the following order:
Sys.getenv("JULIA_BINDIR") # "" if not found
Sys.which("julia") # "" if not found
# On Mac/Linux:
julia_path <- file.path(Sys.getenv("HOME"), ".juliaup", "bin", "julia")
file.exists(julia_path) # FALSE if not found- Double-check the path you added to
.Renviron - Make sure you’re pointing to the
bindirectory, not the Julia executable - Make sure you’re using the file path separator appropriate for your
operating system, which you can find with
.Platform$file.sep. - Verify you saved
.Renvironand restarted R - View your current PATH
Check whether your Julia bin directory is in your PATH:
# View PATH split by separator for readability
paths <- strsplit(Sys.getenv("PATH"), .Platform$path.sep)[[1]]
paths[grepl("julia", paths)]Managing Julia versions with juliaup
Some useful terminal commands are listed below.
# See installed versions and current default
juliaup status
# Install latest stable version
juliaup add release
# Install specific version
juliaup add 1.11.3
# Set default version
juliaup default 1.11.3
# Update juliaup and all Julia versions
juliaup update
# Remove a version
juliaup remove 1.10.0
# List all available versions
juliaup listRemoving the Julia environment for sdbuildR
install_julia_env(remove = TRUE)Advanced: Using multiple Julia versions
If you have multiple Julia versions installed, the first one found in your PATH will be used by default.
Switch versions with juliaup
If using juliaup, you can change the default version
without modifying .Renviron:
Switch versions temporarily
sdbuildR uses your default Julia version. To use a different version:
Sys.setenv(JULIA_BINDIR = "C:/Users/YourName/.julia/juliaup/julia-1.11.3+0.x64.w64.mingw32/bin")Note: This only affects the current R session and is not permanent.