Introducing Julia/Getting started
Getting started
editTo install Julia on your computer, visit http://julialang.org/downloads/ and follow instructions. You can then run the Julia interpreter using a terminal app on your computer. This is known as using the REPL.
Alternatively, you can use Julia online, in your browser, at sites such as NextJournal and Repl.it.
If you’d prefer to work locally, you can also use free but more powerful (and complicated) software packages such as Juno (based on Atom) and VisualStudio Code. Another popular way to run Julia is from a Jupyter notebook, via the IJulia.jl package. Jupyter is the interactive notebook technology that lets you run code in Julia, Python, and R in a browser window. Setting these up for working with Julia is usually straightforward, but you’ll probably have to follow a list of instructions carefully. The simplest way to start is to fire up the REPL.
On macOS
editOn a Mac, you download the Julia DMG, double-click to open it, and drag the icon to the Applications folder. To run Julia, you can double-click the icon of the Julia package in the /Applications folder. This opens the terminal application, and starts a new window. This is the REPL, introduced in the next section:
$ julia
_ _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.5.2 (2020-09-23) _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | julia>
Alternatively, you can type, in a terminal, something like this:
$ /Applications/Julia-1.5.app/Contents/Resources/julia/bin/julia
Here you’re specifying the path name of the Julia binary executable that lives inside the Julia application bundle. The exact version name might be different — check it using this command, which shows all available versions:
$ ls /Applications/Julia*/Contents/Resources/julia/bin/julia /Applications/Julia-0.4.5.app/Contents/Resources/julia/bin/julia /Applications/Julia-0.4.7.app/Contents/Resources/julia/bin/julia /Applications/Julia-0.5.app/Contents/Resources/julia/bin/julia /Applications/Julia-0.6.app/Contents/Resources/julia/bin/julia /Applications/Julia-0.7.app/Contents/Resources/julia/bin/julia /Applications/Julia-1.0.app/Contents/Resources/julia/bin/julia /Applications/Julia-1.2.app/Contents/Resources/julia/bin/julia /Applications/Julia-1.3.app/Contents/Resources/julia/bin/julia /Applications/Julia-1.4.app/Contents/Resources/julia/bin/julia /Applications/Julia-1.5.app/Contents/Resources/julia/bin/julia /Applications/Julia-1.6.app/Contents/Resources/julia/bin/julia
Running directly from terminal
editTypically, Julia is installed in /Applications, which isn't included in your PATH, and so the shell can't find it when you type julia
on the command line.
But there are clever things you can do with paths and profiles, so that you can log in to a terminal and type julia with immediate success.
For example, after you find out the location of the Julia binary executable file (see above), you can define the following alias:
alias julia="/Applications/Julia-1.5.app/Contents/Resources/julia/bin/julia"
Obviously this will have to be updated every time the version number changes.
As an alternative, you could add the /Applications/Julia... path to the PATH variable (the mechanism that the OS uses to find executable programs on your system):
PATH="/Applications/Julia-1.5.app/Contents/Resources/julia/bin/:${PATH}" export PATH
A different approach is to create a link to the executable and put it into the /usr/local/bin
directory (which should already be in your path), so that typing julia
is the exact equivalent of typing /Applications/Julia/.../julia
. This command does that:
ln -fs "/Applications/Julia-1.5.app/Contents/Resources/julia/bin/julia" /usr/local/bin/julia
Whichever method you choose, you can add the relevant command to your ~/.bash_profile
or ~/.zprofile
files, which run every time you start a new shell.
You can add the 'shebang' line at the top of a text file ('script') containing Julia code, so that the shell can find Julia and execute the file:
#!/usr/bin/env julia
This also works in a lot of text editors, where you can choose Run to run the file. This works if the editor reads the user's environment variables before running the file. (But not all do!)
Running a Julia program
editIf you have a text file containing Julia code, you can run it from the command line:
$ julia hello-world.jl
or from within the Julia REPL:
$ julia julia> include("hello-world.jl")
If the first line specifies a Julia interpreter:
#!/Applications/Julia-1.2.app/Contents/Resources/julia/bin/julia
or
#!/usr/bin/env julia
you can run the file like this:
$ ./hello-world.jl
Running a script with Julia
editIf you want to write Julia code in an editor and run it, in true scripting-language fashion, you can. At the top of the script file, add a line like the following:
#!/Applications/Julia-1.2.app/Contents/Resources/julia/bin/julia
where the pathname points to the right place on your system, somewhere inside the relevant Julia application bundle, or:
#!/usr/bin/env julia
This is called the shebang line.
Now you can run the script from inside the editor in the same way that you'd run any other script, such as a shell or Perl script.
Using Homebrew
editIf you're a fan of homebrew
, you should be able to install Julia with:
$ brew install julia
On Windows
editOn a Windows machine, you download the Julia Self-Extracting Archive (.exe) 32-bit or 64-bit. Double-click to start the installation process.
By default, it will install to your AppData folder. You may keep the default or choose your own directory (eg. C:\Julia).
After the installation has finished, you should create a System Environment variable called JULIA_HOME and set its value to the \bin directory under the folder where you installed Julia.
It is important to point JULIA_HOME to the /bin directory instead of the JULIA directory.
Then you can append ;%JULIA_HOME% to your PATH System Environment variable, so you can run scripts from any directory. Make sure that the registry key HKEY_CURRENT_USER\Environment\Path is of type REG_EXPAND_SZ, so %JULIA_HOME% gets expanded properly.
On FreeBSD
editTo install Julia on FreeBSD (including TrueOS) or DragonFly BSD you can use either a binary package or using the ports system.
Installing from package
editInstalling the Julia package is straightforward. Open up a terminal and type:
$ pkg install julia
To remove the package again you can use:
$ pkg remove julia
Installing from ports
editIf you have the ports collection installed on your system (you can do so using running the command portsnap auto) the following is the canonical way to compile and install Julia onto your system:
$ cd /usr/ports/lang/julia/ && make install clean
On Linux
editUsing Binaries
editYou can use Julia direct from the binaries, without installing it on your machine. This is useful if you have old Linux distributions or if you don't have administrator's access to the machine. Just download the binaries from the website, and extract to a directory. Go into this directory (using cd), then into the bin folder. Now type:
$ ./julia
If the program doesn't have permission to run, use the following command to give this permission:
$ chmod +x julia
In principle, this method could be used on any Linux distribution.
A better setup
editIf you want to run it by just typing julia in the terminal, you can set it up as follows. We'll assume you've downloaded the binary and extracted it to the folder /home/user/julia13.
Do one or more of the following:
1: add the following line to your ~/.bashrc file:
alias julia="/home/user/julia13/bin/julia"
(You'll have to re-run this file, either by restarting the terminal or using ~/.bashrc.
2: Add /home/user/julia13/bin/julia to your PATH environment variable.
3: Create a symlink to the julia executable to somewhere that is in PATH. For example:
sudo ln -s /home/user/julia13/bin/julia /usr/local/bin/julia
Installing from package
editThis is the easiest way to install Julia if you're using Linux distributions based on RedHat, Fedora, Debian or Ubuntu. To install, download the respective package from the website (or JuliaPro), and install using your favorite way (double-clicking on the package file usually works). After doing this, Julia will be availabe from command line. On a terminal you can do:
$ julia _ _ _ _(_)_ | Documentation: http://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for "help()", "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version xxxxxxxxxxx _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | julia>
Arch Linux
editOn Arch Linux, Julia is available from community repository, and can be installed running:
$ sudo pacman -S julia
To remove Julia package and it's dependencies (if not used by any other software on your system), you can run:
$ sudo pacman -Rsn julia
Void Linux
editOn Void Linux distributions, Julia is available from main repository, and can be installed running:
$ sudo dpkg-install -Sy julia
Fedora
editOn Fedora distributions, Julia is available from the updates repository (a default repository) and can be installed running:
$ sudo dnf install julia
To remove Julia package and its dependencies (again, if not used by other software on your system), you can run:
$ sudo dnf remove julia
Note that this applies only to Fedora, downstream distributions such as RHEL or CentOS must check their own repositories to see if Julia is available.
Running a script with Julia
editTo tell your operating system that it should run the script using Julia, you can use what is called the shebang syntax. To do this, just use the following line on the very top of your script:
#!/usr/bin/env julia
With this as the first line of the script, the OS will search for "julia" on the path, and use it to run the script.