Google Colab is a free Jupyter Notebook environment that runs in the cloud. It allows us to write and execute code without having to install any software on your local machine. Colab also provides access to powerful GPUs, which can be useful for running computationally expensive Julia code.
Before we dive into the setup process, let’s briefly explore why Julia is worth our attention:
Speed: Julia is renowned for its impressive execution speed, making it an excellent choice for data analysis, scientific computing, and machine learning tasks.
Versatility: Julia is designed to be versatile, with seamless integration of existing code from other programming languages like Python, R, and MATLAB.
Active Community: The Julia community is vibrant and growing, with an array of packages and libraries that can be leveraged for various applications.
Now, let’s get started with setting up Julia on Google Colab.
To get started, go to Google Colab and sign in with your Google account.
Once you’re signed in, create a new notebook by clicking the “File” drop-down list in the top-left corner and selecting the “New notebook” option. This will open a fresh Jupyter notebook for you to work in.
By default, the Jupyter Notebook only supports Python and R as runtime environments. To execute code in Julia, we need to set our own runtime environment by performing the following steps:
Inside your Colab notebook, write the following commands in a single code block. Here, %%shell
is used to run shell commands in a notebook cell, and set -e
is used to make the script exit immediately upon encountering an error, which is a best practice when automating system-level tasks like software installation.
%%shellset -e
Next, we’ll define some variable configurations for our runtime environment as follows:
JULIA_VERSION="1.8.2" # any version ≥ 0.7.0JULIA_PACKAGES="IJulia BenchmarkTools"JULIA_PACKAGES_IF_GPU="CUDA" # or CuArrays for older Julia versionsJULIA_NUM_THREADS=2
JULIA_VERSION
: It specifies the desired Julia version (in this case, 1.8.2
).
JULIA_PACKAGES
: It is a space-separated list of Julia packages to install. In this example, it includes IJulia
and BenchmarkTools
.
JULIA_PACKAGES_IF_GPU
: They are additional Julia packages to install if a GPU is available. This code installs CUDA
or CuArrays
depending on the GPU’s presence.
JULIA_NUM_THREADS
: It specifies the number of CPU threads to use for Julia.
So far, we have specified which version of Julia we will install and the relevant packages, additional GPU, and the number of CPU threads to use. Next, we’ll install Julia by using the above configuration variables:
if [ -z `which julia` ]; thenJULIA_VER=`cut -d '.' -f -2 <<< "$JULIA_VERSION"`echo "Installing Julia $JULIA_VERSION on the current Colab Runtime..."BASE_URL="https://julialang-s3.julialang.org/bin/linux/x64"URL="$BASE_URL/$JULIA_VER/julia-$JULIA_VERSION-linux-x86_64.tar.gz"wget -nv $URL -O /tmp/julia.tar.gztar -x -f /tmp/julia.tar.gz -C /usr/local --strip-components 1rm /tmp/julia.tar.gz
The if
statement checks if Julia is already installed by attempting to find the Julia executable in the system’s path using the which
command. If Julia is not found, the script proceeds to install it. Inside the if
statement, we perform the following:
We calculate the Julia version number without the patch version (e.g., 1.8
).
We then construct the URL to download the Julia tarball.
We use wget
to download the tarball and tar
to extract the files, placing them in the /usr/local
directory.
We should have successfully installed Julia on our notebook by now. Next, we’ll move on to installing some relevant packages for our Julia runtime environment.
Note: This is a continuation of the above code and is to be executed inside the above if
statement.
# Install Packagesnvidia-smi -L &> /dev/null && export GPU=1 || export GPU=0if [ $GPU -eq 1 ]; thenJULIA_PACKAGES="$JULIA_PACKAGES $JULIA_PACKAGES_IF_GPU"fifor PKG in `echo $JULIA_PACKAGES`; doecho "Installing Julia package $PKG..."julia -e 'using Pkg; pkg"add '$PKG'; precompile;"' &> /dev/nulldone
In the above code, we handle the installation of Julia packages, considering the availability of GPU, as follows:
We check for the presence of an NVIDIA GPU using the nvidia-smi -L
command. If a GPU is found, we set the GPU
variable to 1
; otherwise, it is set to 0
.
Depending on GPU availability, we append the GPU-specific package (CUDA
or CuArrays
) to the list of packages to be installed.
Next, we iterate through the list of packages and use the julia
command to add and precompile each package.
Next, we proceed with setting up our runtime environment, as shown below:
# Install kernel and rename it to "julia"echo "Installing IJulia kernel..."julia -e 'using IJulia; IJulia.installkernel("julia", env=Dict("JULIA_NUM_THREADS"=>"'"$JULIA_NUM_THREADS"'"))'KERNEL_DIR = `julia -e "using IJulia; print(IJulia.kerneldir())"`KERNEL_NAME = `ls -d "$KERNEL_DIR"/julia*`mv -f $KERNEL_NAME "$KERNEL_DIR"/juliafi
Here, we install the Julia kernel for our Jupyter Notebook and rename it to "julia"
as follows:
We use the IJulia
package to install a Julia kernel for Jupyter notebooks with the specified environment variable JULIA_NUM_THREADS
.
The KERNEL_DIR
line determines the directory where the Julia kernel is to be installed and finds the actual kernel name.
We rename the kernel to "julia"
.
Here’s the complete code for you to execute in the Google Colab notebook:
%%shellset -eJULIA_VERSION="1.8.2" # any version ≥ 0.7.0JULIA_PACKAGES="IJulia BenchmarkTools"JULIA_PACKAGES_IF_GPU="CUDA" # or CuArrays for older Julia versionsJULIA_NUM_THREADS=2if [ -z `which julia` ]; thenJULIA_VER=`cut -d '.' -f -2 <<< "$JULIA_VERSION"`echo "Installing Julia $JULIA_VERSION on the current Colab Runtime..."BASE_URL="https://julialang-s3.julialang.org/bin/linux/x64"URL="$BASE_URL/$JULIA_VER/julia-$JULIA_VERSION-linux-x86_64.tar.gz"wget -nv $URL -O /tmp/julia.tar.gztar -x -f /tmp/julia.tar.gz -C /usr/local --strip-components 1rm /tmp/julia.tar.gz# Install Packagesnvidia-smi -L &> /dev/null && export GPU=1 || export GPU=0if [ $GPU -eq 1 ]; thenJULIA_PACKAGES="$JULIA_PACKAGES $JULIA_PACKAGES_IF_GPU"fifor PKG in `echo $JULIA_PACKAGES`; doecho "Installing Julia package $PKG..."julia -e 'using Pkg; pkg"add '$PKG'; precompile;"' &> /dev/nulldone# Install kernel and rename it to "julia"echo "Installing IJulia kernel..."julia -e 'using IJulia; IJulia.installkernel("julia", env=Dict("JULIA_NUM_THREADS"=>"'"$JULIA_NUM_THREADS"'"))'KERNEL_DIR = `julia -e "using IJulia; print(IJulia.kerneldir())"`KERNEL_NAME = `ls -d "$KERNEL_DIR"/julia*`mv -f $KERNEL_NAME "$KERNEL_DIR"/juliafi
Once you’ve executed the above commands, you’ll see some execution followed by an error message, CalledProcessError: Command 'set -e
. You don’t need to worry about it because it will only occur the first time once you execute the above commands. To fix the error, refresh the notebook to load the Julia runtime environment by following the below process.
After reloading the notebook, move to the “Runtime” tab in the toolbar and select the “Change runtime type” option. You’ll see a popup where, from the drop-down menu, you can now select julia 1.8.2
as your runtime environment.
After selecting the runtime environment, you can now verify what version of Julia you’ve installed by typing versioninfo()
. This will print out the version details, including the platform info and environment, as shown below:
Julia Version 1.8.2Commit 36034abf260 (2022-09-29 15:21 UTC)Platform Info:OS: Linux (x86_64-linux-gnu)CPU: 2 × AMD EPYC 7B12WORD_SIZE: 64LIBM: libopenlibmLLVM: libLLVM-13.0.1 (ORCJIT, znver2)Threads: 2 on 2 virtual coresEnvironment:LD_LIBRARY_PATH = /usr/local/nvidia/lib:/usr/local/nvidia/lib64JULIA_NUM_THREADS = 2
Following the above script, you can write code in Julia in a Google Colab notebook.
Free Resources