Author Archives: Steven Whitaker

Understanding Variables and Functions

By: Steven Whitaker

Re-posted from: https://glcs.hashnode.dev/variables-and-functions

Variables and functionsare the building blocksof any programmer’s code.Variables allow computations to be reused,and functions help keep code organized.

In this post,we will cover some of the basicsof variables and functionsin Julia,a relatively new,free, and open-source programming language.In particular,we will discuss what a variable is,what sorts of datacan be assigned to a variable,andhow to define and use functions.

Variables

A variable is a labelused to refer to an object.

julia> a = 11

In the above code snippet,we assigned a value of 1to a variable called a.Now we can use a in other expressions,and the value of a (1 in this case)will be used.

julia> a + 23

We can also reassign variables.

julia> a = 44julia> a = "Hello""Hello"julia> a = [1, 2, 3]3-element Vector{Int64}: 1 2 3

We can even use Unicode characters!We can write many math symbolsby typing the corresponding LaTeX commandand then pressing <tab>.Here,we assign (a Julia constant equal to \( \pi \)and typed with \pi<tab>)to (typed with \theta<tab>).

julia>  =  = 3.1415926535897...

Variables Are Labels

One important thing to remember about variablesis that they are labels for data,not the data itself.Let’s illustrate what that means.At this point,a refers to a Vector.We will create another variable, b,and assign it the value of a.

julia> b = a3-element Vector{Int64}: 1 2 3

Now let’s change one of the elements of b.

julia> b[1] = 100; b3-element Vector{Int64}: 100   2   3

We didn’t change a,so it should be the same as before, right?Nope!

julia> a3-element Vector{Int64}: 100   2   3

What happened?Remember, a is just a labelfor some data (a Vector).When we created b,we created a new labelfor the same data.Both a and b referto the same data,so modifying one modifies the other.

Two labels on the same box

If you want bto have the same values as abut refer to different data,use copy.

julia> b = copy(a)3-element Vector{Int64}: 100   2   3julia> b[1] = 1; b3-element Vector{Int64}: 1 2 3julia> a3-element Vector{Int64}: 100   2   3

Two labels on different boxes

Now that we knowhow to create variables,let’s learn aboutsome basic types of datawe can assign to variables.

Basic Types

Julia has many basic data types.

integer = 9000floating_point = 3.14boolean = trueimaginary = 1 + 2imrational = 4//3char = 'x'str = "a string"array = [1.0, 2.0]

Integers and floating-point numberscan be expressedwith different numbers of bits.

Int64, Int32, Int16       # and moreFloat64, Float32, Float16 # and more

By default,integer numbers(technically, literals)are of type Int64 on 64-bit computersor of type Int32 on 32-bit computers.(Note that Int is shorthand for Int64 or Int32for 64-bit or 32-bit computers, respectively.Therefore, all integer literals are of type Int.)

On the other hand,floating-point numbers (literals)of the form 3.14 or 2.3e5are of type Float64 on all computers,while those of the form 2.3f5are of type Float32.

To use different numbers of bits,just use the appropriate constructor.

julia> Int16(20)20julia> Float16(1.2)Float16(1.2)

Basic Operations

Now we will coversome basic operations.This is by no means an exhaustive list;check out the Julia documentationfor more details.

# Mathaddition = 1 + 2.0subtraction = 1 - 1multiplication = 3 * 4//3division = 6 / 4integer_division = 6  4 # Type \div<tab>power = 2^7# Booleannot = !falseand = true && notor = not || and# Comparisonequality = addition == 1greater = division > integer_divisionchained = addition < subtraction <= power# Stringsstring_concatenation = "hi " * "there"string_interpolation = "1 - 1 = $subtraction"string_indexing = string_interpolation[5]substring = string_concatenation[4:end]parsing = parse(Int, string_indexing)# Arraysa = [1, 2, 3]b = [4, 5, 6]concat_vert = [a; b]concat_horiz = [a b]vector_indexing = b[2]vector_range_indexing = b[1:2]matrix_indexing = concat_horiz[2:3,1]elementwise1 = a .+ 1elementwise2 = a .- b# Displayingprint(addition)println(string_concatenation)@show not@info "some variables" power a

