Introducing Julia/Getting started

From Wikibooks, open books for an open world
Jump to: navigation, search
« Introducing Julia
Getting Started
»
Contents The REPL

Getting started[edit]

To install Julia on your computer, visit http://julialang.org/downloads/ and follow instructions.

You can use Julia online, in a browser. JuliaBox provides online IJulia notebooks, which let you run Julia on a remote machine, using Jupyter (formerly IPython) interactive notebooks. Jupyter is the interactive notebook technology that lets you run code in Julia, Python, and R in a browser window. You'll need to log in using your Google or GitHub identities.

Alternatively, you can install a local version of IJulia by installing IJulia.

On Mac OS X[edit]

On 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
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0-pre.alpha.0 (2017-02-28 13:15 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-apple-darwin13.4.0

julia>

Alternatively, you can type, in a terminal, something like this:

$ /Applications/Julia-0.6.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 the command:

$ 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

Running directly from terminal[edit]

Typically, Julia is installed in /Applications, which isn't included in your PATH, and so the shell won'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-0.6.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:

PATH="/Applications/Julia-0.6.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-0.6.app/Contents/Resources/julia/bin/julia" /usr/local/bin/julia

Whichever method you choose, you can add the relevant command to your ~/.bash_profile file to run every time you create a new shell.

You can use the 'shebang' line at the top of a text file ('script') so that the shell can find Julia and execute the file:

#!/usr/bin/env julia

This also works in a lot of text editors, so that 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[edit]

If 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-0.5.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[edit]

If 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-0.5.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 the shebang line.

Now you can run the script from inside the editor in the same way that you'd run a Perl script.

On Windows[edit]

On 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 %JULUA_HOME% gets expanded properly.

On FreeBSD[edit]

To install Julia on FreeBSD (including TrueOS) or DragonFly BSD you can use either a binary package or using the ports system.

Installing from package[edit]

Installing the Julia package is straight forward. Open up a terminal and type:

$ pkg install julia

To remove the package again you can use:

$ pkg remove julia

Installing from ports[edit]

If 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[edit]

Installing from package[edit]

This 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, 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
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" to list help topics
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version xxxxxxxxxxx
 _/ |\__'_|_|_|\__'_|  |  Commit xxxxxx
|__/                   |  

julia>

Installing from PPA (Ubuntu and derivatives)[edit]

On Ubuntu (and other distributions based on Ubuntu, like elementary or Linux Mint) you can install Julia even easier. You just have to add a repository and install julia from the command line terminal:

$ sudo add-apt-repository ppa:staticfloat/juliareleases
$ sudo add-apt-repository ppa:staticfloat/julia-deps
$ sudo apt-get update
$ sudo apt-get install julia

With this, Julia will be updated together with the other software on your machine. Notice that if you are logged as root, you don't have to use sudo on the commands.

To remove the package, run:

$ sudo apt-get purge julia

And to remove the repository, run:

$ sudo add-apt-repository --remove ppa:staticfloat/juliareleases

Make sure you remove the package before removing the repository.

Arch Linux[edit]

On 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

Fedora[edit]

On 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 it's dependencies (again, if not used by other software on your system), you can run:

$ sudo dnf remove julia

Please note, this applies only to Fedora, downstream distributions such as RHEL or CentOS must check their own repositories to see if Julia is available.

Using Binaries[edit]

You can use Julia direct from the binaries, without installing it on your machine. This is usefull 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, extract to a directory. While in this directory, enter the bin folder and run:

$ ./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.

Running a script with Julia[edit]

To 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.