# Julia

Although this course is language agnostic, we will use Julia to demonstrate various algorithms. It is a high-level language for scientific computing that provides exceptional run-time performance, approaching that of statically-compiled languages like C. The software is free and open source and is under active development.

If you are already familiar with MATLAB, R, or Python, see this list of notable differences.

There are many ways to get going with Julia. Here are some options:

1. Install Julia on your own machine. You can download and install the command line version or a version that is bundled with the Juno IDE or VS Code.
2. All you need to do is ssh into rice.stanford.edu to access the shared computing environment. The default version of Julia available on the machines is 0.5. To use the latest Julia 1.5.1, first load the Julia module and check the version. You can then save it to be the default:
$module load julia$ julia --version
julia version 1.5.1
\$ module save

### Using Julia Packages

Julia’s base installation already comes with several handy packages for containers, linear algebra, random number generation, and the like. The Base features are directly usable in the REPL while for the Standard Library, you need to import individual stdlib packages in the REPL with using, i.e. julia> using LinearAlgebra and so on. Check the left-hand pane of the docs for the list of features under Base and under Standard Library. For any further packages, you will have to add them separately. You can use the Package manager (which is part of stdlib) by doing julia> using Pkg; Pkg.add("XYZ") or by pressing ] in the REPL to go to package manager mode directly and doing ] add XYZ, where XYZ is the name of some registered package. It is unlikely you will need to install unregistered packages, but if so, check here for more instructions or ask us in office hours.

## Notebooks

#### Jupyter

Jupyter notebooks are useful for visualizations and code in a single working document. To install Juypter (using the IJulia package) follow the steps here:

1. Install Julia: Instructions
2. Install Jupyter: IJulia Instructions

#### Pluto

Another option for a notebook-style workspace is Pluto.jl. Pluto is very new but super interactive! See examples and installation instructions here.

## Troubleshooting

Potential fixes for things that could go wrong.

### Problems installing jupyter

First, check to see if IJulia was able to install it. julia> Pkg.add("IJulia") will download the IJulia Julia package, and, if you do not already have jupyter, this will automatically install it via miniconda via the Conda.jl package.

To test the installation, in Julia, run

julia> using IJulia
julia> notebook()

This should open a jupyter notebook browser in the current directory. If this doesn’t work, jupyter may not be installed correctly.

If you are using Windows and you get the message failed to create process, it is likely that you have spaces in the file path (if you are in this situation please contact Robert at mossr@cs.stanford.edu so I can verify that these instructions are correct). Check to see if there are any spaces in the path returned by julia> Pkg.dir(). If there are, one workaround is to install Miniconda in a path that does not have spaces:

1. Install miniconda with python 3.6 from here. Make sure to select install for all users. This should install miniconda in a directory like C:\Miniconda3.
2. Install the required jupyter python package. In the windows command prompt, run the following command:conda install jupyter
3. Close all command prompts and restart julia so that the environment is updated with the new miniconda installation, and rebuild IJulia: julia> Pkg.build("IJulia")
4. Start the notebook with the following command in a command prompt: jupyter notebook and try creating a new Julia notebook.

Note:  If you use this workaround, you may need to manually install dependencies for other packages (such as matplotlib for PyPlot.jl) using conda instead of from within julia, and you may have to notify julia of your Anaconda installation with julia> ENV["PYTHON"]="C:\\Miniconda3\\python.exe; Pkg.build("PyCall").

### HDF5 build error when installing BayesNets.jl from install.jl

HDF5 is a data storage library (the same that is used in Matlab’s .mat files). Julia should automatically download and install the library, but you may need to do it manually. More instructions are at the HDF5.jl repo – Ubuntu: sudo apt-get install hdf5-tools

### Bayesian network graph visualizations do not display correctly in notebook

• If text is displayed without an image – TikzGraphs.jl or one of its dependencies may not be installed correctly. Try reinstalling it.
• If only edges are displayed or node labels do not display correctly – Use the google-chrome browser. Firefox and other browsers may not display the svgs correctly.

### PGFPlots/SVG conversion errors when running in a notebook

If you get an error similar to

Error saving as SVG
could not spawn pdf2svg tikzpicture.pdf tikzpicture.svg: no such file or directory (ENOENT)


You need to have pdf2svg installed on your machine. If on OSX, run:

brew install pdf2svg


Please install homebrew here if you do not have it yet. If you do not have xcode tools installed, refer to this. If on Ubuntu, run:

sudo apt-get install pdf2svg

If on Windows, download this: https://github.com/jalios/pdf2svg-windows/archive/master.zip. Extract the zip file where you want to keep it. Then add the directory inside called “dist-64bits” to your path. Instructions on how to do this can be found here.

And make sure that you add pdf2svg to your path, regardless of your platform, so that you can run “pdf2svg” from the terminal.

If you get an error similar to:

Error saving as SVG
could not spawn lualatex --enable-write18 --output-directory=. tikzpicture: no such file or directory (ENOENT)

then you do not have tex plotting capabilities. To get plotting to work, we recommend installing texlive (either through your package manager on linux [texlive-extra-utils on ubuntu] or from https://www.tug.org/texlive/quickinstall.html) or from https://www.tug.org/texlive/acquire-netinstall.html.