Function Basics

Some of the basic operations we saw above,e.g., parse and print,were functions.As demonstrated above,functions are calledusing the following familiar syntax:

func()           # For no inputsfunc(arg1)       # For one inputfunc(arg1, arg2) # For two inputs# etc.

Note that just writing the function name(i.e., without parentheses)is valid syntax, but it is not a function call.In this case,the function name is treated essentially like a variable,meaning, for example, it can be used as an inputto another function.

For example,one way to compute the sumof the absolute valueof an array of numbersis as follows:

julia> sum(abs, [-1, 0, 1])2

Here,the function abs is not being called (by us)but is used as an input to the function sumas if it were a variable.

Function Vectorization

Often,we have a functionthat operates on a single inputthat we want to applyto every element of an array.Julia provides a convenient syntaxto do so:just add a dot (.).For example,the following takes the absolute valueof every array element:

julia> abs.([-1, 0, 1])3-element Vector{Int64} 1 0 1

There is also a function, map,that does the same thingin this example:

julia> map(abs, [-1, 0, 1])3-element Vector{Int64} 1 0 1

(Note, however,that map and the dot syntaxare not always interchangeable.)

Defining Functions

When writing Julia code,it is convenientto place code inside of functions.There are two main syntaxesfor creating a function.

  1. Using the function keyword:
    function myfunc(x)    return x + 1end
  2. Using the assignment form:
    myfunc2(x, y) = x + y

Optional Arguments

Sometimes we want a functionto have optional inputs.The syntax for specifying optional arguments is

function myfunc3(required, optional = "hello")    println(required, optional)end

Here,optional is optionaland has a default value of "hello"if not provided by the caller.

julia> myfunc3("say ")say hellojulia> myfunc3("see you ", "later")see you later

Keyword Arguments

Another way to specify optional argumentsis to use keyword arguments.The syntax is almost the sameas regular optional arguments,except we use a semicolon (;) instead of a comma (,).

function myfunc4(x; y = 3)    return x * yend

Here,y is optional,but to specify itwe need to use the keyword y.

julia> myfunc4(2)6julia> myfunc4(2; y = 10)20julia> myfunc4(2, 10)ERROR: MethodError: no method matching myfunc4(::Int64, ::Int64)

When calling myfunc4we can also use a commawhen specifying y.

julia> myfunc4(2, y = 1)2

Returning Multiple Values

Sometimes we need a functionto return multiple values.The way to do this in Juliais to return a Tuple.Here’s an example:

function plusminus1(x)    return (x + 1, x - 1)end

Then multiple variables can be assigned at once.

julia> (plus1, minus1) = plusminus1(1)(2, 0)julia> plus12julia> minus10

Note that taking the outputof a function with multiple return valuesand assigning it to a single variablewill assign that variable the whole Tuple of outputs.The following code illustrates thisand shows how to return just one output:

julia> both = plusminus1(1);julia> both(2, 0)julia> (one,) = plusminus1(1);julia> one2

(Note, however, that in this last casethe second output is still computed;it is just immediately discarded,so there are no savings in computation.)

Vectorizing a Function with Multiple Return Values

Vectorizing a function with multiple return valuesrequires a bit more work.For this example,we will use the sincos functionthat computes the sine and cosine simultaneously.We can still use the dot syntax,but we might be tempted to try the following:

julia> (s, c) = sincos.([0, /2, ]);julia> s(0.0, 1.0)julia> c(1.0, 6.123233995736766e-17)

Here, s has the value of sincos(0),not the value of sin.([0, /2, ])like we might have expected.

Instead, we can do the following:

julia> sc = sincos.([0, /2, ])3-element Vector{Tuple{Float64, Float64}}: (0.0, 1.0) (1.0, 6.123233995736766e-17) (1.2246467991473532e-16, -1.0)julia> s = first.(sc)3-element Vector{Float64}: 0.0 1.0 1.2246467991473532e-16julia> c = last.(sc)3-element Vector{Float64}:  1.0  6.123233995736766e-17 -1.0

