# Setting up Your Julia Environment¶

### Florian Oswald¶

This is lecture is a slightly modified version of https://lectures.quantecon.org/jl/getting_started.html Thank you to the amazing Quantecon.org team!

## Overview¶

In this lecture we will cover how to get up and running with Julia

Topics:

1. Jupyter
2. Local installation vs. cloud-based solutions
3. Installation of libraries, including the Julia code that underpins these lectures

## Jupyter¶

• Like Python and R, and unlike Matlab and Stata, there is a looser connection between Julia as a programming language and Julia as a development environment.
• Because of this, you have much more flexibility in how you write and edit your code, whether that be locally or on the cloud, in a text-editor or IDE, etc.
• One example is Jupyter, which provides a browser-based interface to execute code in different languages (e.g. Julia, Python, R, etc.)

While you will eventually use other editors, there are some advantages to starting with Jupyter while learning Julia:

• The ability to mix formatted text (including mathematical expressions) and code in a single document
• Nicely formatted output including tables, figures, animation, video, etc.
• Conversion tools to generate PDF slides, static HTML, etc.
• Can be used in the cloud without requiring installation

Whether you end up using Jupyter as your primary work environment or not, you’ll find learning about it an excellent investment

## Installing Julia and Dependencies¶

• We do not recommend JuliaPro due to its limited number of available packages
• Open Julia, by navigating to Julia through your menus or desktop icons.

You should now be looking at something like this (modulo your operating system)

This is called the JULIA REPL (Read-Evaluate-Print-Loop), which we discuss more below

• In the Julia terminal, type the following

This installs enough Julia packages to begin using the lecture notes Note: On OS/X you will need to type the ] separately and cannot copy/paste the whole string

### Using Jupyter from Julia REPL¶

• You can easily open a notebook browser directly from julia
• To do so, start julia. then:
using IJulia
notebook(dir=".",detached=true)

this will open a notebook browser in directory dir.

### Using Jupyter via Anaconda Python¶

Another Approach is to install the latest version Anaconda and then Julia

• Make sure you click yes to “add Anaconda to my PATH”
• To run Jupyter, open a terminal or windows console, cd to the location you wish to write files and type

jupyter lab

## Using Jupyter Notebooks¶

### Getting Started¶

After you started the notebook browser from the REPL, you will see something like:

To open an empty julia notebook, click on new and select julia 1.0.

### Using QuantEcon Lecture Packages¶

To use the curated set of packages in the QuantEcon lecture notes, put the following text in a notebook cell, and hit Shift-Enter to run the cell

using InstantiateFromURL

# activate the QuantEcon environment
activate_github("QuantEcon/QuantEconLecturePackages", tag = "v0.9.5");

This downloads, installs, and compiles the correct version of all of packages used in the QuantEcon lectures

Depending on your computer, this may take 10-15 minutes to run the first-time, but be virtually instantaneous thereafter

This code can be put at the top of any notebook in order to get a tested set of packages compatible with the code in the QuantEcon notes

More details on packages are explained in a later lecture

### Notebook Basics¶

Notice that in the previous figure the cell is surrounded by a blue border

This means that the cell is selected, and double-clicking will place it in edit mode

As a result, you can type in Julia code and it will appear in the cell

When you’re ready to execute these commands, hit Shift-Enter

The next thing to understand about the Jupyter notebook is that it uses a modal editing system

This means that the effect of typing at the keyboard depends on which mode you are in

The two modes are

1. Edit mode

• Indicated by a green border around one cell, as in the pictures above
• Whatever you type appears as is in that cell
2. Command mode

• The green border is replaced by a blue border
• Key strokes are interpreted as commands — for example, typing b adds a new cell below the current one

(To learn about other commands available in command mode, go to “Keyboard Shortcuts” in the “Help” menu)

#### Switching modes¶

• To switch to command mode from edit mode, hit the Esc key
• To switch to edit mode from command mode, hit Enter or click in a cell

The modal behavior of the Jupyter notebook is a little tricky at first but very efficient when you get used to it

#### Working with Files¶

To run an existing Julia file using the notebook you can copy and paste the contents into a cell in the notebook

If it’s a long file, however, you have the alternative of

