Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

The current default version of Python running on Google Colab is 3.7, but I need 3.9 for my notebooks to work.

How can I update Google Colab's Python version to 3.9 (or greater)?

In Google Colab you have a Debian-based Linux, and you can do whatever you can on a Debian Linux. Upgrading Python is as easy as upgrading it on your own Linux system.

Detect the current python version in Colab:

!python --version
#Python 3.8.16
  • Install new python version
  • Let's first install and upgrade to Python 3.9:

    #install python 3.9
    !sudo apt-get update -y
    !sudo apt-get install python3.9
    #change alternatives
    !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
    !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
    #check python version
    !python --version
    #3.9.16
    
  • Port Colab kernel to the new installed python
  • As mentioned in the comments, the above commands just add a new python version to your google colab and update the default python for commandline usage. But your runtime packages such as sys are still running on the previous python version. The following commands need to be executed as well, to update the sys version.

    # install pip for new python 
    !sudo apt-get install python3.9-distutils
    !wget https://bootstrap.pypa.io/get-pip.py
    !python get-pip.py
    # credit of these last two commands blongs to @Erik
    # install colab's dependencies
    !python -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor
    # link to the old google package
    !ln -s /usr/local/lib/python3.8/dist-packages/google \
           /usr/local/lib/python3.9/dist-packages/google
    

    Now you can restart runtime and check the sys version. Note that in the new python version you have to install every packages, such as pandas, tensorflow, etc. from scratch.

    Also, note that you can see a list of installed Python versions and switch between them at any time with this command: (If nothing changed after installation, use this command to select python version manually)

    !sudo update-alternatives --config python3
    #after running, enter the row number of the python version you want. 
                    Unfortunately this is not working. It install python 3.9 but the Colab Kernel is still running on 3.7.
    – Conchylicultor
                    Apr 20, 2022 at 11:41
                    @Conchylicultor If you're sure you have already installed Python 3.9, then you can change the default python in use by this command !sudo update-alternatives --config python3 or !sudo update-alternatives --config python manually.
    – Kaveh
                    Apr 21, 2022 at 12:30
                    !python --version show me 3.9.12, but import sys ; sys.version run on the colab itself still show 3.7: imgur.com/a/jfkc9km
    – Conchylicultor
                    Apr 21, 2022 at 12:50
                    @Conchylicultor You mean runtime python. My answer only adds a new python to your runtime for the command line usages. But of course, all developing packages are already running on the default python version. If you want to port all your packages to the new python version you need to do some further steps. Because, with the above steps you have just updated your python, and it is a raw python without any packages even pip.
    – Kaveh
                    Apr 21, 2022 at 15:38
                    @Conchylicultor For example You need to do !sudo apt-get install python3.9-distutils && wget https://bootstrap.pypa.io/get-pip.py && python get-pip.py to install pip for new python. And then I think you need to install fresh ipykernel, and even google colab, and I don't know even it is compatible with new python versions or not.
    – Kaveh
                    Apr 21, 2022 at 15:41
    

    It's also possible to update the kernel without going through ngrok or conda with some creative package installation.

    Raha's answer suggesting making a link between the default google package and the newly installed Python version is the trick that makes this work because, at least with Python 3.9, the version of pandas (0.24.0) that the google package requires fails to build.

    Here's the code I used to install and switch my Colab kernel to Python 3.9:

    #install python 3.9 and dev utils
    #you may not need all the dev libraries, but I haven't tested which aren't necessary.
    !sudo apt-get update -y
    !sudo apt-get install python3.9 python3.9-dev python3.9-distutils libpython3.9-dev
    #change alternatives
    !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
    !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
    #Check that it points at the right location
    !python3 --version
    # install pip
    !curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    !python3 get-pip.py --force-reinstall
    #install colab's dependencies
    !python3 -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor
    # link to the old google package
    !ln -s /usr/local/lib/python3.8/dist-packages/google \
           /usr/local/lib/python3.9/dist-packages/google
    # There has got to be a better way to do this...but there's a bad import in some of the colab files
    # IPython no longer exposes traitlets like this, it's a separate package now
    !sed -i "s/from IPython.utils import traitlets as _traitlets/import traitlets as _traitlets/" /usr/local/lib/python3.9/dist-packages/google/colab/*.py
    !sed -i "s/from IPython.utils import traitlets/import traitlets/" /usr/local/lib/python3.9/dist-packages/google/colab/*.py
    

    If Google updates from Python 3.8, you'll have to change the path to the default package.

    Then go the Runtime menu and select Restart runtime. It should reconnect and choose the updated version of Python as the default kernel. You can check that it worked with:

    #check python version
    import sys
    print(sys.version)
    !python3 --version
    !python --version
                    wow, it works even with Python 3.11! and what even better the syntax highlighting isn't broken like with other solutions! Many thanks!
    – vak
                    Nov 26, 2022 at 11:34
                    Colab runtime crashes frequently after installation of TensorFlow 1.x to python 3.7. I have downgraded python from  python==3.8 to python==3.7. How to fix that runtime crash issue?
    – npn
                    Feb 10 at 9:41
    

    Update 24.12.2022 - Unfortunately, the method does not work anymore.

    This worked for me (copied from GitHub), I successfully installed Python 3.10.

    #The code below installs 3.10 (assuming you now have 3.8) and restarts environment, so you can run your cells.
    import sys #for version checker
    import os #for restart routine
    if '3.10' in sys.version:
      print('You already have 3.10')
    else:
      #install python 3.10 and dev utils
      #you may not need all the dev libraries, but I haven't tested which aren't necessary.
      !sudo apt-get update -y
      !sudo apt-get install python3.10 python3.10-dev python3.10-distutils libpython3.10-dev 
      !sudo apt-get install python3.10-venv binfmt-support #recommended in install logs of the command above
      #change alternatives
      !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
      !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
      # install pip
      !curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10
      !python3 get-pip.py --force-reinstall
      #install colab's dependencies
      !python3 -m pip install setuptools ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor
      #minor cleanup
      !sudo apt autoremove
      #link to the old google package
      !ln -s /usr/local/lib/python3.8/dist-packages/google /usr/local/lib/python3.10/dist-packages/google
      #this is just to verify if 3.10 folder was indeed created
      !ls /usr/local/lib/python3.10/
      #restart environment so you don't have to do it manually
      os.kill(os.getpid(), 9)
    

    To use another python version in google colab, you need to: 1- Installing Anaconda. 2- Adding (fake) google colab library. 3- Starting Jupyterlab. 4- Accessing it with ngrok.

    # install Anaconda3
    !wget -qO ac.sh https://repo.anaconda.com/archive/Anaconda3-2020.07-Linux-x86_64.sh 
    !bash ./ac.sh -b
    # a fake google.colab library
    !ln -s /usr/local/lib/python3.6/dist-packages/google \
           /root/anaconda3/lib/python3.8/site-packages/google
    # start jupyterlab, which now has Python3 = 3.8
    !nohup /root/anaconda3/bin/jupyter-lab --ip=0.0.0.0&
    # access through ngrok, click the link
    !pip install pyngrok -q
    from pyngrok import ngrok
    print(ngrok.connect(8888))
    

    you can also use:

    # Install the python version
    !apt-get install python3.9
    # Select the version
    !python3.9 setup.py
    

    another way is to use a virtual environment with your desired python version:

    virtualenv env --python=python3.9
                    It's unclear: Is it enough to just to virtualenv, or are all the other steps also required? At what point? I.e., once I'm in a colab notebook; or do I need to enter colab in another way? Can this be automated?
    – orome
                    Aug 4, 2021 at 20:12
                    Sorry for being unclear. you can use only the virtual environment without doing any further steps. but you need to install all other libraries or frameworks you need on your virtual environments.
    – Raha Moosavi
                    Aug 4, 2021 at 20:26
    

    In addition to Kaveh's answer, I added the following code. (This colab python version is python 3.8 and I tried to downgrade to python 3.7)

    !pip install google-colab==1.0.0
    # install colab's dependencies
    !python -m pip install ipython==7.9.0 ipython_genutils==0.2.0 ipykernel==5.3.4 jupyter_console==6.1.0 prompt_toolkit==2.0.10 httplib2==0.17.4 astor==0.8.1 traitlets==5.7.1 google==2.0.3
    

    This way, I solved the crashing runtime error.

    Steps to change the Python version of Google Colab:

    In the first step:

    To replace it with the Python version (e.g. 3.7) we want to install, we write the following command in the cell of our Google Colab notebook:

    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7
    

    In the second step:

    We configure this python version:

    sudo update-alternatives --config python3
    

    After this step, we will be asked to make a selection, according to the Python version we want to install.

    Simple as that: -

    !wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py39_4.9.2-Linux-x86_64.sh
    !chmod +x mini.sh
    !bash ./mini.sh -b -f -p /usr/local
    !conda install -q -y jupyter
    !conda install -q -y google-colab -c conda-forge
    !python -m ipykernel install --name "py39" --user
    

    Source: https://colab.research.google.com/drive/1m47aWKayWTwqJG--x94zJMXolCEcfyPS?usp=sharing#scrollTo=r3sLiMIs8If3

    This doesn't work. The linked notebook even says it doesn't work. It has the same issue as the accepted answer––it only changes the commandline version, not the one used by the notebook python cells. – Adair Sep 6, 2022 at 19:05

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.