Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Last active July 20, 2023 11:08
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.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()
@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