1. Saving the file in your present working directory
2. Executing include("filename") in a cell

The present working directory can be found by executing the command pwd()

#### Plots¶

Let’s generate some plots

First, ensure that you have activated a set of packages within the current Jupyter notebook

In [1]:
# Update Jan2020 - CMS: No need of the QuantEcon env.
#using InstantiateFromURL
# activate the QuantEcon environment
#activate_github("QuantEcon/QuantEconLecturePackages", tag = "v0.9.5");

Now try copying the following into a notebook cell and hit Shift-Enter

In [1]:
using Plots
gr()
plot(sin, -2π, 2π, label="sin(x)")
Out[1]:

You’ll see something like this (although the style of plot depends on your installation — more on this later)

Note: The “time-to-first-plot” in Julia takes a while, since it needs to precompile everything

### Working with the Notebook¶

Let’s go over some more Jupyter notebook features — enough so that we can press ahead with programming

#### Tab Completion¶

Tab completion in Jupyter makes it easy to find Julia commands and functions available

For example if you type rep and hit the tab key you’ll get a list of all commands that start with rep

#### Getting Help¶

To get help on the Julia function such as repeat, enter ? repeat

Documentation should now appear in the browser

#### Other Content¶

In addition to executing code, the Jupyter notebook allows you to embed text, equations, figures and even videos in the page

For example, here we enter a mixture of plain text and LaTeX instead of code

Next we Esc to enter command mode and then type m to indicate that we are writing Markdown, a mark-up language similar to (but simpler than) LaTeX

(You can also use your mouse to select Markdown from the Code drop-down box just below the list of menu items)

Now we Shift + Enter to produce this

#### Inserting unicode (e.g. Greek letters)¶

Julia supports the use of unicode characters such as α and β in your code

Unicode characters can be typed quickly in Jupyter using the tab key

Try creating a new code cell and typing \alpha, then hitting the tab key on your keyboard

#### Shell Commands¶

You can execute shell commands (system commands) in Jupyter by prepending a semicolon

For example, ; ls will execute the UNIX style shell command ls, which — at least for UNIX style operating systems — lists the contents of the current working directory

These shell commands are handled by your default system shell and hence are platform specific

#### Package Manager¶

You can enter the package manager by prepending a ]

For example, ] st will give the status of installed packages in the current environment

### Sharing Notebooks¶

Notebook files are just text files structured in JSON and typically end with .ipynb

A notebook can easily be saved and shared between users — you just need to pass around the ipynb file

To open an existing ipynb file, import it from the dashboard (the first browser page that opens when you start Jupyter notebook) and run the cells or edit as discussed above

The Jupyter organization has a site for sharing notebooks called nbviewer which provides a static HTML representations of notebooks

QuantEcon also hosts the QuantEcon Notes website, where you can upload and share your notebooks with other economists and the QuantEcon community

### The REPL¶

While we have not emphasized it, on any JupyterHub or local Jupyter installation you will also have access to the Julia REPL

This is a Julia specific terminal disconnected from the graphical interface of Jupyter, and becomes increasingly important as you learn Julia

For example, the REPL is what we used in the beginning of this lecture to install InstantiateFromURL.jl and IJulia

To start the REPL from the Jupyter notebook browser, click on "New", then "Terminal" in the dropdown menu:

Otherwise, if you have a local installation, then

1. Navigate to Julia through your menus or desktop icons (Windows, OSX), or
2. Open a terminal and type julia (Linux)

The REPL is one of the best places to add and remove packages, so a good test is to see the current status of the package manager

