Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Last active March 20, 2025 23:01
Show Gist options
  • Save MohamedElashri/1b0367992988f053a02dbfd5797fb59e to your computer and use it in GitHub Desktop.
Save MohamedElashri/1b0367992988f053a02dbfd5797fb59e to your computer and use it in GitHub Desktop.
Run ROOT on google colab, working with python 3.11 [latest python version on Colab]. More information at https://melashri.net/ROOT
# Step 1: Download the pre-built ROOT tarball from GitHub Releases
!wget -q --show-progress https://github.com/MohamedElashri/ROOT/releases/download/v6.30.04_python11/root_v6.30.04_Ubuntu_Python3.11.zip
# Step 2: Extract the ROOT files
!unzip -q root_v6.30.04_Ubuntu_Python3.11.zip
# Step 3: Install missing system dependencies for ROOT
!sudo ldconfig & apt-get install -y git dpkg-dev cmake g++ gcc binutils libx11-dev libxpm-dev libxft-dev libxext-dev tar gfortran subversion libpython3.11-dev
# Step 4: Remove the tarball to free up space
!rm -f root_v6.30.04_Ubuntu_Python3.11.zip
# Step 5: Install Compatible libssl
!wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
!sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb
!rm -f libssl1.1_1.1.1f-1ubuntu2_amd64.deb
import sys
sys.path.append("/content/root_build/")
sys.path.append("/content/root_build/bin/")
sys.path.append("/content/root_build/include/")
sys.path.append("/content/root_build/lib/")
import ctypes
ctypes.cdll.LoadLibrary('/content/root_build/lib//libCore.so')
ctypes.cdll.LoadLibrary('/content/root_build/lib//libThread.so')
ctypes.cdll.LoadLibrary('/content/root_build/lib//libTreePlayer.so')
import ROOT
h = ROOT.TH1F("gauss","Example histogram",100,-4,4)
h.FillRandom("gaus")
c = ROOT.TCanvas("myCanvasName","The Canvas Title",800,600)
h.Draw()
c.Draw()
@zahrasadat9
Copy link

hi!
I know it has been a long time since you posted this
but I'm getting the below error
[ I'm a total newbie btw ]

https://files.pythonhosted.org/packages/02/90/d9ef075a5efb64959fbabfe2f8a61b14bb4d28ab3f359af39529c407b90d/root_numpy-2.0.1.tar.gz#sha256=b22148f327944bf7f47e6c08794227f97319422014c5af8f923c2810cab93573 (from https://pypi.org/simple/root-numpy/).

ERROR: Could not find a version that satisfies the requirement root_numpy (from versions: 2.0.1, 2.0.2, 2.0.3, 2.0.4, 2.1.0, 3.0.0, 3.0.1, 3.0.2, 3.1.0, 3.1.1, 3.2.0, 3.3.0, 3.3.1, 3.4.0, 3.4.1, 4.0.0, 4.0.1, 4.1.0, 4.1.1, 4.1.2, 4.2.0, 4.2.1, 4.3.0, 4.4.0, 4.4.1, 4.5.0, 4.5.1, 4.5.2, 4.6.0, 4.7.1, 4.7.2, 4.7.3, 4.8.0)
ERROR: No matching distribution found for root_numpy

any idea?
thanks!

@MohamedElashri
Copy link
Author

Hi @zahrasadat9

Yes, I have figured out the problem. As you said this is an old code. things for root_numpypackage changed since then.
In the code there is pip install root_numpy line which at the point when this code was written referred to compatible version with ROOT 6.14 I made. But now it is not the case.

Anyway it is optional usage and ROOT will work without it. Just remove this line
But if you need it also, just run the line in a separate cell after you run the original code without this line and the package will install.

Example notebook on Colab is here

Good luck

@vijaypanchalr3
Copy link

vijaypanchalr3 commented Jul 4, 2023

hi, i'm trying to run ROOT on colab and find your gist, i checked your colab notebook but when i ran it and even i ran yours than i'm getting following error.

ImportError Traceback (most recent call last)
in <cell line: 11>()
9 ctypes.cdll.LoadLibrary('/content/root_build/lib//libTreePlayer.so')
10
---> 11 import ROOT
12
13 h = ROOT.TH1F("gauss","Example histogram",100,-4,4)

1 frames
/content/root_build/lib/cppyy.py in
59 sys.setdlopenflags( 0x100 | 0x2 ) # RTLD_GLOBAL | RTLD_NOW
60
---> 61 import libPyROOT as _backend
62
63 # reset dl flags if needed

ImportError: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory

@MohamedElashri
Copy link
Author

hi, i'm trying to run ROOT on colab and find your gist, i checked your colab notebook but when i ran it and even i ran yours than i'm getting following error.

ImportError Traceback (most recent call last) in <cell line: 11>() 9 ctypes.cdll.LoadLibrary('/content/root_build/lib//libTreePlayer.so') 10 ---> 11 import ROOT 12 13 h = ROOT.TH1F("gauss","Example histogram",100,-4,4)

1 frames /content/root_build/lib/cppyy.py in 59 sys.setdlopenflags( 0x100 | 0x2 ) # RTLD_GLOBAL | RTLD_NOW 60 ---> 61 import libPyROOT as _backend 62 63 # reset dl flags if needed

ImportError: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory

Thanks for bringing this to my attention, I didn't use ROOT on colab for a long time. Colab now updated runtime python to python3.10 and does not have the python3.6 shared libraries that this trick use. I will need to build a new version for ROOT and the libs needed. I don't have time for that but there is a workaround that should give you ROOT on colab but will probably create more problems if you depend on other packages (new version of them)

We can make colab use python3.6 which will work with the current ROOT build. There might other clever methods but this worked for me as of 4th of July 2023

We will create a virtual env for python3.6 and use it instead of version that comes by default with Colab runtime

1- Install the required packages and create a virtual environment with Python 3.6:

!apt-get install python3.6 python3.6-dev python3.6-venv

2- Create the env

!python3.6 -m venv py36env
  1. Activate the virtual environment:
!source py36env/bin/activate

4- Install pip for Python 3.6:

!curl https://bootstrap.pypa.io/pip/3.6/get-pip.py -o get-pip.py
!python3.6 get-pip.py
  1. check that now we have pip with python3.6
!python3.6 -m pip --version

This should give you something like this

pip 21.3.1 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6)

