Setting up Your Julia Environment

Overview

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

Topics:

  1. Installation
  2. Interactive Julia sessions
  3. Running sample programs
  4. Installation of libraries, including the Julia code that underpins these lectures

First Steps

Installation

The first thing you will want to do is install Julia

The best option is probably to install the current release from the download page

  • Read through any download and installation instructions specific to your OS on that page
  • Unless you have good reason to do otherwise, choose the current release rather than nightly build and the platform specific binary rather than source

Assuming there were no problems, you should now be able to start Julia either by

  • navigating to Julia through your menus or desktop icons (Windows, OSX), or
  • opening a terminal and typing julia (Linux)

Either way you should now be looking at something like this (modulo your operating system — this is a Linux machine)

../_images/julia_term_1.png

The program that’s running here is called the Julia REPL (Read Eval Print Loop) or Julia interpreter

Let’s try some basic commands:

../_images/julia_term_2.png

The Julia intepreter has the kind of nice features you expect from a modern REPL

For example,

  • Pushing the up arrow key retrieves the previously typed command
  • If you type ? the prompt will change to help?> and give you access to online documentation
../_images/julia_term_3.png

You can also type ; to get a shell prompt, at which you can enter shell commands

../_images/julia_term_4.png

(Here ls is a UNIX style command that lists directory contents — your shell commands depend on your operating system)

From now on instead of showing terminal images we’ll show interactions with the interpreter as follows

julia> x = 10
10

julia> 2 * x
20

Installing Packages

Julia includes many useful tools in the base installation

However, you’ll quickly find that you also have need for at least some of the many external Julia code libraries

Fortunately these are very easy to install using Julia’s excellent package management system

For example, let’s install DataFrames, which provides useful functions and data types for manipulating data sets

julia> Pkg.add("DataFrames")

Assuming you have a working Internet connection this should install the DataFrames package

If you now type Pkg.status() you’ll see DataFrames and its version number

To pull the functionality from DataFrames into the current session we type using DataFrames

julia> using DataFrames

Now let’s use one of its functions to create a data frame object (something like an R data frame, or a spreadsheet)

julia> df = DataFrame(x1=[1, 2], x2=["foo", "bar"])
2x2 DataFrame
| Row | x1 | x2    |
|-----|----|-------|
| 1   | 1  | "foo" |
| 2   | 2  | "bar" |

One quick point before we move on: Running

julia> Pkg.update()

will update your installed packages and also update local information on the set of available packages

Running Julia Scripts

Let’s say you a have a file on your machine containing Julia code (a “script” file) and you want to run it

How would you do that?

Let’s look at a “primitive” method for running Julia code from inside the Julia REPL

(This is actually the way that we run most of our Julia code day to day)

Suppose we have a Julia script called test_script.jl that we wish to run, with contents

# filename: test_script.jl

for i in 1:3
    println("i = $i")
end

Suppose that this file exists as a plain text file somewhere on your computer

If that somewhere happens to be the present working directory for your REPL then you are good to go

  • The present working directory for your REPL can be found in a Julia session by typing pwd()

Then you can run it directly from within Julia by typing include("test_script.jl")

To try this,

  1. find out what your present working directory by typing pwd()
  2. copy the code above and paste it into a text editor (e.g., Notepad, TextEdit, TextMate)
  3. Save it in the present working directory with name test_script.jl
  4. Now go back to Julia and type include("test_script.jl")

Here’s how it looks on our machine, when test_script.jl sits in directory /home/john/temp

(Paths to files will look different on different operating systems)

julia> pwd()
"/home/john/temp"

julia> include("test_script.jl")  # exists in /home/john/temp
i = 1
i = 2
i = 3

What if the file isn’t in your present working directory?

Two simple options are

  1. copy the file to your present working directory, or
  2. change Julia’s present working directory to the location of the script

For example:

julia> cd("/home/john/temp")  # paths look different on Windows or Mac

Now run using include("test_script.jl") as before

Editing Julia Scripts

Hopefully you can now run Julia scripts

You also need to know how to edit them

Text Editors

Some people like IDEs for coding but we usually use plain text editors

The beauty of text editors is that if you master one of them, you can use it for every coding task you come across, regardless of the language

At a minimum, a text editor for coding should provide

  • syntax highlighting for the languages you want to work with
  • automatic indentation
  • efficient text manipulation (search and replace, copy and paste, etc.)

There are many text editors that speak Julia, and a lot of them are free

Suggestions:

  • Atom is a popular open source next generation text editor
  • Sublime Text is a modern, popular and highly regarded text editor with a relatively moderate learning curve (not free but trial period is unlimited)
  • Emacs is a high quality free editor with a sharper learning curve

Finally, if you want an outstanding free text editor and don’t mind a seemingly vertical learning curve plus long days of pain and suffering while all your neural pathways are rewired, try Vim

IDEs

IDEs (Integrated Development Environments) combine an interpreter and text editing facilities in the one application

For Julia one nice option is Juno

Alternatively there’s Jupyter, which is a little bit different again but has some great features that we now discuss

Jupyter

To work with Julia in a scientific context we need at a minimum

  1. An environment for editing and running Julia code
  2. The ability to generate figures and graphics

One option that provides these features is Jupyter

As a bonus, Jupyter also provides

  • nicely formatted output in the browser, including tables, figures, animation, video, etc.
  • The ability to mix in formatted text and mathematical expressions between cells
  • Functions to generate PDF slides, static html, etc.

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

Installing Jupyter

