replace
[bash:~] $ sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python2.7 2
[bash:~] $ sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python3.5 3
with
[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \
/usr/bin/python2.7 2
[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \
/usr/bin/python3.5 3
e.g. installing into /usr/local/bin instead of /usr/bin.
and ensure the /usr/local/bin is before /usr/bin in PATH.
i.e.
[bash:~] $ echo $PATH
/usr/local/bin:/usr/bin:/bin
Ensure this always is the case by adding
export PATH=/usr/local/bin:$PATH
to the end of your ~/.bashrc file. Prefixing the PATH environment variable with custom bin folder such as /usr/local/bin or /opt/<some install>/bin is generally recommended to ensure that customizations are found before the default system ones.
replace
[bash:~] $ sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python2.7 2
[bash:~] $ sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python3.5 3
with
[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \
/usr/bin/python2.7 2
[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \
/usr/bin/python3.5 3
e.g. installing into /usr/local/bin instead of /usr/bin.
and ensure the /usr/local/bin is before /usr/bin in PATH.
i.e.
[bash:~] $ echo $PATH
/usr/local/bin:/usr/bin:/bin
Ensure this always is the case by adding
export PATH=/usr/local/bin:$PATH
to the end of your ~/.bashrc file. Prefixing the PATH environment variable with custom bin folder such as /usr/local/bin or /opt/<some install>/bin is generally recommended to ensure that customizations are found before the default system ones.
Per Debian policy, python refers to Python 2 and python3 refers to Python 3. Don't try to change this system-wide or you are in for the sort of trouble you already discovered.
Virtual environments allow you to run an isolated Python installation with whatever version of Python and whatever libraries you need without messing with the system Python install.
With recent Python 3, venv is part of the standard library; with older versions, you might need to install python3-venv or a similar package.
$HOME~$ python --version
Python 2.7.11
$HOME~$ python3 -m venv myenv
... stuff happens ...
$HOME~$ . ./myenv/bin/activate
(myenv) $HOME~$ type python # "type" is preferred over which; see POSIX
python is /home/you/myenv/bin/python
(myenv) $HOME~$ python --version
Python 3.5.1
A common practice is to have a separate environment for each project you work on, anyway; but if you want this to look like it's effectively system-wide for your own login, you could add the activation stanza to your .profile or similar.
To solve the error: no alternatives for python3 error message open the terminal and run the following command:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
sudo update-alternatives --config python3
DON'T upgrade the default python version of your system. You will break it. Instead, set up virtual environments to run specific Python versions.
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
You can achieve this by applying below simple steps -
Check python version on terminal:
python --versionExecute this command to switch to python 3.6:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1Check python version:
python --versionDone.
You are definitely able to do an "n to n" link as you state, just add the python3 group symlink you created as a potential target to python:
alternatives --install /usr/bin/python python /usr/bin/python3 300
alternatives --set python /usr/bin/python3
Each --install command will add or update an alternative, which you can see with alternatives --display python
Try update-alternatives --config python and update the current selection using the arrow keys. This gives the option to set the default python version.
The correct way is sudo apt install python-is-python3 - it effectively does a symlink, but it also keeps pace with future updates; so if your ubuntu distribution moves to say Python 3.9, the manual symlink will no longer work, but the package makes sure it is still valid.
Firstly to answer your question, your approach should work, I think the path you've given in your alias needs the / preceding the path so the command should be:
alias python='/usr/bin/python3.8'
This would indeed need to go into your ~/.bashrc file assuming you are using bash.
Secondly, Ubuntu has a really nice method of setting default binaries globally rather than messing with dot config files as depicted here: update-alternatives, so a better solution may be to simply run:
sudo update-alternatives --set python /usr/bin/python3.8
This will ensure you have the version of python in use that you intend, everywhere.
The reason is, python versions aren't fully compatibile. If you set 3.3 version as your default, the applications made for 2.7 could not work.
Or maybe you can.
Important Note: The solution presented bellow (with update-alternatives) may break your system.
If things break, try to fix them and keep python 3, report the fix you made to the correct bug-reports sites and help the community to grow; or move back to python 2. At the end of the answer I'll add a simple command to move back in case you need.
If you just want to change python for your own user, not the system, you can use an alias.
Solution 1: alias
Add this line into your ~/.bashrc or ~/.bash_aliases file:
alias python=python3
And that is it. You don't need to read bellow.
Solution 2: update-alternatives
If alias is not what you are looking for, and you really want to change the default of the whole system, then keep reading.
First check your python version:
# python -V
Python 2.7.13
Use this commands to install alternatives (run as root or use sudo)
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
Then configure the version with (again, run also root or use sudo):
$ sudo update-alternatives --config python
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: 2
Now check the version:
# python -V
Python 3.5.3
When you run the above command, you can choose selection number: 0 or 2 to pick python 3. If things break, run again and pick number 1 to go back to python 2.
Good readings:
python3 statement
Hack-a-Day
The Register News