6- Install the required packages inside the virtual environment:

!pip install ipykernel

7- Add the virtual environment as a new kernel to Jupyter:

!python3.6 -m ipykernel install --user --name=py36env

8- run this gist code

!wget https://github.com/MohamedElashri/HEP-ML/releases/download/ROOT/ROOT.tar.zip
!unzip /content/ROOT.tar.zip
!tar -xf  ROOT.tar
!apt-get install git dpkg-dev cmake g++ gcc binutils libx11-dev libxpm-dev libxft-dev libxext-dev tar gfortran subversion
!apt-get inatall libpython3.6-dev
import sys
sys.path.append("/content/root_build/")
sys.path.append("/content/root_build/bin/")
sys.path.append("/content/root_build/include/")
sys.path.append("/content/root_build/lib/")
import ctypes
ctypes.cdll.LoadLibrary('/content/root_build/lib//libCore.so')
ctypes.cdll.LoadLibrary('/content/root_build/lib//libThread.so')
ctypes.cdll.LoadLibrary('/content/root_build/lib//libTreePlayer.so')
import ROOT
h = ROOT.TH1F("gauss","Example histogram",100,-4,4)
h.FillRandom("gaus")
c = ROOT.TCanvas("myCanvasName","The Canvas Title",800,600)
h.Draw()
c.Draw()

Now you will have a ROOT working again. This is probably not the best solution, and you might see problems down the line, but the optimal solution would be to build ROOT for python3.10 and provide the pre-built (ROOT team doesn't provide python3.10 compatible pre-build version on ubuntu)

I also updated my notebook with the workaround presented here

@MohamedElashri
Copy link
Author

MohamedElashri commented Jul 5, 2023

Update: I built a ROOT version with python3.10 and updated the gist. You can use the method now without python3.6 virtual env trick. Example is provided here

I forgot to add that I updated the ROOT version to v6.28 instead of the old v6.14

@Jordan597
Copy link

Jordan597 commented Feb 21, 2025

Hi Mohamed. Today 21/02/2025, how to install the ROOT CERN in Google Colab on https://colab.research.google.com/ ?

Thanks in advance !

@MohamedElashri
Copy link
Author

MohamedElashri commented Feb 23, 2025

Hi Mohamed. Today 21/02/2025, how to install the ROOT CERN in Google Colab on colab.research.google.com ?

Thanks in advance !

Hi it seems colab again updated python version to be py3.11 so the current will not work.

I have built ROOT v6.30.04 for py3.11 and uploaded it to releases. So you can use the example in this notebook which will work.

I think I will need more organized and automated way to do this which I'm currently implementing that will not rely on manual work. I will update this gist when I'm done.

@Jordan597
Copy link

Olá Mohamed. Hoje 21/02/2025, como instalar o ROOT CERN no Google Colab em colab.research.google.com ?
Desde já, obrigado !

Olá, parece que o Colab atualizou novamente a versão do Python para py3.11, então a atual não funcionará.

Eu construí o ROOT v6.30.04 para py3.11 e o carreguei para releases. Então você pode usar o exemplo neste notebook que funcionará.

Acho que vou precisar de uma maneira mais organizada e automatizada de fazer isso, que estou implementando atualmente, que não dependerá de trabalho manual. Atualizarei este gist quando terminar.

Thank you Mohamed. It is good to hear that someone wants to help us with this question!!

@Jordan597
Copy link

When I run the code it returns the following error on the line ctypes.cdll.LoadLibrary("root_build/lib/libCore.so")

OSError: /lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.38' not found (required by root_build/lib/libCore.so)

@MohamedElashri
Copy link
Author

When I run the code it returns the following error on the line ctypes.cdll.LoadLibrary("root_build/lib/libCore.so")

OSError: /lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.38' not found (required by root_build/lib/libCore.so)

Sorry I did this mistake. I will fix it but will take two hours until build get updated. in the mean time change the downloaded link in first cell by this zip file. Change the first line to this.

!wget -q --show-progress https://github.com/MohamedElashri/ROOT/releases/download/ubuntu/root_v6.30.04_Ubuntu_Python3.11.zip

Or just try to work from this notebook

@Jordan597
Copy link

It's working for now

@MohamedElashri
Copy link
Author

It's working for now

Great, enjoy using it and please tell if at any point it stopped working.

@Jordan597
Copy link

Captura de tela 2025-02-28 112744

@Jordan597
Copy link

When i try to import root_numpy it gives me

Captura de tela 2025-03-20 174903

Thank you!

@MohamedElashri
Copy link
Author

When i try to import root_numpy it gives me

Captura de tela 2025-03-20 174903

Thank you!

There are several problems

1- You didn't install root_numpy
2- root_numpy is not part of ROOT and is a separate software
3- It is unmaintained for years now and not will work with new python version (certainly not on Colab)

So you shouldn't use it but work with PyROOT which is the python wrapper of ROOT that this installs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment