372

I am using Ubuntu 16.04 LTS . I have python3 installed. There are two versions installed, python 3.4.3 and python 3.6 . Whenever I use python3 command, it takes python 3.4.3 by default. I want to use python 3.6 with python3.

python3 --version shows version 3.4.3

I am installing ansible which supports version > 3.5 . So, whenever, I type ansible in the terminal, it throws error because of python 3.4

sudo update-alternatives --config python3
update-alternatives: error: no alternatives for python3
8
  • 8
    What does your sudo update-alternatives --config python3 say? Commented Dec 13, 2017 at 9:35
  • 3
    update-alternatives: error: no alternatives for python3
    – codeclue
    Commented Dec 13, 2017 at 9:38
  • 2
    @Mikael Different Python versions are not alternatives on Ubuntu. The OS relies on a specific version (or versions) being installed.
    – wjandrea
    Commented Jan 1, 2020 at 19:22
  • 6
    Just run: sudo apt install python-is-python3
    – Slion
    Commented Aug 31, 2021 at 18:18
  • 1
    Best / safest to add alias python3="python3.6" to ~/.bash_profile
    – goonerify
    Commented Oct 8, 2021 at 11:19

10 Answers 10

433

From the comment:

sudo update-alternatives --config python

Will show you an error:

update-alternatives: error: no alternatives for python3 

You need to update your update-alternatives , then you will be able to set your default python version.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2

Then run :

sudo update-alternatives --config python

Set python3.6 as default.

Or use the following command to set python3.6 as default:

sudo update-alternatives  --set python /usr/bin/python3.6
14
  • 39
    This answer will probably break apt. After selecting a different version of python try to run sudo apt update. If it gives you an error like ModuleNotFoundError: No module named 'apt_pkg' you want to go to /usr/lib/python3/dist-packages and create the symlink: ln -s apt_pkg.cpython-{34m,36m}-x86_64-linux-gnu.so. Commented May 30, 2019 at 8:22
  • 16
    Making python3 point to python3.8 with this method broke gnome-terminal for me.
    – user114651
    Commented Oct 17, 2019 at 23:49
  • 14
    @Johny I just added alias python="python3.8" to ~/.bash_profile
    – user114651
    Commented Nov 25, 2019 at 21:02
  • 12
    Don't do this. It will break apt like @Giacomo mentioned along with other OS components like do-release-upgrade. Simply use an alias instead.
    – wjandrea
    Commented Jan 1, 2020 at 19:15
  • 2
    Works fine in Ubuntu 18.04 LTS without breaking APT. Note to those confused: if you are getting the error: no alternatives for python message, then you need to run the --install commands following first (replacing the python versions you want to use as appropriate, and the numbers at the end are just for order) then you run the --config command again and you will be asked which version you want to use by default.
    – tpartee
    Commented Feb 25, 2020 at 6:50
74

You can achieve this by applying below simple steps -

  1. Check python version on terminal: python --version

  2. Execute this command to switch to python 3.6:

    sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
    
  3. Check python version: python --version

  4. Done.

3
  • Works fine on Ubuntu 18.04. so simple than other solutions. thanks a lot Commented Aug 13, 2019 at 23:29
  • @Sam-T your comment didn't work for me. I am on Debian 10 & this worked for me sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1; python --version
    – Kumar
    Commented Apr 14, 2020 at 3:47
  • This command doesn't change the behavior for dependencies such as pip. After executing this, pip was still using the older version of python.
    – Amir Zare
    Commented Oct 5, 2023 at 10:46
46

if you have multiple version of python in your system. You just need to update the symbolic link of python inside /usr/bin/

root@irshad:/usr/bin# ls -lrth python*
lrwxrwxrwx 1 root root    9 Apr 16  2018 python -> python2.7
-rwxr-xr-x 1 root root 3.6M Nov 12  2018 python2.7
-rwxr-xr-x 2 root root 4.4M May  7 14:58 python3.6

In above example if you see the output of python --version you will get python2.7

Now update the python symlink using below command-

root@irshad:/usr/bin# unlink python
root@irshad:/usr/bin# ln -s /usr/bin/python3.6 python
root@irshad:/usr/bin# python --version
Python 3.6.8
4
  • 1
    I agree this answer is the best. However, I would add that some people's newly installed python won't be located in /usr/bin/pythonX.X. Instead figure out the location with which pythonX.X and substitute in. Commented Jan 19, 2022 at 1:07
  • I couldn't find the newly installed python3.9 in /user/bin/, any suggestion please? I am on Ubuntu 18.04
    – Adamy
    Commented Jun 17, 2022 at 0:37
  • Note that if you don't have root permission, then u may need execute the above commands with sudo.
    – Bowen Xu
    Commented Jul 22, 2022 at 4:41
  • I needed this variation: sudo unlink /usr/bin/python3, sudo ln -s /usr/bin/python3.8 /usr/bin/python3, python3 --version Commented Jun 25, 2023 at 23:26