(Note that instead of using first or last,we could write it this way:output_i = getindex.(sc, i).This way also works for functionsthat return more than two values.)

Summary

In this post,we learned about what a variable isand some basic data types.We also learned abouthow to define and use functions.

There is a lot more we could coverabout these topics,so if you want to learn more,check out the links below,or write a comment belowletting us know what additional concepts or topicsyou would like to see!

Understand variables and functions in Julia?Move on to thenext post to learn how to master the Julia REPL!Or,feel free to take a lookat our other Julia tutorial posts!

Additional Links

Installing Julia and VS Code – A Comprehensive Guide

By: Steven Whitaker

Re-posted from: https://blog.glcs.io/install-julia-and-vscode

Julia is a relatively new,
free, and open-source programming language.
It has a syntax
similar to that of other popular programming languages
such as MATLAB and Python,
but it boasts being able to achieve C-like speeds.

One popular IDE to use with Julia
is Visual Studio Code,
or VS Code.

In this post,
we will learn how to install Julia and VS Code.
We will also learn how to configure VS Code
to get it to work with Julia.

The instructions for installing Julia and VS Code
vary by operating system.
Please use the table of contents
to jump to the instructions
that are relevant to you.
Note that the instructions for configuring VS Code
are the same for all operating systems.

Windows

Installing Julia

  1. Go to https://julialang.org/downloads.
  2. In the table under the heading
    “Current stable release”,
    go to the row labeled “Windows”
    and click the “64-bit (installer)” link.
    (Of course, click the 32-bit link
    if you have a 32-bit machine.)

    Downloading Julia installer on Windows

    (See Installing a Specific Julia Version
    for details on installing an older Julia version.)

  3. Run the installer.
  4. Choose where to install Julia.
    (The default location works well
    unless you have a specific need
    to install Julia somewhere else.)
    Then click “Next”.

    Selecting Julia installation directory

  5. Specify some additional options.

    • “Create a Desktop shortcut”:
      Allows you to run Julia
      by clicking the shortcut
      on your Desktop.
    • “Create a Start Menu entry”:
      Allows you to pin Julia to your Start Menu.
      Also allows you to easily search for Julia
      from the Start Menu.
    • “Add Julia to PATH”:
      (Recommended)
      Allows you to run Julia
      from the command line
      without specifying the full path
      to the Julia executable.
      Also enables easier integration
      with VS Code.

    Then click “Next”.

    Specifying additional options

  6. Installation is complete!
    Click “Finish” to exit the installer.

    Installation is complete

Running Julia

There are a few different ways
to run Julia on Windows.

  • If you created a Desktop shortcut,
    you can double-click the shortcut
    to start Julia.

    Julia Desktop icon in Windows

  • If you created a Start Menu entry,
    you can run Julia from the Start Menu.
    First search for “julia”.

    Start menu search bar

    And then click on the appropriate result
    to start Julia.

    Start menu Julia search results

  • You can also run Julia
    from the command line
    (Command Prompt or PowerShell).
    If Julia was added to PATH,
    you can run

    C:\Users\user> julia
    

    Otherwise,
    you can specify the full (or relative) path.

    C:\Users\user> .\AppData\Local\Programs\Julia-1.9.3\bin\julia.exe
    

After starting Julia
you will be greeted with
a fresh Julia prompt.

Fresh Julia prompt on Windows

Now you know how to install and run Julia on Windows!

Installing VS Code

  1. Go to https://code.visualstudio.com.
  2. Click “Download for Windows”.

    Downloading VS Code installer on Windows

  3. Run the installer.
  4. Click “I accept the agreement”
    and then click “Next”.

    VS Code license agreement on Windows

  5. Choose where to install VS Code.
    (The default location works well
    unless you have a specific need
    to install VS Code somewhere else.)
    Then click “Next”.

    Selecting VS Code installation directory

  6. Specify the Start Menu folder for VS Code.
    (The default folder is fine.)
    Then click “Next”.

    Choosing VS Code start menu folder

  7. Specify additional options if desired.
    (The defaults are fine,
    but if you want a Desktop shortcut
    be sure to select that option.)
    Then click “Next”.

    Specifying additional options

  8. Click “Install”.

    Confirming installation options for VS Code in Windows

  9. Installation is complete!
    Click “Finish” to exit the installer.

    Installation is complete

