11

I need to change my python version from 3.8 to 3.6 ? How can I achieve this in Ubuntu 20.04. I tried pyenv, but when I try to use pyenv like pyenv global 3.6.0 then I do python3 and I have still 3.8 verision.

3
  • 1
    Do you really need to change the python version or do you just need to be able to run certain scripts with a different version? Commented Nov 3, 2021 at 18:47
  • I Need to run script with a differen version Commented Nov 3, 2021 at 18:48
  • 3
    Please edit your question and clarify that because changing the global python is a very bad idea and really not a good solution if you only need this for certain scripts. Are you just looking for /usr/bin/python3.6 /path/to/script.py? Commented Nov 3, 2021 at 18:49

2 Answers 2

16

Do not downgrade the system version: it's likely that some parts of the system would stop working. Never change /usr/bin/python3, and avoid putting an older version of python3 before it in the $PATH.

The deadsnakes archive provides packages of most supported Python versions for currently supported Ubuntu LTS versions. To make these packages available, follow the usual instructions to enable a PPA. Then install the package(s) you want.

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6

You can then create a virtual environment for your chosen Python version and with a chosen set of packages.

python3.6 -m venv ~/python/foo-3.6
sh -c '.export PYTHONNOUSERSITE=1;  ~/python/foo-3.6/bin/activate; pip install …'

To run a program in this environment, source the bin/activate script in a shell.

$ bash
$ export PYTHONNOUSERSITE=1
$ . ~/python/foo-3.6/bin/activate
$ ./my_python_program
3
  • 1
    When setting up the venv, it reports and error with ensurepip something non-zero exit status. The package python3.9 installed from deadsnakes doesn't include pip. How do I solve that? Commented Jun 4, 2023 at 16:21
  • @PuiHoLam Ask a new question about your problem and copy-paste the full output. Commented Jun 4, 2023 at 17:33
  • In my case, the non-zero exit code of ensurepip could be resolved by installing additional packages like sudo apt install python3.9 python3.9-venv python3.9-dev Commented Aug 14, 2024 at 5:21
11

Alternatively, you can create a virtual environment.

Suppose you have python 3.8 (or higher) installed on the system, but for a specific task, you need python 3.7 (or lower). The best idea is (not to downgrade) to Create a virtual environment with python 3.7(or any 3.x, change the commands below according to your desired version. Below is an implementation of a virtual environment with python 3.7)

Steps: (Checked August 2022)

  1. Install python 3.7 and it’s virtual environment packages.

    sudo apt-get install python3.7-dev python3.7-venv

NB: If you'll get errors like: E: Couldn’t find any package by glob ‘python3.7’ , stating that the packages can not be installed.

run the following commands below, then re-run the install command above:

apt update

sudo apt install software-properties-common

sudo add-apt-repository ppa:deadsnakes/ppa

  1. Find out where your python 3.7 is located by this command:

    which python3.7 (Should be something like /usr/bin/python3.7, if not found, then install python 3.7 manually)

  2. Create Virtual Environment in the Home directory.

    cd

    mkdir virtual_env

    /usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7

    source ~/virtual_env/venv_with_python3.7/bin/activate

  3. python --version (Should be python 3.7 now)

  4. Done. Python 3.7 can be used in this virtual environment. Type which python, you’ll see you have created python 3.7 in a virtual environment, rather than in the system globally.

    Run deactivate when you need to deactivate.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.