285

I was trying to set default python version to python3 in Ubuntu 16.04. By default it is python2 (2.7). I followed below steps :

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3

but I'm getting the following error for the second statement,

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs <link> <name> <path> <priority>

Use 'update-alternatives --help' for program usage information.   
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
RejeeshChandran
  • 4,168
  • 3
  • 21
  • 32
  • 3
    As stated in the warning, you are missing priority. – greedy52 Feb 01 '17 at 17:59
  • 14
    Take care not to remove Python 2.7 as it will cripple many facilities of you OS (from experience :( ) – Jacques de Hooge Feb 01 '17 at 18:36
  • I made an edit to my answer in relation to your priority error. – Steampunkery Feb 02 '17 at 04:59
  • 1
    A word of warning: It sounds like a bad idea to me to change `python` to Python 3. The default way to invoke scripts written in Python 2 is `python my-script-p2.py`, while it's `python3 my-script-p3.py`. I would expect many system scripts to rely on this. – Jan Groth Sep 26 '19 at 02:20
  • 1
    For those who're interested in the topic I'd recommend to pay attention to the virtual environment: https://docs.python.org/3/tutorial/venv.html My Ubuntu 18 LTS still uses Python 2.7 and, for example, I use the virtual environment for using Python 3.X and be up-to-date in my Django projects. – Viktor Born Jan 20 '20 at 10:41
  • 1
    This link might have the answer ;) https://unix.stackexchange.com/questions/410579/change-the-python3-default-version-in-ubuntu – raz Jun 01 '20 at 22:27
  • See this: https://stackoverflow.com/a/63207387/10625611 – Qumber Dec 16 '20 at 06:40
  • Thansk @mr_azad comment, this is most likely the simplest way to make the change (on Ubuntu 18.04): https://unix.stackexchange.com/a/498264/482233 – Json Jul 19 '21 at 07:13

24 Answers24

570

The second line mentioned can be changed to