Running VS Code

You can run VS Code
using any of the methods described above
for running Julia:

  • using the Desktop shortcut.

    VS Code Desktop icon in Windows

  • using the Start Menu.

    Start menu VS Code search results

  • using the command line.
    C:\Users\user> code
    

Now you know how to install and run VS Code on Windows!

Jump to Configuring VS Code for Julia
to learn how to configure VS Code.

macOS

Installing Julia

  1. Go to https://julialang.org/downloads.
  2. In the table under the heading
    “Current stable release”,
    go to the rows labeled “MacOS”
    and click the “64-bit (.dmg)” link for either Apple Silicon
    or the Intel or Rosetta link for older machines or special scenarios.

    Downloading Julia .dmg on macOS

    (See Installing a Specific Julia Version
    for details on installing an older Julia version.)

  3. Open the downloads folder and double-click on the Julia Disk Image you just downloaded.

    Open Julia .dmg on macOS

  4. In the new window, drag Julia into the Applications folder.

    Drag Julia

  5. Allow Julia to be copied with your credentials or Touch ID.

    Allow Julia

  6. Eject the disk image, either with the eject button
    or with right-click and selecting “Eject”.

    Eject Julia Disk Image

  7. We recommend you follow these final instructions
    to make it easier to open Julia on your Mac.
    Open a new terminal window and follow these instructions:

    sudo mkdir -p /usr/local/bin
    sudo rm -f /usr/local/bin/julia
    sudo ln -s /Applications/Julia-1.9.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia
    

    Original source.

Running Julia

You can start Julia through your normal means of opening applications
or through typing julia into your terminal (if you did the last step above).
See Apple’s support if you need help.

After starting Julia
you will be greeted with
a fresh Julia prompt.

Fresh Julia prompt on macOS

Now you know how to install and run Julia on macOS!

Installing VS Code

  1. Go to https://code.visualstudio.com.
  2. Click “Download Mac Universal”.

    Downloading VS Code app on macOS

  3. Open the downloads folder and drag VSCode into the Applications folder.

    Drag VSCode

  4. Allow VSCode to be moved with your credentials or Touch ID.

    Allow VSCode

You can start VS Code through your normal means of opening applications.

Now you know how to install and run VS Code on macOS!

Jump to Configuring VS Code for Julia
to learn how to configure VS Code.

Linux

Installing Julia

  1. Go to https://julialang.org/downloads.
  2. In the table under the heading
    “Current stable release”,
    go to the row labeled “Generic Linux on x86”
    and click the “64-bit (glibc)” link.
    (This link should work for most Linux systems.
    If you have a different computer architecture,
    you probably already know to choose a different download link.)

    Downloading Julia installer on Linux

    (See Installing a Specific Julia Version
    for details on installing an older Julia version.)

  3. Open a terminal
    and navigate to the directory
    where you want to install Julia.
    In this post,
    we will install Julia
    in ~/programs/julia/.
    (Note that everything up to and including the $
    is the terminal prompt,
    so the actual command to run
    is everything after the $.)

    ~$ mkdir -p ~/programs/julia
    ~$ cd ~/programs/julia
    
  4. Move the downloaded archive
    to the current directory.

    ~/programs/julia$ mv ~/Downloads/julia-1.9.3-linux-x86_64.tar.gz .
    
  5. Unarchive the file.
    ~/programs/julia$ tar xzf julia-1.9.3-linux-x86_64.tar.gz
    
  6. Add Julia to your PATH
    by adding the following
    to your .bashrc or .zshrc file.
    (Remember to change /home/user/programs/julia
    to the directory where you installed Julia.)

    export PATH="$PATH:/home/user/programs/julia/julia-1.9.3/bin"
    

    (Restart the terminal to actually update the PATH.)

Julia is now installed!

Running Julia

You can run Julia from the command line.