27

Using these commands can help you:

  1. check the version of python: ls /usr/bin/python*
  2. alias: alias python='/usr/bin/pythonxx' (add this to . ~/.bashrc)
  3. re-login or source . ~/.bashrc
  4. check the python version again: python --version
3
  • 1
    thanks but after restarting the computer the main python would be still the ex python 2.7 Commented Aug 13, 2019 at 23:25
  • 3
    You should add the script to modify the default in ~/.bashrc. Then you will make it work forever.
    – Newt
    Commented Nov 4, 2019 at 13:32
  • like this: export python=/home/xxx (your py3 directory)
    – Newt
    Commented Nov 4, 2019 at 13:40
14

First check that you have a python3.6 folder?

ls /usr/bin/python3.6

If you have "python3.6" folder, you are good to go. Now update-alternatives

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1

then update new config for python3

sudo update-alternatives --config python3

Finally, check default python3 version:

python3 --version
3
  • 1
    nit: you don't have folders under bin, you have executables.
    – mayid
    Commented Oct 1, 2020 at 22:52
  • Didn't work for me.
    – Evan Zamir
    Commented Sep 6, 2021 at 0:15
  • 1
    This broke sudo apt-get update for me ModuleNotFoundError: No module named 'apt_pkg'
    – Mark
    Commented Dec 7, 2021 at 22:51
6

Create symlink for /usr/bin/python3. In my LinuxMint:

# ls -lh /usr/bin/python3 /usr/bin/python
lrwxrwxrwx 1 root root 9 ноя 24  2017 /usr/bin/python -> python2.7
lrwxrwxrwx 1 root root 9 сен  6  2017 /usr/bin/python3 -> python3.5

# mv /usr/bin/python /usr/bin/python.bak
# cp /usr/bin/python3 /usr/bin/python
# python --version
Python 3.5.2
1
  • 4
    This broke apt installer.
    – jws
    Commented May 5, 2020 at 21:30
5

An easy answer would be to add an alias for python3.6.

Just add this line in the file ~/.bashrc : alias python3="python3.6", then close your terminal and open a new one. Now when you type python3 xxx it gets translated to python3.6 xxx.

This solution fixes your problem without needing to tweak your system too heavily.

EDIT :

As Mikael Kjær pointed out, this is a misconfiguration of ansible with your system.

As seen here :

Set the ansible_python_interpreter configuration option to /usr/bin/python3. The ansible_python_interpreter configuration option is usually set per-host as an inventory variable associated with a host or group of hosts:

  # Example inventory that makes an alias for localhost that uses python3
  [py3-hosts]
  localhost-py3 ansible_host=localhost ansible_connection=local

  [py3-hosts:vars]
  ansible_python_interpreter=/usr/bin/python3

As seen here about the config file :

Changes can be made and used in a configuration file which will be processed in the following order:

* ANSIBLE_CONFIG (an environment variable)
* ansible.cfg (in the current directory)
* .ansible.cfg (in the home directory)
* /etc/ansible/ansible.cfg
3
  • Actually, I am installing ansible , which supports only > python 3.5
    – codeclue
    Commented Dec 13, 2017 at 9:35
  • 2
    You can set the ansible_python_interpreter variable to use your python3.6. docs.ansible.com/ansible/latest/python_3_support.html Commented Dec 13, 2017 at 9:37
  • @MikaelKjær I just added your comment to my answer
    – user264413
    Commented Dec 13, 2017 at 9:46
4

update-alternatives is to change system symlinks to user-defined/admin-defined symlinks. If you have multiple versions of python3 installed in your system and want to control which python3 version to invoke when python3 is called. Do the following

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.4 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 2

Run below command if you want to change priority in the future.

update-alternatives --config python3

Explanation:-

sudo update-alternatives --install <symlink_origin> <name_of_config> <symlink_destination> <priority>

You can go on change name_of_config to python4, but then you have to invoke update-alternatives --config with python4 to reconfigure.

Using this approach you are able to control system python version and python3 version separately.

1
  • This is one of only 2 answers that updates python3 without updating the python version and this one seems to have been first. Thanks!!
    – sage
    Commented Aug 12, 2021 at 18:39
1

As mentioned in this comment by Slion,sudo apt install python-is-python3 will do this for you in a reasonable way, currently IIUC by added an appropriate symlink.

0

You can change the simbolic link by ln -sf python3.6 python3 inside /usr/bin. With this when you call python3 it will execute python3.6

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .