Skip to contents
library(sdbuildR)
library(JuliaConnectoR)

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] TRUE

Find your Julia installation path

From Julia

If you can start Julia from your terminal/command prompt, run this inside Julia:

Base.julia_cmd()[1]

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).

From your terminal/command prompt

Windows:

where julia

macOS/Linux:

which julia
# or
whereis julia

Take note of the path, removing the filename to get just the bin directory.

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.

  1. Save the .Renviron file
  2. 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
  1. Double-check the path you added to .Renviron
  2. Make sure you’re pointing to the bin directory, not the Julia executable
  3. Make sure you’re using the file path separator appropriate for your operating system, which you can find with .Platform$file.sep.
  4. Verify you saved .Renviron and restarted R
  5. 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 list

Removing 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:

# From terminal/command prompt
juliaup default 1.11.3

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.