[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10

This gives a priority of 10 for the path of python3.

The disadvantage of alternatively editing .bashrc is that using the commands with sudo will not work.

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
Pardhu
  • 5,935
  • 2
  • 11
  • 23
  • 6
    Good and easy way out. – PrakashG Jan 11 '19 at 09:05
  • 10
    Good and right to the point. " " in the error message already suggested it. BTW, "sudo" is typically needed to run this install command. – ywu May 07 '19 at 20:20
  • 4
    Like ywu said, I had to run "sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10" – Royal Sep 04 '19 at 11:51
  • 3
    This is the right way to do it for sure, but it's worth noting that changing the systemwide default is likely to break some things. For instance, I had to go and [apply a fix to terminator](https://askubuntu.com/a/869732/119639), which only works with python2. – Dale C. Anderson Sep 13 '19 at 18:09
  • 2
    Doesn't work completely -- after this command `python` runs `python3`, but `python-config` still runs `python2-config` and general breakage ensues – Chris Dodd Dec 01 '19 at 17:28
  • You can add the `alias sudo='sudo '` if you want to be able to run aliases as sudo. – Erasmus Cedernaes Jun 10 '20 at 07:13
  • what is python3 here? – Suresh Jun 22 '20 at 11:56
  • whats that 10 doing there at the last of the command? – Vicrobot Nov 05 '20 at 12:34
  • Type `which python3` to locate the path of python3 first. Then execute the `sudo update-alternatives --install python 10` – pandas007 Jul 06 '21 at 21:46
  • `sudo update-alternatives --install /usr/bin/python3 python /usr/bin/python3.8 8` this worked for me – Philippe Remy Jan 12 '22 at 07:31
  • You can use aliases with sudo if you add the alias `alias sudo='sudo '` that is, an alias for sudo that is sudo with one blank space. The blank space after sudo allows you to use aliases from your `.bashrc` file with `sudo`. – mchid Apr 20 '23 at 18:54
190

EDIT:

I wrote this when I was young and naive, update-alternatives is the better way to do this. See @Pardhu's answer.


Outdated answer:

Open your .bashrc file nano ~/.bashrc. Type alias python=python3 on to a new line at the top of the file then save the file with ctrl+o and close the file with ctrl+x. Then, back at your command line type source ~/.bashrc. Now your alias should be permanent.

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
Steampunkery
  • 3,839
  • 2
  • 19
  • 28
  • 91
    This is the wrong answer. Editing your bashrc does not do the same thing as update-alternatives. For example, scripts that begin with `#!/usr/bin/env python` will not use the version in bashrc. Please use @Pardhu's answer. – stonewareslord Mar 28 '19 at 19:45
  • I wrote this answer a long time ago, and I am aware that update alternatives is not the same as changing bashrc. I can edit the answer if you'd like. – Steampunkery Mar 29 '19 at 20:17
  • 1
    It is more of a warning to users with this question that changing the alias does not do the same thing. Up to you if you want to edit. – stonewareslord Apr 01 '19 at 18:35
  • This is the only answer that helped me. I tried doing `sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5.2`. `But I got: update-alternatives: --install needs Use 'update-alternatives --help' for program usage information.` – alexchenco Jan 10 '20 at 10:14
  • 1
    @alexchenco you did not add priority at the end. right after python3.5.2 you should put something like 10 – Ajeetkumar Jul 04 '20 at 09:10
  • 4
    I did this and it broke things, like virtualenvs. This answer is creating more problems – Kuzeko Jul 19 '20 at 14:29
  • 1
    Maybe it's better to use @Pardhu 's answer – Milad Yarmohammadi Jul 20 '20 at 13:19
  • We all were young. We needed the money. – Mikko Ohtamaa Jul 13 '22 at 09:46
  • It's good to do it for pip too. `alias pip=pip3` – Qinjie Nov 03 '22 at 01:22
93

To change Python 3.6.8 as the default in Ubuntu 18.04 to Python 3.7.

Install Python 3.7

Steps to install Python3.7 and configure it as the default interpreter.

  1. Install the python3.7 package using apt-get

    sudo apt-get install python3.7

  2. Add Python3.6 & Python 3.7 to update-alternatives

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
  1. Update Python 3 to point to Python 3.7

    sudo update-alternatives --config python3 Enter 2 for Python 3.7

  2. Test the version of python

python3 --version
Python 3.7.1 
Alvin Sartor
  • 2,249
  • 4
  • 20
  • 36
  • 7
    You might want to include a warning that this can break packaged software. Python 3.6 is the distributed default and any bundled software packages will also assume this version. – Tim Aug 25 '19 at 00:27
  • Can I replace the python to version 3.7 instead of python3? – Wee Hong Mar 09 '20 at 17:10
  • @Tim, what's the best way to take care of that? – magikarp May 27 '20 at 13:22
  • @Wee Hong, yes you can. Instead of $ sudo update-alternative --install /usr/bin/python3 python3 .... you just replace to $ sudo update-alterative --install /usr/bin/python python ..... and after: sudo update-alternatives --config python. – Arthur Zennig Jul 01 '20 at 18:05
  • Worked for me, but I realized that I had some old packages in the system that had python2 pre-remove scripts. So I couldn't deinstall them - had to switch back to python2 to be able to remove them... – Roman Mar 04 '21 at 14:17
71

If you have Ubuntu 20.04 LTS (Focal Fossa) you can install python-is-python3:

sudo apt install python-is-python3

which replaces the symlink in /usr/bin/python to point to /usr/bin/python3.

silviot
  • 4,615
  • 5
  • 38
  • 51
32

To change to python3, you can use the following command in terminal alias python=python3.

DanteVoronoi
  • 1,133
  • 1
  • 13
  • 20
26

Update:
Since Ubuntu 20.04, the python3 is the default version, but still, python is not registered as python3 by default. In order to make that happen, you can simply do :

sudo apt install python-is-python3

For more information you can check this out.
Old way:

Do

cd ~
gedit .bash_aliases

then write either

alias python=python3

or

alias python='/usr/bin/python3'

Save the file, close the terminal and open it again.
You should be fine now! Link

Hossein
  • 24,202
  • 35
  • 119
  • 224
20

A simple safe way would be to use an alias. Place this into ~/.bashrc file: if you have gedit editor use

gedit ~/.bashrc

to go into the bashrc file and then at the top of the bashrc file make the following change.

alias python=python3

After adding the above in the file. run the below command

source ~/.bash_aliases or source ~/.bashrc

example:

$ python --version

Python 2.7.6

$ python3 --version

Python 3.4.3

$ alias python=python3

$ python --version

Python 3.4.3

Community
  • 1
  • 1
Khan
  • 1,288
  • 12
  • 11
19

Just follow these steps to help change the default python to the newly upgrade python version. Worked well for me.

  • sudo apt-install python3.7 Install the latest version of python you want
  • cd /usr/bin Enter the root directory where python is installed
  • sudo unlink python or sudo unlink python3 . Unlink the current default python
  • sudo ln -sv /usr/bin/python3.7 python Link the new downloaded python version
  • python --version Check the new python version and you're good to go
Shorya Sharma
  • 457
  • 3
  • 10
13

At First Install python3 and pip3

sudo apt-get install python3 python3-pip

then in your terminal run

alias python=python3

Check the version of python in your machine.

python --version
10

As an added extra, you can add an alias for pip as well (in .bashrc or bash_aliases):

alias pip='pip3'

You many find that a clean install of python3 actually points to python3.x so you may need:

alias pip='pip3.6'
alias python='python3.6'

Paraic
  • 137
  • 1
  • 6
9

This is a simple way that works for me.

sudo ln -s /usr/bin/python3 /usr/bin/python

You could change /usr/bin/python3 for your path to python3 (or the version you want).

But keep in mind that update-alternatives is probably the best choice.

cbcram
  • 165
  • 2
  • 6
6

As it says, update-alternatives --install needs <link> <name> <path> and <priority> arguments.

You have link (/usr/bin/python), name (python), and path (/usr/bin/python3), you're missing priority.

update-alternatives --help says:

<priority> is an integer; options with higher numbers have higher priority in automatic mode.

So just put a 100 or something at the end

6

get python path from

ls /usr/bin/python*

then set your python version

alias python="/usr/bin/python3"
4

To change Python 3.6.8 as the default in Ubuntu 18.04 from Python 2.7 you can try the command line tool update-alternatives.

sudo update-alternatives --config python

If you get the error "no alternatives for python" then set up an alternative yourself with the following command:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

Change the path /usr/bin/python3 to your desired python version accordingly.

The last argument specified it priority means, if no manual alternative selection is made the alternative with the highest priority number will be set. In our case we have set a priority 2 for /usr/bin/python3.6.8 and as a result the /usr/bin/python3.6.8 was set as default python version automatically by update-alternatives command.

we can anytime switch between the above listed python alternative versions using below command and entering a selection number:

update-alternatives --config python
Ranjeet SIngh
  • 381
  • 5
  • 15
3

For another non-invasive, current-user only approach:

# First, make $HOME/bin, which will be automatically added to user's PATH
mkdir -p ~/bin
# make link actual python binaries
ln -s $(which python3) python
ln -s $(which pip3) pip

python pip will be ready in a new shell.

tdihp
  • 2,329
  • 2
  • 23
  • 40
3

Simply remove python-is-python2:

sudo apt purge python-is-python2

And install python-is-python3:

sudo apt install python-is-python3

It will automate the process of transition to new python3. Optionally you can get rid of remaining packages later:

sudo apt autoremove && sudo apt autoclean
Farab Alipanah
  • 351
  • 4
  • 2
3

Set priority for default python in Linux terminal by adding this:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1

Here, we set python3 to have priority 10 and python2 to priority 1. This will make python3 the default python. If you want Python2 as default then make a priority of python2 higher then python3

3
       ~$ sudo apt-get install python3.9
/usr/bin$ cd /usr/bin
/usr/bin$ sudo unlink python3
/usr/bin$ sudo ln -sv /usr/bin/python3.9 python3
/usr/bin$ python3 --version
          Python 3.9.5
/usr/bin$ pip3 --version
          pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9)