There are two steps here:

  1. Installing Jupyter itself
  2. Installing IJulia, which serves as an interface between`Jupyter notebooks and Julia

In fact you can get both by installing IJulia

However, if you have the bandwidth, we recommend that you

  1. Do the two steps separately
  2. In the first step, when installing Jupyter, do this by installing the larger package Anaconda Python

The advantage of this approach is that Anaconda gives you not just Jupyter but the whole scientific Python ecosystem

This includes things like plotting tools we’ll make use of later

Installing Anaconda

Installing Anaconda is straightforward: download the binary and follow the instructions

If you are asked during the installation process whether you’d like to make Anaconda your default Python installation, say yes — you can always remove it later

Otherwise you can accept all of the defaults

Note that the packages in Anaconda update regularly — you can keep up to date by typing conda update anaconda in a terminal

Installing IJulia

Now open up a Julia terminal and type

julia> Pkg.add("IJulia")

Warning

The IJulia website states that if you get an error message you should force a rebuild with Pkg.build("IJulia")

If you have problems, consult the installation instructions

Other Requirements

Since IJulia runs in the browser it might be a good time to update your browser

One good option is to install a free modern browser such as Chrome or Firefox

In our experience Chrome plays well with IJulia

Getting Starting

Now either

  1. search for and start the Jupyter notebook application on your machine or

  2. open up a terminal (or cmd in Windows) and type

    jupyter notebook

You should see something (not exactly) like this

../_images/starting_nb_julia.png

The page you are looking at is called the “dashboard”

The address localhost:8888/tree you see in the image indicates that the browser is communicating with a Julia session via port 8888 of the local machine

If you click on “New” you should have the option to start a Julia notebook

../_images/starting_nb_julia_options.png

Here’s what your Julia notebook should look like:

../_images/nb2_julia.png

The notebook displays an active cell, into which you can type Julia commands

Notebook Basics

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

This means that the cell is 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 instead of the usual Enter

../_images/nb3_julia.png

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

Plots

Let’s generate some plots

There are several options we’ll discuss in detail later

For now lets start with Plots.jl

julia> Pkg.add("Plots")

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

using Plots
plot(sin, -2pi, pi, label="sine function")

This is what you should see:

../_images/nb4_julia.png

Working with the Notebook

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

Tab Completion

A simple but useful feature of IJulia is tab completion

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

../_images/nb5_julia.png

IJulia offers up the possible completions

This helps remind you of what’s available and saves a bit of typing

On-Line Help

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

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

../_images/nb6_julia.png

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

../_images/nb7_julia.png

Shell Commands

You can execute shell commands (system commands) in IJulia 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

Working with Files

To run an existing Julia file using the notebook we can either

  1. copy and paste the contents into a cell in the notebook, or
  2. use include("filename") in the same manner as for the Julia interpreter discussed above

More sophisticated methods for working with files are under active development and should be on-line soon

Sharing Notebooks

Notebook files are just text files structured in JSON and typically ending 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

nbviewer

The Jupyter organization has a site for sharing notebooks called nbviewer

The notebooks you see there are static HTML representations of notebooks

However, each notebook can be downloaded as an ipynb file by clicking on the download icon at the top right of its page

Once downloaded you can open it as a notebook, as we discussed just above

QuantEcon

QuantEcon is an organization that promotes development of open source code for economics and econometrics (feel free to get involved!)

QuantEcon.jl

Among other things, QuantEcon supports QuantEcon.jl, an open source code library for quantitative economic modeling in Julia

You can install this package through the usual Julia package manager:

julia> Pkg.add("QuantEcon")

For example, the following code creates a discrete approximation to an AR(1) process

julia> using QuantEcon: tauchen

julia> tauchen(4, 0.9, 1.0)
Discrete Markov Chain
stochastic matrix of type Array{Float64,2}:
4x4 Array{Float64,2}:
 0.945853     0.0541468    2.92863e-10  0.0
 0.00580845   0.974718     0.0194737    1.43534e-11
 1.43534e-11  0.0194737    0.974718     0.00580845
 2.08117e-27  2.92863e-10  0.0541468    0.945853

We’ll learn more about the library as we go along

QuantEcon.applications

The smaller Julia scripts we use in these lectures live in a separate repository called QuantEcon.applications

You can grab a copy of the files in this repo directly by downloading the zip file — try clicking the “Download ZIP” button on the main page

Alternatively, you can get a copy of the repo using Git

For more information see Exercise 1

Exercises

Exercise 1

If you haven’t heard, Git is a version control system — a piece of software used to manage digital projects such as code libraries

In many cases the associated collections of files — called repositories — are stored on GitHub

GitHub is a wonderland of collaborative coding projects

Git is the underlying software used to manage these projects

Git is an extremely powerful tool for distributed collaboration — for example, we use it to share and synchronize all the source files for these lectures

There are two main flavors of Git

  1. the plain vanilla command line version (which we recommend)
  2. the point-and-click GUI versions

As an exercise, try getting a copy of QuantEcon.applications using Git

(Perhaps google for recent recommendations matching your operating system)

If you’ve installed the command line version, open up a terminal (cmd on Windows) and enter

git clone https://github.com/QuantEcon/QuantEcon.applications

This is just git clone in front of the URL for the repository

Even better, sign up to GitHub — it’s free

Look into ‘forking’ GitHub repositories

(Loosely speaking, forking means making your own copy of a GitHub repository, stored on GitHub)

Try forking QuantEcon.applications

Now try cloning it to some local directory, making edits, adding and committing them, and pushing them back up to your forked GitHub repo

For reading on these and other topics, try