Author Archives: Uwe

Installing Julia 1.10 and VSCode

By: Uwe

Re-posted from: https://ufechner7.github.io/2024/08/09/installing-julia-with-juliaup.html

Introduction

Installing Julia is easy, but perhaps you also want to install an integrated development environment (IDE) or a version control system (e.g. git), therefore I give some hints how to do that in this blog post.

Furthermore there are different ways to install multiple Julia versions in parallel and to keep your version up-to-date which are also explained in this blog post.

Highlights of version 1.10 of Julia are explained here .

Installation of Julia

Windows

Windows

Please download and install Julia using juliaup. Launch the Command Prompt app and type:

winget install julia -s msstore
juliaup add 1.10
juliaup update

If that doesn’t work, download https://install.julialang.org/Julia.appinstaller and double-click on the downloaded file to install it.

Optional

It is suggested to install Windows Terminal . Copy and paste works better, unicode works much better and you can use it with bash or Command Prompt, whatever you prefer. It is suggested to set one of these two as default using the Settings menu of Windows Terminal.

Uninstallation

Uninstallation is preferably performed by using the Windows uninstaller. The directory in %HOME%/.julia can then be deleted if you want to remove all traces of Julia (this includes user installed packages).

Linux

Linux

Copy and past the following line to install julia:

curl -fsSL https://install.julialang.org | sh

Restart your terminal, and then execute:

juliaup add 1.10
juliaup update

It is suggested to add the following line to your .bashrc file:

alias jl='./bin/run_julia'

This makes it possible to run Julia with the shortcut jl later, if you have a run_julia script in the bin folder of your projects. I suggest to use such a script, the most simple version of it would just contain the line julia --project .

Mac

Mac

Please download and install juliaup as explained at https://github.com/JuliaLang/juliaup .

Restart your terminal, and then execute:

juliaup add 1.10
juliaup update

Installation of the IDE VSCode

It is useful to install the integrated development environment VSCode, even though it is not required. You can also use any editor of your choice.

VSCode provides syntax highlighting, but also the feature “goto definition” which can help to understand and explore the code.

You can download and install VSCode for all operating systems here .

For Ubuntu Linux the following ppa can be used to install vscode and to keep it up-to-date: https://www.ubuntuupdates.org/ppa/vscode .

Installing the Julia extension

  • Start or open Visual Studio Code.
  • Select View and then click Extensions to open Extension View.
  • Enter the term julia in the marketplace search box. Click the green Install button to download the extension.

Julia VSCode extension

You successfully downloaded the Julia extension for VS Code.

NOTE: It is recommended that you restart VS Code after installation.

Julia development with VSCode is well documented here: Julia Visual Studio Code Documentation

I would NOT use all the advanced features of julia-vscode, I prefer to just use the vscode terminal and launch julia from the terminal. This makes it easy to launch Julia with any command line options and also to start and restart Julia quickly.

Other useful VSCode extensions

  • Project Manager
  • Better TOML
  • Code Spell Checker

VScode supports git out-of-the box.

Installing Julia 1.8 and VSCode

By: Uwe

Re-posted from: https://ufechner7.github.io/2022/08/18/installing-julia.html

Introduction

Installing Julia is easy, but perhaps you also want to install an integrated development environment (IDE) or a version control system (e.g. git), therefore I give some hints how to do that in this blog post.

Furthermore there are different ways to install multiple Julia versions in parallel and to keep your version up-to-date which are also explained in this blog post.

Highlights of version 1.8 of Julia are explained here .

Installation of Julia

Windows

Please download and install Julia as explained here .
Choose the “64-bit (installer)”. Make sure to check the option “Add Julia to path” when running the installer.
For advanced users it is suggested also to install git for Windows which also includes the bash command line interface, very useful for small automation tasks. The git version control system keeps track of the changes of your files and allows SW development in a team.

It is suggested to launch Julia from the command line, using either the “Windows command prompt” or “bash” by typing julia or julia --project (if you work with projects). If you never used a command prompt before, read the Beginners Guide.

Juliaup

An installer and version manager for Julia called juliaup is available in the Microsoft Store. It can be used to install specific Julia versions or update to the latest release. This package handles all PATH related aspects of Julia, and alerts users when new Julia versions are released.

Uninstallation

Uninstallation is preferably performed by using the Windows uninstaller. The directory in %HOME%/.julia can then be deleted if you want to remove all traces of Julia (this includes user installed packages).

Linux

Copy and past the following line to install the latest stable version of Julia:

bash -ci "$(curl -fsSL https://raw.githubusercontent.com/abelsiqueira/jill/master/jill.sh)"

If you want to be able to easily switch between different versions of Julia consider to install
the Python version of jill .

pip install jill --user -U
jill install 1.8

If you should later install version 1.9 with jill install 1.9 you can then switch between the versions with jill switch 1.8 etc.

It is suggested to add the following line to your .bashrc file:

alias jl='./bin/run_julia'

This makes it possible to run julia with the shortcut jl later, if you have a run_julia script in the bin folder of your projects. I suggest to use such a script, the most simple version of it would just contain the line julia --project .

Mac

Please download Julia here .

A julia-1.8.0-mac64.dmg file is provided, which contains Julia-1.8.app. Installation is the same as any other Mac software: drag the Julia-1.8.app to Applications Folder’s Shortcut. The Julia download runs on macOS 10.9 Mavericks and later releases.

You can launch Julia by opening the Julia app like any other application.

Add Julia to PATH

If you want to launch Julia from the command line, first open a new terminal window, then run the following snippet from your shell (e.g., using the Terminal app, not inside the Julia prompt).

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

This code creates a symlink to a Julia version (here 1.8) of your choosing. To launch Julia, simply type julia inside your shell and press return. If you are working with projects, use the command julia --project.

Installing multiple versions in parallel

The jill installers will most likely also work on Mac and allow easy switching of different Julia versions (see Linux section).

Uninstallation

You can uninstall Julia by deleting Julia.app and the packages directory in ~/.julia . Multiple Julia.app binaries can co-exist without interfering with each other. If you would also like to remove your preferences files, remove ~/.julia/config/startup.jl and ~/.julia/logs/repl_history.jl .

Installation of the IDE VSCode

It is useful to install the integrated development environment VSCode, even though it is not
required. You can also use any editor of your choice.

VSCode provides syntax highlighting, but also the feature “goto definition” which can help to understand and explore the code.

You can download and install VSCode for all operating systems here .

For Ubuntu Linux the following ppa can be used to install vscode and to keep it up-to-date: https://www.ubuntuupdates.org/ppa/vscode .

Installing the Julia extension

  • Start or open Visual Studio Code.
  • Select View and then click Extensions to open Extension View.
  • Enter the term julia in the marketplace search box. Click the green Install button to download the extension.

Julia VSCode extension

You successfully downloaded the Julia extension for VS Code.

NOTE: It is recommended that you restart VS Code after installation.

Julia development with VSCode is well documented here: Julia Visual Studio Code Documentation

I would NOT use all the advanced features of julia-vscode, I prefer to just use the vscode terminal and launch julia from the terminal. This makes it easy to launch Julia with any command line options and also to start and restart Julia quickly.

Other useful VSCode extensions

  • Project Manager
  • Better TOML
  • Code Spell Checker

VScode supports git out-of-the box.

Working with Julia projects

By: Uwe

Re-posted from: https://ufechner7.github.io/2022/08/16/julia-projects.html

Introduction

When you start to use Julia you might ask yourself: How shall I structure my code?
There are different approaches for different use cases.

Simple scripts

If you just write short, simple scripts that are not using any packages you can just keep them in one file and put them in any folder, no special folder structure needed. If you care about performance you should put everything in a function. Example:

# constants
const LANG="DE"

# functions
function hello(name)
    if LANG == "DE"
        println("Hallo liebe/r $name !")
    else
        println("Hello dear $name !")
    end
end

# main program
function main()
    hello("Peter")
    hello("Jane")
end

main()
nothing

The structure is:

  • constants
  • functions
  • main function
  • call the main function
  • return nothing

Do not use global variables! That kills your performance [1].

If you store this code in a file with the name hello.jl you can execute it from the REPL with the command:

include("hello.jl")

The advantage of having a main() function is that you can include some error checks and return an error code if they fail. Furthermore you can measure the performance by running:

@time("hello.jl")
@time main()

The first timing you get includes the compilation time, the second number shows the pure execution time.

It is a good habit to return nothing, unless you want to return your result, e.g. a plot (diagram) or a dataset.

Scripts that are using packages

If you are using any packages, you should create a proper project to keep track of your dependencies and their versions.

That is simple:
Creating a project does not mean to create a package. It is much simpler:

mkdir my_project
cd my_project
mkdir src
julia --project="."

Now add the packages you need:

using Pkg
pkg"add Plots"
pkg"add DataFrames"

Now put your code in a file in the src folder, for example like this:

cd src
gedit my_plot.jl

and put the following code into it:

using Plots, DataFrames

function main()
    time = 0:0.01:10             # step range from 0 to 10 step 0.1
    u    = sin.(time*5)          # signal with a frequency of 5 rad/s
    step = 1 .- 1 ./ exp.(time)  # step response
    df  = DataFrame(;time, u, step)
    plt = plot(df.time,  u, legend=false)
    plot!(df.time, step)
    plt
end
plt = main()

and save it.
If you want to run it, make sure you are in the my_project folder and then
start julia with:

julia --project

and execute your script with:

include("src/my_plot.jl")

You should see the following plot:

myplot

When you are happy with your code and the packages you are using, make a backup copy
of your Manifest.toml file:

cp Manifest.toml Manifest.toml.bak

If you – half a year later – update your packages and your code stops to work, just restore the Manifest file:

cp Manifest.toml.bak Manifest.toml

No need to create any module or Julia package…

Using compat to improve long term robustness

If you add compat bounds to the versions of the packages you are using your project becomes more robust. This means,
if you are adding new packages in the future the currently used packages will not be unintentionally upgraded.

If you are using Julia 1.8 or newer you can use the following approach:

  1. launch julia with julia --project
  2. enter the package manager mode by pressing the key ]
  3. list the status of your project:
    (my_project) pkg> st
    Status `~/repos/my_project/Project.toml`
      [a93c6f00] DataFrames v1.3.4
      [91a5bcdd] Plots v1.31.7
    

    If you are careful allow only bugfixes for the installed packages which means only the last number of the version string is allowed to be increased. To achieve that, type

    (my_project) pkg> compat
       Compat `~/repos/my_project/Project.toml`
      Select an entry to edit:
     >            julia      none
    [a93c6f00] DataFrames none
    [91a5bcdd] Plots      none
    

    Now select julia and enter ~1.8, DataFrames and enter ~1.3 and Plots and enter ~1.31. Use the first two numbers of the version strings of the currently installed packages as shown by the command st.
    If you now type compat again it should look like this:

    (my_project) pkg> compat
       Compat `~/repos/my_project/Project.toml`
      Select an entry to edit:
     >            julia      ~1.8
    [a93c6f00] DataFrames ~1.3
    [91a5bcdd] Plots      ~1.31
    

    Press q to quit and then backspace to quit the package manager mode.

Congratulations!

You have now set limits for your package versions, and if you should type up in the package manger the next time you will only get bugfixes for the installed packages, but nothing that could brake your current code.

Your Project.toml file should now look like this:

 ufechner@desktop:~/repos/my_project$ cat Project.toml
[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

[compat]
DataFrames = "~1.3"
Plots = "~1.31"
julia = "~1.8"

If you have a Julia version older than 1.8 you can also just edit the file Project.toml manually with your preferred editor to add the compat section. If your code was tested with multiple package or Julia versions, you can create a list, for example:

 [compat]
 julia = "~1.6,~1.7,~1.8"

Further reading

If you want to understand the meaning of the semantic versioning that is used by Julia packages, please read the section Compatibility in the documentation of the package manager.

Outlook

If you want to create re-usable packages that you want to use in multiple programs/ projects consider to create real Julia packages. This is a little bit more complicated, but it has the advantage of automated unit tests and easy installation for yourself and others. I will talk about that in one of my future blog posts.


[1] You can assign a value with a different type to global variables. As a result Julia has to generate more generic code that is valid for potentially all types which is bad for the performance. From Julia 1.8 onwards you can annotate global variables with a concrete type, e.g. NUMBER::Int64. In this case the performance is better, but still not as good as it is for local variables.