Getting started with Julia
We are going to get started with Julia. This section will describe how to run Julia scripts and interact with the Julia Read-Eval-Print Loop (REPL), the command line or console used to execute Julia code interactively. But first, let's install Julia on your system.
Installing Julia
We are going to install the stable release of Julia on your system. There are many ways to install Julia, depending on your operating system. Here, we are going to install Julia using the official binaries. First, let's download the binary of Julia's Long-Term Support (LTS) release, Julia 1.6. You can install other releases, but I recommend using Julia 1.6 to ensure reproducibility of the book's examples:
- Go to https://julialang.org/downloads/.
- Download the binary file for the LTS release that matches your operating system (Windows, macOS, Linux, or FreeBSD) and the computer architecture bit widths (64-bit or 32-bit).
If you are a Windows user, we recommend downloading the installer. When choosing the installer, note that a 64-bit version will only work on 64-bit Windows.
If you are using Linux, you should also match the instruction set architecture of your computer processor (x86, ARM, or PowerPC). You can use the following command in your terminal to learn your architecture:
uname --all
Note that i386 on the output means you have a 32-bit x86 processor, and x86_64 indicates a 64-bit x86 processor.
The instructions that follow this step depend on your operating system, so, let's see them separately. Once you have finished, you can test your Julia installation by typing julia -v
on your terminal; it should print the installed Julia version.
Linux
Installation on Linux is pretty simple; you should do the following:
- Move the downloaded file to the place you want to install Julia.
- Decompress the downloaded
.tar.gz
file. On a terminal, you can usetar -xf
followed by the filename to do it. - Put the
julia
executable that is inside thebin
folder of the decompressed folder onPATH
. ThePATH
system variable contains a colon-separated list of paths where your operating system can find executables. Therefore, we need the folder containing thejulia
executable on that list to allow your operating system to find it. There are different ways to do that depending on the Unix shell that runs in your system terminal. We will give the instructions for Bash; if you are using another shell, please look for the corresponding instructions. You can know the shell that runs in your terminal by executingecho $0
.
Using Bash, you can add the full absolute path to the bin
folder to PATH
in ~/.bash_profile
(or ~/.bashrc
) by adding the following line to that file:
export PATH="$PATH:/path/to/julia_directory/bin"
You should replace /path/to/julia_directory/bin
with the full path to the bin
folder containing the julia
binary. The $
and :
characters are required; it is easy to mess up your Bash terminal by forgetting them.
macOS
To install Julia on macOS, do the following:
- Double-click the downloaded
.dmg
file to decompress it. - Drag the
.app
file to the Applications folder. - Put
julia
onPATH
to access it from the command line. You can do that by creating a symbolic link tojulia
on/usr/local/bin
– for example, if you are installing Julia 1.6, you would use this:ln -s /Applications/Julia-1.6.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia
Windows
If you are on Windows, you should only run the installer and follow the instructions. Please note the address where Julia has been installed. You will need to put the julia
executable on PATH
to access Julia from the command line. The following instructions are for Windows 10:
- Open Control Panel.
- Enter System and Security.
- Go to System.
- Click on Advanced system settings.
- Click on the Environment variables... button.
- In the User Variables and System Variables sections, look for the
Path
variable and click that row. - Click the Edit... button of the section that has the
Path
variable selected. - Click New in the Edit environment variable window.
- Paste the path to the
julia
executable. - Click the OK button.
Interacting with the Julia REPL
Now you have installed Julia, let's start exploring the Julia REPL or console. Let's do a simple arithmetic operation:
- Type
julia
on your system terminal to open the Julia REPL. If you are using Windows, we recommend using a modern terminal as the Windows terminal. - Type
2 + 2
after the julia> prompt on the Julia REPL and press Enter.
Voilà! You have done your first arithmetic operation in Julia.
- Exit the Julia REPL using one of the following options:
- Type
exit()
and press Enter. - Press the control key (Ctrl) together with the D key on a blank line.
- Type
Now that we know the basics of interacting with the Julia REPL, let's see some of its more exciting capabilities – tab completion, support for Unicode characters, and REPL modes.
Getting help from tab completion
The Tab key is handy when working on the Julia REPL. It can speed up your code input and show you the available options in a given context. When entering a name on a current workspace, pressing the Tab key will autocomplete the name until there are no ambiguities. If there are multiple options, Julia will list the possibilities.
You can also use tab completion to get information about the expected and keyword arguments that a function can take. To get that, you need to press the Tab key after the open bracket or after a comma in the argument list. For example, you can see the list of methods for the sum
function, including possible arguments and their accepted types, by typing sum(
and pressing the Tab key. Note that typing sum([1, 2, 3],
and pressing the Tab key will give you a shorter list, as Julia now knows that you want to sum a vector of integers.
Last but not least, you can use tab completion to find out the field names of a given object. For example, let's see the field names of a range object:
- Type
numbers = 1:10
on the Julia REPL and press Enter. That defines the variable numbers to contain a range of integers. - Type
numbers.
and press the Tab key. Julia will autocompletest
, as the two fields of a range object start with these two letters. - Press the Tab key. Julia will list the field names of the range object – in this case,
start
andstop
. - Type
a
to disambiguate and press the Tab key to autocomplete. - Press the Enter key to see the value of
numbers.start
.
Julia can autocomplete more things, such as the fields of a function output or a dictionary's keys. As we will see next, you can also use tab autocompletion to enter Unicode characters.
Unicode input
Julia allows you to use any Unicode character, including an emoji, on your variables and function names. You can copy and paste those characters from any source. Also, you can insert them using tab completion of their LaTeX-like abbreviation, starting with the \
character. For example, you can enter π
by typing \pi
and pressing the Tab key.
REPL modes
Julia offers a variety of REPL modes to facilitate different tasks. You can enter the different modes by pressing a specific key just after the julia> prompt. The following figure shows you the built-in REPL modes that come with Julia:
Let's test that by using the most helpful mode, the help mode to access the documentation of the split
function:
- Press the
?
key just after the julia> prompt; you will see that the prompt changes to help?>. - Type
split
and press Enter; you will see the function's documentation and automatically return to the julia> prompt.
Going back to the julia> prompt only requires pressing the Backspace key just after the prompt of the REPL mode. Another way to come back to the julia> prompt is by pressing the control (Ctrl) and C keys together. In this case, we didn't need the Backspace key to return to the julia> prompt as the help mode isn't sticky. However, the shell and pkg modes shown in Figure 1.1 are sticky and require pressing the Backspace key to go out of them.
Running Julia scripts
Using the Julia REPL is helpful for interactive tasks and ephemeral code, but creating and running Julia scripts can be a better option in other situations. A Julia script is simply a text file with the jl
extension containing Julia code. The easiest way to run a Julia script in your system terminal is by running the julia
executable and giving the path to the script file as the first positional argument. For instance, if you want to run a Julia script named script.jl
that is in the current folder, you can run the following line in the terminal:
julia script.jl
We have now installed Julia and learned how to run Julia code. In the next section, we will learn how to install Julia packages and manage project environments to ensure reproducibility.