Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Last active July 20, 2023 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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.10 [latest python version on Colab].
!wget https://github.com/MohamedElashri/ROOT/releases/download/ubuntu/root_v6.28.04_Ubuntu_20.04.zip
!unzip /content/root_v6.28.04_Ubuntu_20.04.zip
!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
# The following is needed because colab upgraded the openssl library
!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
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

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