How to Install Python 3.7.2 in Ubuntu 18.04 / 18.10

Last updated: October 12, 2019

This quick tutorial is for those who want to install the latest Python 3.7.2 in Ubuntu 18.04 LTS, Linux Mint 19.x, and Ubuntu 18.10.

Ubuntu 18.04 comes with Python 2.7 and Python 3.6 out-of-the-box, and later includes Python 3.7.1 in its updates (universe) repository.

For the updated Python 3.7.2, you can do following steps to use the ToolChain PPA packages.

1. Open terminal either via Ctrl+Alt+T keyboard shortcut or by searching for “Terminal” from app launcher. When it opens, run command to add the ToolChain PPA:

sudo add-apt-repository ppa:ubuntu-toolchain-r/ppa

Type user password (no visual feedback) when it asks and hit Enter to continue.

2. Then run command to install Python3.7:

sudo apt install python3.7

For Linux Mint 19.x, run command sudo apt update to check updates first.

3. As the PPA contains other updates toolchain packages, e.g., gcc-7.4.0, gcc-8.2.0, and python-3.6.8, you may remove the PPA afterwards via Software & Updates -> Other Software tab.

4. To make python3 use the new installed python 3.7.2 instead of the default 3.6.7, run commands to add python3.7 as choice:

This step may cause issues that depends python3.6! As I know, terminal won’t launch afterwards unless you recreate a symlink to python3.6.

For Ubuntu 18.04, you may run following 2 commands as a workaround:

sudo rm /usr/bin/python3

sudo ln -s python3.6 /usr/bin/python3
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1

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

Then switch between the two versions via command:

sudo update-alternatives --config python3

That’s it. Enjoy!

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via [email protected] Buy me a coffee: https://ko-fi.com/ubuntuhandbook1

12 responses to How to Install Python 3.7.2 in Ubuntu 18.04 / 18.10

  1. Hi and thank you for the walkthrough!
    As announced, the terminal doesn’t open anymore; how do I recreate a symlink to python3.6 (or otherwise make the terminal open again?

    Thanks in advance.

    • For Ubuntu 18.04, open terminal and run following 2 commands:

      sudo rm /usr/bin/python3
      
      sudo ln -s python3.6 /usr/bin/python3
      • is there a way that i don’t have to run these lines for and keep the terminal containing them open every time?

  2. I have the same problem, the gnome terminal could not be opened more. This made missed module named apt_pkg. I end up installed ubuntu again.

  3. root@ubuntu:~# apt-get install python3.7
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package python3.7

  4. i fix it download synaptic packege maneger and seargh for python 3.7 find it green colour becouse installed earler click on it and chose mark for remove . same thinge find python 3.6 and mark for reinstallation ….enjoy/home/mo1/Pictures/Screenshot from 2019-08-15 04-10-13.png

  5. Hi, python 3.7 runs fine. However I cannot create new virtual env using python3 -m venv .env command. It says install python3-venv package, but I already have this package with version 3.6.7. How can I install this package with 3.7 version?

  6. Oh nice! after do this I can’t run apt-get update.

    It returns whole lot bunch of errors.

    “`
    Traceback (most recent call last):
    File “/usr/lib/cnf-update-db”, line 8, in
    from CommandNotFound.db.creator import DbCreator
    File “/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py”, line 11, in
    import apt_pkg
    ModuleNotFoundError: No module named ‘apt_pkg’
    Error in sys.excepthook:
    Traceback (most recent call last):
    File “/usr/lib/python3/dist-packages/apport_python_hook.py”, line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
    File “/usr/lib/python3/dist-packages/apport/__init__.py”, line 5, in
    from apport.report import Report
    File “/usr/lib/python3/dist-packages/apport/report.py”, line 30, in
    import apport.fileutils
    File “/usr/lib/python3/dist-packages/apport/fileutils.py”, line 23, in
    from apport.packaging_impl import impl as packaging
    File “/usr/lib/python3/dist-packages/apport/packaging_impl.py”, line 24, in
    import apt
    File “/usr/lib/python3/dist-packages/apt/__init__.py”, line 23, in
    import apt_pkg
    ModuleNotFoundError: No module named ‘apt_pkg’

    Original exception was:
    Traceback (most recent call last):
    File “/usr/lib/cnf-update-db”, line 8, in
    from CommandNotFound.db.creator import DbCreator
    File “/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py”, line 11, in
    import apt_pkg
    ModuleNotFoundError: No module named ‘apt_pkg’
    Reading package lists… Done
    E: Problem executing scripts APT::Update::Post-Invoke-Success ‘if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi’
    E: Sub-process returned an error code
    “`

  7. hey ! i was excited to upgrade to the package but exactly it broken my whole os

  8. @Ji m — setting “alternatives” for python3 to 3.7 (as shown by you) seems to ALWAYS corrupt terminal and others?! Reading your lines, I thought, it depends on python2 or just pyton – but it seems to depend on python3 leading to python3.6;

    @ALL: my xterm still worked !!
    – maybe ssh from another pc will also do?

    @Ji m — thanks for your workaround …
    – but wouldn’t it be better to mention / edit the blog – not the comments?
    – your “rm /usr/bin/python3” will simply override the “alternatives” to become useless!

    @ALL:
    – workaround IMHO should be setting python3 back to python3.6 (is to python3.7, latest by default, after first use)
    $ sudo update-alternatives python3
    – workaround IMHO around all this blog? just use direct call:
    $ python3.7
    – or setup/change “alternatives” for python (!) to python3.7

    Thanks for the suspense and thri!! – even better than TV or Hitchcock – and much more close and striking :)
    BRs Klaus

  9. tl;dr: do not follow the steps above

    Professional Python programmers normally use ppa:deadsnakes/ppa – a reliable source of all important Python versions for years. This repo also provides the often required related dev-packages and other helpful developer stuff.

    Furthermore: please do not force your system to use your favorite Python version. Your system knows better, what Python version it works with best.

    Oh, and if you install something from a repo (like Python 3.7), please do not disable the source of your package (as shown above) afterwards! This way you disable updates, bug fixes and are asking for trouble – security-wise and dependency-wise.

    What works for me:

    $ sudo apt-add-repository ppa:deadsnakes/ppa
    $ sudo apt update
    $ sudo apt install python3.7 python3.7-dev

    and that’s it. Running

    $ python3

    still gives Python3.6 but this is the way things work best for my system. If I want something to run with Python 3.7, then I call it directly:

    $ python3.7 myscript.py

    or run a virtualenv:

    $ apt install python-virtualenv
    $ virtualenv -p /usr/bin/python3.7 venv37
    $ source ./venv37/bin/activate
    [venv37] $