$ ~/programs/julia/julia-1.9.3/bin/julia

Or, if you added Julia to your PATH:

$ julia

After starting Julia
you will be greeted with
a fresh Julia prompt.

Fresh Julia prompt on Linux

Now you know how to install and run Julia on Linux!

Installing VS Code

The following steps were tested
on a computer running Ubuntu.
They should work as written
for any Debian-based Linux distribution,
but modifications may be necessary
for other Linux distributions.
Note that you will need admin privileges
for one of the steps below.

  1. Go to https://code.visualstudio.com.
  2. Click ” .deb”.

    Downloading VS Code installer on Linux

  3. (Requires admin privileges)
    Either open your file manager
    and double-click the downloaded file,
    or install VS Code
    via the command line.
    (Remember to change the file path/name as needed.)

    $ sudo dpkg -i /home/user/Downloads/code_1.81.1-1691620686_amd64.deb
    

VS Code is now installed!

Running VS Code

You can run VS Code from the command line.

$ code

Now you know how to install and run VS Code on Linux!

Jump to Configuring VS Code for Julia
to learn how to configure VS Code.

Installing a Specific Julia Version

If you need a specific version of Julia,
you can navigate to the older releases page
and find the appropriate installer
for the version you need.
For example,
to install Julia 1.0.5 on Windows,
you would find “v1.0.5” on the left column
and then click on the download link
to download the installer.

Installing Julia 1.0.5

Otherwise,
the installation instructions
should be basically the same.

Configuring VS Code for Julia

After starting VS Code
for the first time
you will be greeted with
“Get Started with VS Code”.

Get Started with VS Code

Feel free to walk through the options,
but for this post
we will ignore them
and just click “< Welcome”
on the top left of the window.
That brings us to the welcome page.

VS Code welcome page

We need to install the Julia extension for VS Code.

  1. Click on the extensions button.

    VS Code extensions button

  2. Search for “julia”.
    Click “install” on the Julia extension.

    Installing Julia VS Code extension

Now we need to make sure
the Julia extension knows where Julia is installed.
If you added Julia to PATH,
this step can be skipped.
However,
if you get errors trying to run Julia in VS Code
or if you find the wrong Julia version is being used
(if you have multiple versions installed),
you can follow these steps.

  1. Open the Julia extension settings
    by clicking the settings icon.
    Click “Extension Settings”.

    Opening Julia extension settings

  2. Search for “executable path”.
    Then type the path to the Julia executable
    in the “Julia: Executable Path” extension setting.

    Telling VS Code where the Julia executable is

Now VS Code is ready for Julia development!

Developing Julia Code in VS Code

  1. Click the file icon in the top left
    to open the Explorer pane.
    Then click “Open Folder”.

    Explorer pane in VS Code

  2. Navigate to a folder
    (or create one)
    where your Julia code will be saved.
    In this post,
    we created a folder called Julia.
  3. Click the “New File” button
    next to the folder in the Explorer pane.
    Then type a name for the file
    where you will write Julia code.
    It should have a .jl extension.
    In this post,
    we created a file called script.jl.

    Creating a new file in VS Code

    VS Code will now open a new tab
    with a blank file
    ready for us to edit.

  4. Add the following code to your file
    and then save it.

    println("Hello, World!")
    
  5. To run the code,
    place your cursor on the line of code to execute
    and then press Shift+Enter.
    This command will start a Julia session
    (if one has not already been started)
    and then run the code.
    (The very first time you run code
    may take a little while
    because packages need to precompile.)

    Hello, World from VS Code

  6. Now add more lines of code.

    function plus1(x)
        x + 1
    end
    
    a = 3
    b = plus1(a)
    
    b == a + 1
    
  7. To run all the code in the file,
    open the command palette
    with Ctrl+Shift+P
    and search for
    “Julia: Execute active file in REPL”.
    Highlight the command and press Enter.

    VS Code command palette

    File execution in VS Code

    Note that two lines of output
    were added to the REPL:
    the println statement
    and the value of b == a + 1
    (the last line that was executed).

  8. Now that some code has run,
    we can look at the Julia pane
    on the left
    and see the variables and function we defined
    in the “Workspace” tab.

    Workspace pane in VS Code

  9. We can also use the REPL directly.

    VS Code Julia REPL

