Installing Julia

Author

Matthew DeHaven

Published

April 1, 2024

This is a guide for installing and setting up Julia to use with Visual Studio Code.

If you do not already have VS Code installed, install it here.

Installing Julia

If you are on macOS, I would recommend using homebrew instead, and installing Julia with the following command:

terminal
brew install julia

Installing the Julia VS Code Extension

In VS Code, we need to install an extension to support Julia.

This is done most easily from within VS Code, by clicking on the “Extensions” tab and searching for “Julia”.

Basic Example

Open a new folder called “test-jl” in VS Code for this example.

Creating a Julia File

  • Add a new file, with the extension “.jl”

VS Code should automatically recognize this is a Julia file.

  • Add some code to the Julia file
msg = "Hello World!"
println(msg)
Hello World!

You have two main options for running the Python Code:

  1. Run the whole file: Click on the “Run” arrow button at the top of the VS Code window

  2. Run the current line or selection: hit Shift+Enter

Option 2 tends to be better for editing and iterating on code. Notice that it starts a live Julia session in a terminal. You can tell it’s a Julia terminal by the prompt: julia>. You may see this called a Julia “REPL” which stands for “read-eval-print loop” and is simply an interactive terminal.

  1. You can also start the Julia REPL from the command palette (Shift+Cmd+P), then searching for “Julia: Start REPL”

Julia Package and Environment Manager

Julia has a built-in package and environment manager: Pkg.jl.

You can install packages system wide, but we are going to first set up an environment for our project. Environments are important to keep the packages installed for each project separate.

  • Launch Julia’s package manager: in the Julia REPL, type ]

This switches the prompt from “julia>” to “(@v1.10) pkg>” (where the version number matches the version of Julia you installed). We can now send commands to the package manager.

  • Activate a project in this folder:
pkg>
activate .

The . represtents here in paths. Activate is a command to start a Julia project, which is the Julia term for an enviroment.

The only change you will notice is that the package manager prompt is now “(test-jl) pkg>”

Installing a Package

  • Install the DataFrames.jl pacakge
add DataFrames

This will download and compile the DataFrames package.

You should see two new files in your folder:

  1. Project.toml
  2. Manifest.toml

“Project.toml” contains a list of all the packages you have installed. “Manifest.toml” contains a list of your Julia version, your package versions, and the packages those packages depend upon.

You can commit these two files when you are using Git and someone will be able to recreate your environment by simply running activate . in this folder.

Exiting the Package Manager

  • Hit “delete” to leave the package manager prompt back to Julia

A gif of entering the package manager, activating an environment, adding a package, and exiting the package manager:

Setting the Environment for VS Code

We need to tell VS Code to use this environment for this workspace.

Here are a couple options of how to do this:

  1. Select ’Julia env: ___’ at the bottom left of the screen. Then choose the workspace environment “test-jl”.

  2. Open Command Palette (Shift+Cmd+P), then choose “Julia: Activate This Environment”

Both options will add a new folder “.vscode”, with a “settings.json” file. Inside you will find a setting for “julia.environmentPath”. This is how VS Code will know what Julia environment to use. This “.vscode” shouldn’t be committed with Git, as it is specific to you and your machine.

Using DataFrames Package

An example using DataFrames after adding it to the environment.

  • Add the following code:
using DataFrames

df = DataFrame(a=1:4, b=["Y", "N", "N", "Y"])
4×2 DataFrame
Row a b
Int64 String
1 1 Y
2 2 N
3 3 N
4 4 Y

The keyword using loads all functions in a Julia package.