Install Python 3 alongside python 2.7 on Ubuntu 18.04
As Hugo stated in the comment above, python3 should already be installed and you can run it by using python3
in the terminal as he mentioned.
Setting up virtual Environments:
You can also setup virtual python environments using pip. Install pip with the following command.
sudo apt install python-pip
(to install pip3 for python3, use this command sudo apt install python3-pip
)
Now that you have pip installed you can refer to the instructions at this link to learn how to setup these virtual environments for testing various python versions and packages
Switching between python versions using update-alternatives:
If setting up virtual environments is not what you want to do, then you can also use update-alternatives
to switch between python version. Run these two commands first, but make sure the python versions match what you have on your computer
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
You may then run update-alternatives
with the --config
option so that you may select which version you want to choose to be the default
$ sudo update-alternatives --config python
Use the interactive menu to select which version you would like to use as the default. In order to check if your changes have worked you can use python --version