Ubuntu is a great distro for developers. Python programmers will need to install Python on the system before it’s possible to code programs and run Python scripts on Ubuntu.
This tutorial will show you how to install Python 2 or Python 3 on Ubuntu 20.04.
Install Python 3 on Ubuntu
Python 3 is the latest version of the language. It may already be installed on your system by default. You can use the command below to check.
$ python3 --version
If it’s not installed, follow the steps below to install Python 3 on Ubuntu:
Step 1. Python 3 is available for installation from Ubuntu’s package repositories. Execute the following commands to install it, along with its required dependencies. This command will also update Python to the latest version.
$ sudo apt update $ sudo apt install python3
Step 2. You can verify successful installation with this command:
$ python3 --version Python 3.8.10
Step 3. There’s a good chance that you’ll also want to install pip. pip is the package installer for Python. On Ubuntu, pip allows for easy installation of Python programs and dependencies. For Python developers, pip is an essential tool.
$ sudo apt install python3-pip
Step 4. To allow use of the python
command, in place of the python3
command, you can also install the python-is-python3
package.
$ sudo apt install python-is-python3
Install Python 2.7 on Ubuntu
The last release of Python 2 was version 2.7. It’s no longer supported, but you may find a need to install Python 2 on Ubuntu for legacy Python scripts. Follow the steps below to install it.
Note that it’s possible to install Python 2 along with Python 3 on Ubuntu.
Step 1. Python 2 is available for installation from Ubuntu’s package repositories. Execute the following commands to install it, along with its required dependencies.
$ sudo apt update $ sudo apt install python2
Step 2. You can verify successful installation with this command:
$ python2 --version Python 2.7.18
Step 3. To use Python 2 as your default Python version, you can install the python-is-python2
package.
$ sudo apt install python-is-python2
Switch Between Python 2 and Python 3 on Ubuntu
If you have Python 2 and Python 3 installed on Ubuntu simultaneously, you can switch between which default version the system uses. Normally, you would need to change the /usr/bin/python
symbolic link, but Ubuntu has made it even easier for us through the python-is-python2
and python-is-python3
packages. Installing one of these packages will also automatically remove the other package.
Switch to Python 3 as default version
$ sudo apt install python-is-python3
And now the default Python version has changed, but we can still access Python 2 if necessary:
$ python --version Python 3.8.10 $ python2 --version Python 2.7.18
Switch to Python 2 as default version
$ sudo apt install python-is-python2
And now the default Python version has changed, but we can still access Python 3 if necessary:
$ python --version Python 2.7.18 $ python3 --version Python 3.8.10