And now you know how to write and run Julia code
in VS Code!

If you would like to see more tips and tricks
for how to use Julia in VS Code,
be sure to comment below!

Summary

In this post,
we learned how to install Julia
and VS Code
on different operating systems.
We also learned how to configure VS Code
for developing Julia code.

Once you have installed Julia,
move on to the
next post to learn about variables and functions!
Or,
feel free to take a look
at our other Julia tutorial posts!

Installing Julia and VS Code – A Comprehensive Guide

By: Steven Whitaker

Re-posted from: https://glcs.hashnode.dev/install-julia-and-vscode

Julia is a relatively new,free, and open-source programming language.It has a syntaxsimilar to that of other popular programming languagessuch as MATLAB and Python,but it boasts being able to achieve C-like speeds.

One popular IDE to use with Juliais Visual Studio Code,or VS Code.

In this post,we will learn how to install Julia and VS Code.We will also learn how to configure VS Codeto get it to work with Julia.

The instructions for installing Julia and VS Codevary by operating system.Please use the table of contentsto jump to the instructionsthat are relevant to you.Note that the instructions for configuring VS Codeare the same for all operating systems.

Windows

Installing Julia

  1. Go to https://julialang.org/downloads.
  2. In the table under the heading”Current stable release”,go to the row labeled “Windows”and click the “64-bit (installer)” link.(Of course, click the 32-bit linkif you have a 32-bit machine.)

    Downloading Julia installer on Windows

    (See Installing a Specific Julia Versionfor details on installing an older Julia version.)

  3. Run the installer.
  4. Choose where to install Julia.(The default location works wellunless you have a specific needto install Julia somewhere else.)Then click “Next”.

    Selecting Julia installation directory

  5. Specify some additional options.

    • “Create a Desktop shortcut”:Allows you to run Juliaby clicking the shortcuton your Desktop.
    • “Create a Start Menu entry”:Allows you to pin Julia to your Start Menu.Also allows you to easily search for Juliafrom the Start Menu.
    • “Add Julia to PATH”:(Recommended)Allows you to run Juliafrom the command linewithout specifying the full pathto the Julia executable.Also enables easier integrationwith VS Code.

    Then click “Next”.

    Specifying additional options

  6. Installation is complete!Click “Finish” to exit the installer.

    Installation is complete

Running Julia

There are a few different waysto run Julia on Windows.

  • If you created a Desktop shortcut,you can double-click the shortcutto start Julia.

    Julia Desktop icon in Windows

  • If you created a Start Menu entry,you can run Julia from the Start Menu.First search for “julia”.

    Start menu search bar

    And then click on the appropriate resultto start Julia.

    Start menu Julia search results

  • You can also run Juliafrom the command line(Command Prompt or PowerShell).If Julia was added to PATH,you can run
    C:\Users\user> julia

    Otherwise,you can specify the full (or relative) path.

    C:\Users\user> .\AppData\Local\Programs\Julia-1.9.3\bin\julia.exe

After starting Juliayou will be greeted witha fresh Julia prompt.

Fresh Julia prompt on Windows

Now you know how to install and run Julia on Windows!

Installing VS Code

  1. Go to https://code.visualstudio.com.
  2. Click “Download for Windows”.

    Downloading VS Code installer on Windows

  3. Run the installer.
  4. Click “I accept the agreement”and then click “Next”.

    VS Code license agreement on Windows

  5. Choose where to install VS Code.(The default location works wellunless you have a specific needto install VS Code somewhere else.)Then click “Next”.

    Selecting VS Code installation directory

  6. Specify the Start Menu folder for VS Code.(The default folder is fine.)Then click “Next”.

    Choosing VS Code start menu folder

  7. Specify additional options if desired.(The defaults are fine,but if you want a Desktop shortcutbe sure to select that option.)Then click “Next”.

    Specifying additional options

  8. Click “Install”.

    Confirming installation options for VS Code in Windows

  9. Installation is complete!Click “Finish” to exit the installer.

    Installation is complete

