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

  • Download and install Julia, from download page , accepting all default options.

    • 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
] add IJulia InstantiateFromURL; precompile

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

  • Install Anaconda by downloading the binary https://www.anaconda.com/download/

    • 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:

test

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]:
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 [2]:
using Plots
gr(fmt=:png);
plot(sin, -2π, 2π, label="sin(x)")
┌ Info: Recompiling stale cache file /Users/florian.oswald/.julia/compiled/v1.1/Plots/ld3vC.ji for Plots [91a5bcdd-55d7-5caf-9e0b-520d859cae80]
└ @ Base loading.jl:1184
Out[2]:

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:

test

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 [3]:
] st
Project QuantEconLecturePackages v0.9.4
    Status `~/Dropbox/teaching/ScPo/ScPo-CompEcon/CoursePack/Notebooks/.projects/QuantEconLecturePackages-v0.9.5/Project.toml`
  [6e4b80f9] BenchmarkTools v0.4.1
  [3da002f7] ColorTypes v0.7.5
  [34da2185] Compat v1.4.0
  [a93c6f00] DataFrames v0.15.2
  [1313f7d8] DataFramesMeta v0.4.0
  [39dd38d3] Dierckx v0.4.1
  [31c24e10] Distributions v0.16.4
  [2fe49d83] Expectations v1.0.2
  [f6369f11] ForwardDiff v0.10.1
  [28b8d3ca] GR v0.36.0
  [a98d9a8b] Interpolations v0.11.0
  [5ab0869b] KernelDensity v0.5.1
  [b964fa9f] LaTeXStrings v1.0.3
  [76087f3c] NLopt v0.5.1
  [2774e3e8] NLsolve v3.0.1
  [429524aa] Optim v0.17.2
  [d96e819e] Parameters v0.10.2
  [91a5bcdd] Plots v0.22.0
  [f27b6e38] Polynomials v0.5.1
  [1fd47b50] QuadGK v2.0.3
  [fcd29c91] QuantEcon v0.15.0
  [295af30f] Revise v0.7.14
  [f2b01f46] Roots v0.7.4
  [60ddc479] StatPlots v0.8.2
  [90137ffa] StaticArrays v0.10.0
  [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

  • Students: ask your department if these resources are available
  • 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)