1 Installation and general usage
This chapter has no prerequisites.
1.1 Installing Julia
Should include images
The Julia program can be downloaded from https://julialang.org/downloads/. You’ll need to choose the correct version for your system, with different instructions for installation for different systems found at https://julialang.org/downloads/platform/. It will be important to Add Julia to PATH, which you will see mentioned in these instructions, as we’ll want to make Julia findable for our editor in a moment. Alternate ways of downloading Julia exist, such as the Windows Store, the command line program juliaup, or Linux-specific scripts, whichever you prefer.
Once the program is installed, you can launch it, and begin to type (for example, some basic sums like 1 + 2). This input is called the REPL or read-eval-print loop, because what it does is the following:
It reads what the user has typed in
It attempts to evaluate it as Julia code
It prints the result of the evaluation, whether that is a calculation result, or a message telling the user that something has gone wrong
It loops back around to the start, putting the
julia>prompt again and waiting for another user input
The REPL has all the functionality of Julia, and more. Previous inputs can be navigated to by ↑ and ↓, or searched with the search modes (accessible through Ctrl-S / Cmd-S and Ctrl-R / Cmd-R). Help can be found by pressing ? to enter help mode, while Pkg (package) mode comes from typing ] (see Chapter 8 for more details on packages).
However, it still has one major limitation, which is that nothing is saved. If you close the program and reopen it, you’ll notice that it has no memory of any of the computations it has done before. Once we start chaining computations together into fully fledged algorithms, we don’t want to lose all of our progress every time we close the program, nor do we want to leave it running the whole time taking up unnecessary memory. For this, we’ll need to make use of our own files, saved on our computer, with our code written out in full contained within it. This is entirely possible to do within the REPL, but for most people is needlessly difficult, so we’ll make use of a code editor, specifically an integrated development environment, or IDE for short.
An IDE does many things, with different ones having different attractions. The most common features include:
Code editor, for writing and editing code files, which can be saved to the computer
Syntax highlighting, by changing the colour and sometimes other aspects of the font to distinguish keywords, or sections of the code file that do different things
Automatic indentation, which for Julia is not needed in general, but helps for readability of code to show where blocks of code start and end
Debugging tools, such as recognising errors in code before it is run
Integrated terminal/REPL, so code can be run and tested quickly and easily
There are many IDEs in existence, but the most popular for Julia is VS Code, which we’ll focus on here.
1.2 Installing VSCode
Microsoft’s VSCode is an IDE with compatibility with hundreds if not thousands of programming languages, and more beyond that. However, we’ll only look at its use for Julia.
To begin with, VSCode will need to be downloaded from https://code.visualstudio.com/download/, where you’ll need to choose the right version for your system as you did with Julia. Once it’s installed, you’ll need to open it, and install the Julia extension, which allows VSCode to work with Julia. This can be done by finding the extensions tab on the left sidebar (which looks like three squares in an L shape with a fourth floating above), and searching for Julia in the search box. Now, if you’ve installed this, and if you clicked Add Julia to PATH earlier, then everything should be set up to start programming. If not, the documentation at https://www.julia-vscode.org/docs/dev/ should help to resolve your issue.
1.3 Using Julia in VSCode
To open an instance of the Julia REPL in VSCode, hold down the Alt key, and press J followed by O. This functions identically to the REPL that appears when we open the Julia program.
In VSCode, we can also open a .jl file to be able to write code and save it to be run whenever we wish, as well as to be able to be run from within other programs. This is essential for larger projects, and can be done by going to File - New File, and choosing “Julia File”. Alternatively, holding down Alt and pressing J followed by N has the same effect. You can save this file as a Julia .jl file, and any code written in it can be run by the play button in the top right corner of the editor window. This code runs in an instance of the familiar Julia REPL, which you will see in a separate panel. The REPL can also be opened at any time by holding down Alt and pressing J followed by O.
VSCode is highly customisable, and you are recommended to experiment with its many options to find the layouts and functionalities that work best for you.
1.4 Pluto notebooks
A third alternative working environment is provided by the Pluto notebook, from the Pluto.jl package (see Chapter 8 for more discussion of packages). Pluto works markedly differently from the REPL and editor environments, and is perhaps best explained by the Getting Started notebooks on its homepage. As a brief introduction, the individual lines input into the REPL are instead separated into code chunks, and instead of running independently, they run as a network, such that changing one block automatically reruns those blocks which depended on its output. This provides much greater interactivity, at the cost of scalability, so is best suited for short demonstrations rather than full blown software engineering.
First, however, you need to install it, for which you’ll need to install Julia as before, but you don’t need an IDE. With Julia installed, launch the Julia program, giving an instance of the REPL. Next, press ] to enter Pkg mode, type add Pluto, and press Enter ⮠ which will download and install the package.
(@v1.9) pkg> add Pluto
Once that is done, press ← Backspace to return to the normal REPL mode, type using Pluto, and press Enter ⮠, which tells Julia to load the package ready for use.
julia> using PlutoFinally, type Pluto.run() and press Enter ⮠, which will launch Pluto in your default web browser.
julia> Pluto.run()When using Pluto for a second time, you don’t need to install it again, just launch the REPL fresh and start at using Pluto.
The rest of this guide is written with the intention of being used in the REPL, or in an IDE such as VSCode that runs code in the REPL. At times, we will note where differences between the behaviour of the REPL and Pluto notebooks exist, but there are inevitably places that we have overlooked!
1.5 Updating Julia
Julia is continuously being updated, and it is recommended to stay up to date yourself to be able to use new features and to avoid bugs in older versions. The most reliable way of doing this is to simply redownload with the newest version, but you may have to manually delete the old version from your files, and reconfigure IDEs like VSCode to recognise the new version ahead of the old.
At time of writing, there is also a way to do this from inside Julia itself, and this is the UpdateJulia package (again, see Chapter 8). To begin with, open the REPL, press ] to enter Pkg mode, type add UpdateJulia, and press Enter ⮠ to install the package.
(@v1.9) pkg> add Pluto
Once the package has installed, return to the normal REPL with ← Backspace, type using UpdateJulia and press Enter ⮠, then type update_julia() and press Enter ⮠, which will automatically update you to the newest version.
julia> using UpdateJulia
julia> update_julia()