Running VS Code

You can run VS Codeusing any of the methods described abovefor running Julia:

  • using the Desktop shortcut.

    VS Code Desktop icon in Windows

  • using the Start Menu.

    Start menu VS Code search results

  • using the command line.
    C:\Users\user> code

Now you know how to install and run VS Code on Windows!

Jump to Configuring VS Code for Juliato learn how to configure VS Code.

macOS

Installing Julia

  1. Go to https://julialang.org/downloads.
  2. In the table under the heading”Current stable release”,go to the rows labeled “MacOS”and click the “64-bit (.dmg)” link for either Apple Siliconor the Intel or Rosetta link for older machines or special scenarios.

    Downloading Julia .dmg on macOS

    (See Installing a Specific Julia Versionfor details on installing an older Julia version.)

  3. Open the downloads folder and double-click on the Julia Disk Image you just downloaded.

    Open Julia .dmg on macOS

  4. In the new window, drag Julia into the Applications folder.

    Drag Julia

  5. Allow Julia to be copied with your credentials or Touch ID.

    Allow Julia

  6. Eject the disk image, either with the eject buttonor with right-click and selecting “Eject”.

    Eject Julia Disk Image

  7. We recommend you follow these final instructionsto make it easier to open Julia on your Mac.Open a new terminal window and follow these instructions:

    sudo mkdir -p /usr/local/binsudo rm -f /usr/local/bin/juliasudo ln -s /Applications/Julia-1.9.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia

    Original source.

Running Julia

You can start Julia through your normal means of opening applicationsor through typing julia into your terminal (if you did the last step above).See Apple’s support if you need help.

After starting Juliayou will be greeted witha fresh Julia prompt.

Fresh Julia prompt on macOS

Now you know how to install and run Julia on macOS!

Installing VS Code

  1. Go to https://code.visualstudio.com.
  2. Click “Download Mac Universal”.

    Downloading VS Code app on macOS

  3. Open the downloads folder and drag VSCode into the Applications folder.

    Drag VSCode

  4. Allow VSCode to be moved with your credentials or Touch ID.

    Allow VSCode

You can start VS Code through your normal means of opening applications.

Now you know how to install and run VS Code on macOS!

Jump to Configuring VS Code for Juliato learn how to configure VS Code.

Linux

Installing Julia

  1. Go to https://julialang.org/downloads.
  2. In the table under the heading”Current stable release”,go to the row labeled “Generic Linux on x86″and click the “64-bit (glibc)” link.(This link should work for most Linux systems.If you have a different computer architecture,you probably already know to choose a different download link.)

    Downloading Julia installer on Linux

    (See Installing a Specific Julia Versionfor details on installing an older Julia version.)

  3. Open a terminaland navigate to the directorywhere you want to install Julia.In this post,we will install Juliain ~/programs/julia/.(Note that everything up to and including the $is the terminal prompt,so the actual command to runis everything after the $.)
    ~$ mkdir -p ~/programs/julia~$ cd ~/programs/julia
  4. Move the downloaded archiveto the current directory.
    ~/programs/julia$ mv ~/Downloads/julia-1.9.3-linux-x86_64.tar.gz .
  5. Unarchive the file.
    ~/programs/julia$ tar xzf julia-1.9.3-linux-x86_64.tar.gz
  6. Add Julia to your PATHby adding the followingto your .bashrc or .zshrc file.(Remember to change /home/user/programs/juliato the directory where you installed Julia.)
    export PATH="$PATH:/home/user/programs/julia/julia-1.9.3/bin"

    (Restart the terminal to actually update the PATH.)

Julia is now installed!

Running Julia

You can run Julia from the command line.

$ ~/programs/julia/julia-1.9.3/bin/julia

Or, if you added Julia to your PATH:

$ julia

After starting Juliayou will be greeted witha fresh Julia prompt.

Fresh Julia prompt on Linux

Now you know how to install and run Julia on Linux!

Installing VS Code

