python2.7 version 2.7.18 is available in the Ubuntu 22.04 default repositories. Ubuntu 22.04 is the last LTS version of Ubuntu that contains this package in its default repositories. The phrase "I installed the new Ubuntu 22.04" in your question worries me because the newest Ubuntu LTS is not Ubuntu 22.04; it's Ubuntu 24.04. Please run lsb_release -a to confirm what version of Ubuntu you are using.
The expected results of lsb_release -a are:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.5 LTS
Release: 22.04
Codename: jammy
To install python2.7 in Ubuntu 22.04 open the terminal and type:
sudo apt update
sudo apt install python2.7
Ubuntu 24.04
To install the python2.7 package from the Ubuntu 22.04 default repositories in Ubuntu 24.04 open the terminal and type:
sudo apt update
wget http://security.ubuntu.com/ubuntu/pool/universe/p/python2.7/python2.7_2.7.18-13ubuntu1.5_amd64.deb http://security.ubuntu.com/ubuntu/pool/universe/p/python2.7/libpython2.7-stdlib_2.7.18-13ubuntu1.5_amd64.deb http://security.ubuntu.com/ubuntu/pool/universe/p/python2.7/python2.7-minimal_2.7.18-13ubuntu1.5_amd64.deb http://security.ubuntu.com/ubuntu/pool/universe/p/python2.7/libpython2.7-minimal_2.7.18-13ubuntu1.5_amd64.deb
sudo apt install ./libpython2.7-minimal_2.7.18-13ubuntu1.5_amd64.deb ./libpython2.7-stdlib_2.7.18-13ubuntu1.5_amd64.deb ./python2.7-minimal_2.7.18-13ubuntu1.5_amd64.deb ./python2.7_2.7.18-13ubuntu1.5_amd64.deb
Answer from karel on askubuntu.comWhat is the safest way to work with Python 2 on Ubuntu 24.04?
When can we expect Python 3.10 in Anaconda?
It was added to the conda-forge channel 7 hours ago. You can install it with
conda install -c conda-forge python=3.10More on reddit.com
Python Package Installation though WSL ?
Python dev env setup in WSL2
Videos
Hi all! I just started a new job that is having me analyze/write code in Python 2. The reason it is Python 2 and not Python 3 is because we are supporting older systems that require it, and our code must be backwards compatible with these systems.
With that said, the workstation they've supplied me is running Ubuntu 24.04, and I'm at wits end as to how I can SAFELY run Python 2 code on it. From what I've gathered, recent versions of Ubuntu depend on Python 3 packages so it isn't as simple as adding a legacy apt repo and installing Python 2 - it would break my OS.
So, the question remains: How can I safely work with Python 2 without bricking my OS? Do I need to containerize or use virtualization? Thank you!
There are several options to install Python interpreters without messing up the system. If you're comfortable with Docker, the best option would probably be to use a Python2 Docker container. A Docker container is somewhat similar to a VM, it's an isolated environment on your system, but much more lightweight. When starting the container, you can mount the path of your code into the container and then work from within it without affecting your system. Example (using the Docker image suggested by Artur Meinild in the comments):
docker run --rm -itd -v/path/to/code:/code -w /code --name my-python2-env esolang/python2
docker exec -it my-python2-env bash # this will start a shell within the container
docker stop my-python2-env # stop the container when you don't need it anymore
Another method would be to use anaconda or miniconda. This software allows you to install and manage several Python environments distinct from your system's interpreter. It's like Pip, but much more capable, e.g., it can also install interpreters and even non-Python libraries like CUDA with a simple conda install command. For instructions on how to install Conda, please refer to the official documentation.
You could also install Python2 from source into your home directory (or some other non-system directory). But this method is probably more complicated than the other two solutions and would require you to also install other dependencies from source into a non-system directory as well.
I used miniconda as suggested by Green and here's the breakdown of the steps I took to setup a conda environment using python 2.7.
Install miniconda:open this link, select Linux and follow the instructions. Once you install miniconda (or anaconda) your prompt changes to show which conda environment is active (base by default) CLI prompt with base conda environment active
Create a new conda environment using python 2.7:
conda create -n py27 python=2.7where py27 is the name of the virtual environment.Activate the py27 virtual environment.
conda activate py27The prompt changes to show py27 instead of base. Py27 environment activated.
You're all set up. Now when you run
pythoncommand in the terminal you'll be executingpython 2.7.x. Python 2.7.16 in my case.
Note: whenever you open a new terminal, conda environment will be set to base. If you want to remove the py27 environment, run conda env remove --name py27.