4

I've been using Ubuntu 20.04 for a few months now and things are going well, however recently I ran into 2 legacy applications which I'll need to start supporting that will require Python 2. As best as I can figure, I have at least the following choices:

  • Go back to Ubuntu 18.04.5
  • Install Python 2 in Ubuntu 20.04
  • Attempt to install Python 2 in Ubuntu 20.04 but only in a virtual environment
  • Use a VM

I'd prefer to stay away from the VM option b/c I use GPUs frequently which won't go through a VM and also b/c I do a lot of hardware/software interfacing for which using a VM may cause various problems. Also I don't like VMs generally (crashes, mouse/keyboard/screen hiccups, slow performance, etc.)

I'll be honest and admit I don't use virtual environments much, however I can say I'm not inclined towards this option as the 2 legacy applications I'm going to be supporting call Python various places (calling files that start new processes, etc.) that I suspect a virtual environment won't be able to handle. Also, I'v read about problems with an entire separate version of Python in a virtual environment, ex How to create python2.7 virtualenv on Ubuntu 20.04.

Ubuntu 18.04.5 was great but I'd prefer not to backdate at this point, which leaves me to consider installing Python 2. Upon a quick Googling I found many sites that explain the steps to install Python 2 in Ubuntu 20.04 (ex https://linuxconfig.org/install-python-2-on-ubuntu-20-04-focal-fossa-linux or https://www.vultr.com/docs/how-to-install-python-2-on-ubuntu-20-04), and most of these even include instructions on how to use update-alternatives to make python default to Python 2, which is a necessity in my case for supporting one of the legacy options I mentioned earlier.

I find it odd that something as major as installing an entire other version of Python does not cause problems with Ubuntu, since many Linux utilities today use Python. On the other hand Python 2 is offered as an official package by Canonical https://packages.ubuntu.com/focal/python2 and they are generally very good about package integration.

At this time here are my questions:

  1. Can anybody that has installed Python 2 in Ubuntu 20.04 clarify if this caused problems? Are there any common applications that won't work after installing Python 2? Any other "gotchas" I should know about or look out for pertaining to this?

  2. If I continue with the instructions above (update-alternatives to make python default to Python 2) will that potentially cause problems? Any "gotchas" pertaining to this additional step in particular?

  3. Has anybody tried a Python 2 virtual environment configuration (without a Python 2 native install) in Ubuntu? Is it really as easy as this page https://computingforgeeks.com/how-to-install-python2-with-virtualenv-on-ubuntu/ makes it sound? Will virtual environments work for supporting significant applications that call other scripts, use python (expecting it to be Python 2), span/fork other processes, etc?

  4. If the native Python 2 install on Ubuntu 20.04 and/or the virtual environment options are problematic, are there any other good options I haven't covered here?

8
  • 1
    I in the early focal days needed to install python2 to use scripts, and didn't experience any issues with it. The scripts though were ported to python3 and my need for python2 disappeared & I just stopped using it. python2 is still installed I note, but I haven't used it in months (maybe even a year; I was using focal prior to its release). Refer to Thomas' answer – guiverc Jan 27 at 4:56
  • Forget about VMs for this purpose. The only thing a VM gives you is full kernel/HW isolation, which you really don't need for userspace stuff like this. Use your favourite container solution (Podman, Docker, machinectl, …) instead, where none of the downsides you mentioned apply. – TooTea Jan 27 at 16:34
  • @TooTea Containerization is only easier than a VM here if the applications do not have any graphical components. Running a GUI app inside a Docker container and using the host’s display is non-trivial and can be very error-prone at times because some things do not quite work correctly over a networked connection to the X server. – Austin Hemmelgarn Jan 27 at 16:40
  • As a Rubyist, this always massively confuses me. Ruby 1.9 and Py3k happened around the same time, and back then, I predicted that the Python community's transition would be much faster and much smoother because: a) Python has the PEP process, so future changes are known before they are rolled out, whereas in Ruby, you just have to wait for the release of YARV to come out and discover the changes by yourself. b) Python has the from __future__ import feature. c) Python has the 2to3 and 3to2 tools. d) The changes in Ruby 1.9 were much more invasive than Python 3000. – Jörg W Mittag Jan 27 at 19:14
  • 1
    This is not an answer, but an important comment: RUN AWAY. You can safely close out that you are working on a successful project. – peterh - Reinstate Monica Jan 27 at 21:00