Udesh
  • 2,415
  • 2
  • 22
  • 32
  • 1
    Finally the right and most effecient answer. In my case I wanted to change the base "python" and not "python3". So I did this sudo ln -sv /usr/bin/python3.7 python – chainstair Jan 09 '23 at 11:32
  • @chainstair You should never manually modify anything in `/usr/bin`. The OS owns these files, and will get confused if somebody else messes with its little mind. In theory, an OS upgrade could lose any of your changes there. – tripleee Feb 08 '23 at 20:17
2
sudo rm /usr/bin/python3 #remove existing link
sudo ln /usr/bin/python3.8 /usr/bin/python3 # create a new link to the version of your choice
Sajibe Kanti
  • 181
  • 1
  • 9
  • Seems like a bit of a sledgehammer solution, but I guess it'll work. Is is stable when updates are later installed or is this something you might have to redo after major updates? Mind you, this is probably exactly what I would do... – joanis Oct 01 '21 at 12:58
1

The best way in ubuntu 18.04 which will work for all users is

sudo vim /etc/bash.bashrc
add lines
alias python=python3
alias pip=pip3

Save the changes and restart .

After restart what ever version of python 3 you have in the system along with python 2.7 will be taken as default. You could be more specific by saying the following in alias if you have multiple version of python 3.

sudo vim /etc/bash.bashrc
add lines
alias python=python3.6
alias pip=pip3.6
Dr. Mian
  • 3,334
  • 10
  • 45
  • 69
0

You didn't include the priority argument

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 5

You can replace 5 with any priority you want. A higher priority alternative takes precedence over lower priority.

0

If there is a possibility to use particular python version directly, I would go for it compared to update-alternatives and alias solution.

Ex.

python3.6 -m pip install pytest
ptyhon3.6 -m pytest test_sample.py

-m executes particular module for that particular python version. The first line will install pytest for for that particular version and user in possible location /home/user/.local/lib/python3.5/site-packages

JanPo
  • 345
  • 2
  • 8
0

To change the default python version in linux use following command-

sudo ln -sf /usr/bin/python3 /usr/bin/python2

This will change default version to python3

To verify use command-

python --version
-2

At first, Make sure Python3 is installed on your computer

Go to your terminal and type:

cd ~/ to go to your home directory

If you didn't set up your .bash_profile yet, type touch .bash_profile to create your .bash_profile.

Or, type open -e .bash_profile to edit the file.

Copy and save alias python=python3 in the .bash_profile.

Close and reopen your Terminal. Then type the following command to check if Python3 is your default version now:

python --version

You should see python 3.x.y is your default version.

Cheers!

nurealam siddiq
  • 1,567
  • 10
  • 9
  • this method already described in preferred answer for this question. and your answer doesn't contributes anything. – tripulse Oct 06 '19 at 08:19