In [2]:
] st
Project quantecon-notebooks-julia v0.4.0
Status C:\Users\cms27\Project.toml
[2169fc97] AlgebraicMultigrid v0.2.2
[28f2ccd6] ApproxFun v0.11.5
[7d9fca2a] Arpack v0.3.1
[aae01518] BandedMatrices v0.10.1
[6e4b80f9] BenchmarkTools v0.4.3
[a134a8b2] BlackBoxOptim v0.5.0
[ffab5731] BlockBandedMatrices v0.4.6
[324d7699] CategoricalArrays v0.7.1
[34da2185] Compat v2.1.0
[a93c6f00] DataFrames v0.19.4
[1313f7d8] DataFramesMeta v0.5.0
[5721bf48] DataVoyager v0.3.1
[39dd38d3] Dierckx v0.4.1
[31c24e10] Distributions v0.21.3
[2fe49d83] Expectations v1.1.1
[a1e7a1ef] Expokit v0.2.0
[d4d017d3] ExponentialUtilities v1.5.1
[1a297f60] FillArrays v0.6.4
[9d5cd8c9] FixedEffectModels v0.8.5 ⚲
[c8885935] FixedEffects v0.3.0 ⚲
[587475ba] Flux v0.9.0
[f6369f11] ForwardDiff v0.10.3
[38e38edf] GLM v1.3.1 ⚲
[28b8d3ca] GR v0.41.0
[40713840] IncompleteLU v0.1.1
[a98d9a8b] Interpolations v0.12.2
[b6b21f68] Ipopt v0.6.0
[42fd0dbc] IterativeSolvers v0.8.1
[4076af6c] JuMP v0.20.0
[5ab0869b] KernelDensity v0.5.1
[ba0b0d4f] Krylov v0.3.0
[0b1a1467] KrylovKit v0.4.0
[b964fa9f] LaTeXStrings v1.0.3
[5078a376] LazyArrays v0.10.0
[0fc2ff8b] LeastSquaresOptim v0.7.4
[093fc24a] LightGraphs v1.3.0
[7a12625a] LinearMaps v2.5.2
[5c8ed15e] LinearOperators v0.5.4
[76087f3c] NLopt v0.5.1
[2774e3e8] NLsolve v4.0.0
[429524aa] Optim v0.19.3
[d96e819e] Parameters v0.12.0
[14b8a8f1] PkgTemplates v0.6.3
[91a5bcdd] Plots v0.27.0
[f27b6e38] Polynomials v0.5.2
[af69fa37] Preconditioners v0.3.0
[92933f4c] ProgressMeter v1.1.0
[fcd29c91] QuantEcon v0.16.2
[1a8c2f83] Query v0.12.2
[612083be] Queryverse v0.3.1
[ce6b1742] RDatasets v0.6.3
[d519eb52] RegressionTables v0.2.2 ⚲
[295af30f] Revise v2.2.0
[f2b01f46] Roots v0.8.3
[47a9eef4] SparseDiffTools v0.8.0
[684fba80] SparsityDetection v0.1.1
[90137ffa] StaticArrays v0.11.0
[2913bbd2] StatsBase v0.32.0
[3eaba693] StatsModels v0.6.5
[f3b207a7] StatsPlots v0.12.0
[112f6efa] VegaLite v0.7.0
[e88e6eb3] Zygote v0.3.4
[37e2e46d] LinearAlgebra
[9a3f8284] Random
[2f01184e] SparseArrays
[10745b16] Statistics
[8dfed614] Test

## Other Ways to Use Jupyter¶

There are alternative workflows, such as

1. Using Jupyter on the cloud or a department server (if it is available)
2. Installing the pre-built docker-based Julia/Jupyter from QuantEcon

Eventually, you will want to move from just using Jupyter to using other tools and editors such as Atom/Juno, but don’t let the environment get in the way of learning the language

### Using Jupyter Online¶

If you have access to a cloud-based solution for Jupyter, then that is typically an easy solution

• Universities and workgroups: email contact@quantecon.org for help on setting up a shared JupyterHub instance with precompiled packages ready for these lecture notes
• JuliaBox tightly controls allowed packages, and does not currently support the QuantEcon lectures

If you are using an online Jupyter installation for a class, you may not need to do anything to begin using these notebooks

Otherwise, if there are errors when you attempt to use an online JupyterHub, you will need to go open a Jupyter notebook and type

### Installing a Pre-built Jupyter Image¶

Docker is a technology that you use to host a “virtual” version of a software setup on another computer

While it is largely used for running code in the cloud, it is also convenient for using on local computers

QuantEcon has constructed a pre-built docker image

For instructions on how to set this up, see the tools and editors lecture

Note: The Docker installation is easy and complete, but it has limitations on operating systems (in particular, Windows 10 is only supported for the Professional and Education editions, and not the Home edition)