The following steps were testedon a computer running Ubuntu.They should work as writtenfor any Debian-based Linux distribution,but modifications may be necessaryfor other Linux distributions.Note that you will need admin privilegesfor one of the steps below.

  1. Go to https://code.visualstudio.com.
  2. Click ” .deb”.

    Downloading VS Code installer on Linux

  3. (Requires admin privileges)Either open your file managerand double-click the downloaded file,or install VS Codevia the command line.(Remember to change the file path/name as needed.)
    $ sudo dpkg -i /home/user/Downloads/code_1.81.1-1691620686_amd64.deb

VS Code is now installed!

Running VS Code

You can run VS Code from the command line.

$ code

Now you know how to install and run VS Code on Linux!

Jump to Configuring VS Code for Juliato learn how to configure VS Code.

Installing a Specific Julia Version

If you need a specific version of Julia,you can navigate to the older releases pageand find the appropriate installerfor the version you need.For example,to install Julia 1.0.5 on Windows,you would find “v1.0.5” on the left columnand then click on the download linkto download the installer.

Installing Julia 1.0.5

Otherwise,the installation instructionsshould be basically the same.

Configuring VS Code for Julia

After starting VS Codefor the first timeyou will be greeted with”Get Started with VS Code”.

Get Started with VS Code

Feel free to walk through the options,but for this postwe will ignore themand just click “< Welcome”on the top left of the window.That brings us to the welcome page.

VS Code welcome page

We need to install the Julia extension for VS Code.

  1. Click on the extensions button.

    VS Code extensions button

  2. Search for “julia”.Click “install” on the Julia extension.

    Installing Julia VS Code extension

Now we need to make surethe Julia extension knows where Julia is installed.If you added Julia to PATH,this step can be skipped.However,if you get errors trying to run Julia in VS Codeor if you find the wrong Julia version is being used(if you have multiple versions installed),you can follow these steps.

  1. Open the Julia extension settingsby clicking the settings icon.Click “Extension Settings”.

    Opening Julia extension settings

  2. Search for “executable path”.Then type the path to the Julia executablein the “Julia: Executable Path” extension setting.

    Telling VS Code where the Julia executable is

Now VS Code is ready for Julia development!

Developing Julia Code in VS Code

  1. Click the file icon in the top leftto open the Explorer pane.Then click “Open Folder”.

    Explorer pane in VS Code

  2. Navigate to a folder(or create one)where your Julia code will be saved.In this post,we created a folder called Julia.
  3. Click the “New File” buttonnext to the folder in the Explorer pane.Then type a name for the filewhere you will write Julia code.It should have a .jl extension.In this post,we created a file called script.jl.

    Creating a new file in VS Code

    VS Code will now open a new tabwith a blank fileready for us to edit.

  4. Add the following code to your fileand then save it.
    println("Hello, World!")
  5. To run the code,place your cursor on the line of code to executeand then press Shift+Enter.This command will start a Julia session(if one has not already been started)and then run the code.(The very first time you run codemay take a little whilebecause packages need to precompile.)

    Hello, World from VS Code

  6. Now add more lines of code.

    function plus1(x)    x + 1enda = 3b = plus1(a)b == a + 1
  7. To run all the code in the file,open the command palettewith Ctrl+Shift+Pand search for”Julia: Execute active file in REPL”.Highlight the command and press Enter.

    VS Code command palette

    File execution in VS Code

    Note that two lines of outputwere added to the REPL:the println statementand the value of b == a + 1(the last line that was executed).

  8. Now that some code has run,we can look at the Julia paneon the leftand see the variables and function we definedin the “Workspace” tab.

    Workspace pane in VS Code

  9. We can also use the REPL directly.

    VS Code Julia REPL

And now you know how to write and run Julia codein VS Code!

If you would like to see more tips and tricksfor how to use Julia in VS Code,be sure to comment below!

Summary

In this post,we learned how to install Juliaand VS Codeon different operating systems.We also learned how to configure VS Codefor developing Julia code.

Once you have installed Julia,move on to thenext post to learn about variables and functions!Or,feel free to take a lookat our other Julia tutorial posts!