Setting up your Julia session
Introduction
A nice feature of Julia is that supports startup file that contains the code that is executed every time you start a new Julia session.
This functionality is described in the Julia Manual. In this post I want to discuss this functionality from the user’s perspective.
The basics of startup file
The startup file is typically located at ~/.julia/config/startup.jl
folder
(I am using Linux defaults here). However, this default location can be modified.
You can change it by manipulating environment variables. These more advanced
configuration options are described here in the Julia Manual.
The startup.jl
file contains the commands that are executed when Julia is run.
What are typical entries of the startup file?
Usually people load utility packages that they routinely need when working with Julia (to avoid having to manually load them each time). Some popular examples are:
- Revise.jl: allowing to modify source code without having to restarting Julia;
- OhMyREPL.jl: advanced highlighting support in Julia terminal;
- JET.jl: code analyzer for Julia;
- BenchmarkTools.jl: performance tracking of Julia code.
Another use programmatic setting of preferences, for example:
- setting default code editor via
ENV["EDITOR"]
variable; - automatic activation of project environment; I discussed it some time ago in this post.
Selective execution of code in startup file
Julia can be started in two modes:
- interactive (called REPL);
- script execution.
Some of the features are useful only in REPL mode. For example, loading OhMyREPL.jl probably does not have much value added when executing a script.
Julia allows you to add code that should run only when it is activated in REPL mode
by registering a function that is run only in this mode. This registration is achieved
via the atreplinit
function that should be defined in your startup.jl
file.
It is done in the following way (the code inside the function is just an example):
atreplinit() do repl
println("This is printed only in REPL, but not when executing a script")
end
Disabling loading of a startup file
In some cases, especially if you run some third party Julia code, you might want
to disable loading the startup.jl
file. This can be achieved by
passing the --startup-file=no
command line argument.
Why could you want to do it? In this way you make sure that the code you have in
your startup.jl
does not conflict with the code you want to run (this situation
is rare, but is possible).
Conclusions
Some Julia users prefer not to define startup.jl
at all and always be explicit
about what is loaded when Julia is started. This scenario is probably most common
when someone mostly runs Julia scripts as it ensures a clean environment and fastest
load times.
However, in many users, especially if they work interactively a lot, like to initialize their Julia session each time it is started with some standard code. For example, if you are a data scientist you might want to always load for example CSV.jl, DataFrames.jl, Statistics.jl, and StatsBase.jl when you are working in REPL mode to avoid having to load these packages manually every time you start a new session.
When working with startup.jl
files there are two things worth remembering:
- You can selectively decide which code is loaded only if you work in REPL mode
via the
atreplinit
function. - Even if you have
startup.jl
file you can fully disable loading it by passing the--startup-file=no
command line option.
Happy hacking!