21

I need to install an older version of Python to get some software to work. 3.9 is the newest version I can use.

Someone else had this issue and the answer was:

sudo add-apt-repository universe
sudo apt update
sudo apt install python3.9

However this does not work and just gives an error that the 3.9 package can not be found.

So how can I remove 3.10 and get 3.9 instead? Thanks.

fletch
  • 311
  • 2
    Please do not replace: everything in Linux is depending on python3 (and assume it will be python3,9 that you are using?) If you want software to run on older python3 I would suggest to install a linux using 3.1 in virtualbox and use that software in there. – Rinzwind Jun 10 '22 at 20:26
  • 2
    also: see this on how to install TWO python3 versions and how to switch to python3.1 https://askubuntu.com/questions/682869/how-do-i-install-a-different-python-version-using-apt-get Adjust the commands to install 3.1 – Rinzwind Jun 10 '22 at 20:26
  • That's absolutely not necessary @Rinzwind, but I agree your warning ot "not replace" is appropriate. If you start changing symlinks or use update-alternatives you can certainly start inadvertently shutting down your system with the wrong Python, but simply installing another Python binary under it's named path will not allow it to engage with your system by itself, and makes it available for creating virtual environments with it at your option. This is in fact exactly what you link in your second comment suggests. – NeilG Jun 20 '23 at 08:03

3 Answers3

21

I encourage to install just your python dependencies using a python package, as this other question and answer suggest: How do I install a different Python version using apt-get?

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.9
cserpell
  • 340
  • 2
  • 8
  • 1
    This works for Python 3.8, despite the message when adding deadsnakes ppa says it is not provided. – Dženan Apr 12 '23 at 14:14
6

Install anaconda https://phoenixnap.com/kb/how-to-install-anaconda-ubuntu-18-04-or-20-04

Anaconda manages different versions of python and their packages by creating environments that are isolated.

So you can install whatever versions of python without crushing.

conda create --name envp39 python=3.9 

This creates python environment with python 3.9

You can switch version by changing environments with

conda activate envp39

You have to open/install the software in a shell with activated envp39.

Tom
  • 675
  • 1
    having more snakes is in this case a good thing :+ Good one I had not thought about so +1 from me – Rinzwind Jun 11 '22 at 07:27
  • 1
    @Rinzwind I also always think of a snake when the name Python falls, but apparently, the name was initially meant as a wink to the Monty Python show! – vanadium Jun 11 '22 at 08:08
  • 5
    You may also mention pyenv, a more lightweight python version management system: https://github.com/pyenv/pyenv – vanadium Jun 11 '22 at 08:09
  • 2
    @vanadium that's what I use and typically recommend instead of anaconda! I highly recommend PyEnv over Anaconda for how lightweight it is conpared to Anaconda – Thomas Ward Jun 11 '22 at 18:22
  • 2
    I would recommend pyenv if the question is for development with python, not some software dependencies. Anaconda is more brainless I think. – Tom Jun 12 '22 at 01:28
  • It is another way of installing another default python version but it is your answer, so your choice not to mention it. – vanadium Jun 12 '22 at 09:11
  • Instead of the full Anaconda I would use Miniconda: "Miniconda is a free minimal installer for conda." Also see Anaconda vs Miniconda – djvg Aug 24 '22 at 11:44
  • Note that installing Anaconda based on your link is a much cumbersome task than installing the Ubuntu package given by deadsnakes. Also, the link mentions Ubuntu 18.04 and 20.04, and the question is for Ubuntu 22.04. – cserpell Oct 04 '22 at 14:38
3

I think the most pythonic way would be to create a virtual environment and then run your script in the env. For example, if you have miniconda/Anaconda installed,

$ conda create -n your_env_name python=3.9
$ conda activate your_env_name

Install all packages you need:

(your_env_name) $ conda install package_name

If the package is not in conda,

(your_env_name) $ cd ~/your_conda_directory/envs/your_env_name/bin
(your_env_name) $ pip install package_name

Make sure you verify Python version

(your_env_name) $ python --version
Python 3.9.12

And now you are good to go:

(your_env_name) $ python your_code.py

Good luck!