9
  1. Can anybody that has installed Python 2 in Ubuntu 20.04 clarify if this caused problems? Are there any common applications that won't work after installing Python 2? Any other "gotchas" I should know about or look out for pertaining to this?

You can do this, but there may be issues with Python 2 in the near future - pay attention to my later sections of this post!

Installing python2 by default SHOULD make python be equivalent to Python 2 - if it doesn't then...

  1. If I continue with the instructions above (update-alternatives to make python default to Python 2) will that potentially cause problems? Any "gotchas" pertaining to this additional step in particular?

There's already packages for this - python-is-python2 is the one you'll want. Install that once you install Python 2. That'll handle the linking for python to python2.7.

  1. Has anybody tried a Python 2 virtual environment configuration (without a Python 2 native install) in Ubuntu? Is it really as easy as this page https://computingforgeeks.com/how-to-install-python2-with-virtualenv-on-ubuntu/ makes it sound? Will virtual environments work for supporting significant applications that call other scripts, use python (expecting it to be Python 2), span/fork other processes, etc?

I don't use plain virtualenv to get Python installs that differ from the system Python. I use PyEnv for this, because it makes individual userspace installations of Python. You will need the build dependencies for the python and python2.7 installed - sudo apt build-dep python python2.7 should do it.

  1. If the native Python 2 install on Ubuntu 20.04 and/or the virtual environment options are problematic, are there any other good options I haven't covered here?

Neither the native install nor the PyEnv approach will break anything. But there's quite a few things you need to be aware of.

Essentially... Python 2 Is Dead - both Upstream and in PyPA PIP (the pip install ... program)

(NOTE: Some distributions may provide limited support for Python 2, but everything that is still reliant on Python 2 and NOT being migrated to Python 3 should be frowned upon)

Python PIP is not going to be supporting Python 2 as of four days ago with the 21.0 release of the underlying pip module. If you follow the PyEnv or individual separate Python 2 installation separate from System Python and python-pip's install, you will not be able to get Python modules for Python 2 anymore.

Given that you're handling a legacy Python 2 program, I would strongly recommend you work on rewriting the program for Python 3. You are going to run into a TON of issues going forward if you have to maintain this legacy program beyond a year or two since the EOL date of 2020 - support for Python 2 is phased out upstream, and with PIP no longer supporting Python 2 you're going to start running into a ton of problems going forward keeping Python 2 applications around.

For most programs it's not supremely hard to port Python 2 programs to Python 3. If your dependent libraries (and NOT the underlying Python 2 code itself) are only Python 2 then you're going to run into long term support problems - and you REALLY should be looking to replace / upgrade the process.

6
  • It should be noted though that while Python2 is EOL by the PSF, there are distributions out there who will support Python2 for years to come (RHEL supports 2.7 at the very least until 2024). So depending where the program will actually be deployed Python2 can still be fine (although it still makes a ton of sense to port any currently deployed application to 3..). I would make sense to switch to one of those distros for development as well then. – Voo Jan 27 at 14:10
  • 2
    @Voo except if they need newer Python Module deps (or even older legacy ones) such as from PyPI they actually are phasing out Python 2 and have for a while now - they will still face THAT problem as well regardless of distro - eventually old PIP versions will just not work anymore. – Thomas Ward Jan 27 at 14:15
  • Sure, but a legacy application still on 2.x in 2021 probably doesn't need module updates (you should certainly make sure to keep copies of your dependencies though). – Voo Jan 27 at 16:44
  • @Voo well getting old module versions will still break - if pip stops supporting Python 2 requests, then even running an old Python 2 app will require a lot more admin work to get the modules into the runtime space. THAT is the other core problem that I think you're missing here too. (That said, Py2 apps should REALLY be redeveloped for Py3 sooner than later) – Thomas Ward Jan 27 at 17:29
  • Yeah that does sound like a problem. I assume that's why I explicitly mentioned keeping copies of the build dependencies in the previous comment. – Voo Jan 27 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.