11

I have installed ubuntu 18.04 in virtual box and when trying python -v, it says you have python3 installed, I want to remove python 3 or disable so that can use python2.7 and run some scripts i have.

slava
  • 4,085
Rahul
  • 151

5 Answers5

21

Python 2 is no longer installed by default in fresh installations of Ubuntu 18.04 and later. Don't remove python3 from Ubuntu 18.04 and later or else Ubuntu Software, the terminal and many other apps that are installed by default will stop working. If you removed Python 3 and now Ubuntu Software, terminal and other applications don't work follow the instructions in this answer to reinstall it and get all applications working again.

To install Python 2.7 in Ubuntu 18.04 and later open the terminal and type:

sudo apt install python2.7  

To start the Python 2.7 interpreter run this command:

python2.7

To start the Python 3 interpreter run this command:

python3  

Either way the Python interpreter will show a version message when it is started that shows what version of Python you are running.

karel
  • 122,292
  • 133
  • 301
  • 332
7

1) To install Python 2 version on Ubuntu 18.04 open up terminal and enter:

sudo apt install python-minimal

or

sudo apt install python2.7

Check version:

python --version


2) If still python 3+ updated list of Python alternatives to perform a switch between any python version is to run:

update-alternatives --config python

Example:

There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.5   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.5   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode

and select an appropriate version using selction integer as shown above.


3) If you see: update-alternatives: error: no alternatives for python. Run:

ls /usr/bin/python*

Example output:

/usr/bin/python  /usr/bin/python2  /usr/bin/python2.7  /usr/bin/python3  /usr/bin/python3.5

Next, update the Python alternatives list for each version you whish to use with priority 1 and 2:

update-alternatives --install /usr/bin/python python /usr/bin/python3.5 1
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2

Then run again update-alternatives --config python and select an appropriate version..

slava
  • 4,085
7

In Ubuntu 18.04 LTS both Python 2.7, and Python 3 are installed by default:

enter image description here
Screenhot from live session

In 18.10. and in 19.04 it will not be installed in a live session:

enter image description here

You will have to intall Python 2.7 manually before you can use it.

sudo apt install python

Applications depending on it will install it as a dependency.

Takkat
  • 144,580
3

You don't need to remove or disable python3 on your Ubuntu.

If you just need python to run small scripts, you can simply install Python 2 as @karel's answer.

If you start learning Python, I suggest you to use virtual environment or conda

thangdc94
  • 950
2

No need to disable python 3. You can execute python 2.7 using command

python2 filename.py.

You can check python 2 versions by

python2 --version