This document describes how to install Python 3.6 or 3.8 on Ubuntu Linux machines.
To see which version of Python 3 you have installed, open a command prompt and run
If you are using Ubuntu 16.10 or newer, then you can easily install Python 3.6 with the following commands:
$ sudo apt-get update $ sudo apt-get install python3.6
If you’re using another version of Ubuntu [e.g. the latest LTS release] or you want to use a more current Python, we recommend using the deadsnakes PPA to install Python 3.8:
$ sudo apt-get install software-properties-common $ sudo add-apt-repository ppa:deadsnakes/ppa $ sudo apt-get update $ sudo apt-get install python3.8
If you are using other Linux distribution, chances are you already have Python 3 pre-installed as well. If not, use your distribution’s package manager. For example on Fedora, you would use dnf:
$ sudo dnf install python3
Note that if the version of the python3
package is not recent enough for you, there may be ways of installing more recent versions as well, depending on you distribution. For example installing the python3.9
package on Fedora 32 to get Python 3.9. If you are a Fedora user, you might want to read about
multiple Python versions available in Fedora.
Working with Python 3¶
At this point, you may have system Python 2.7 available as well.
This might launch the Python 2 interpreter.
This will always launch the Python 3 interpreter.
Pipenv & Virtual Environments¶
The next step is to install Pipenv, so you can install dependencies and manage virtual environments.
A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.
For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8.
So, onward! To the Pipenv & Virtual Environments docs!
This page is a remixed version of another guide, which is available under the same license.
About the transition
Kali Linux fully switched to Python 3. This means that any tool packaged by Kali that was using Python 2 has been either dropped or converted to use Python 3. Any tool converted to Python 3 contains only scripts with /usr/bin/python3
as their shebang.
Concerning the packages that come straight from Debian, they did
the same on most packages, but there are a few exceptions where packages are allowed to continue to rely on Python 2. However those packages have been updated so that all their scripts use /usr/bin/python2
as their shebang and so that they depend on the python2
binary package [instead of the former python
].
Thanks to those changes, Debian no longer need to provide /usr/bin/python
and recent upgrades will effectively get rid of that symlink.
Unfortunately, when you download a Python script on the
web, it will likely have /usr/bin/python
as its shebang. If you try to execute it without fixing the shebang line, you will end up with an error like this one:
zsh: /home/kali/test.py: bad interpreter: /usr/bin/python: no such file or directory
In Debian, you can reinstate the /usr/bin/python
symlink by installing:
python-is-python2
if you want to have it point topython2
python-is-python3
if you want to have it point topython3
Keeping backwards compatibility in Kali
Given the large number of users that would not know how to avoid the above error, we decided that Kali would continue to ship Python 2 by default [as long as Debian still provides it] and that /usr/bin/python
would point to it. We are also keeping a few common external modules [like requests
] so that random exploit scripts have a reasonable chance to run successfully.
Howewer, pip for Python2 [aka
python-pip] is gone, /usr/bin/pip
is the same as /usr/bin/pip3
and it will install modules for Python 3. See the FAQ below for more information.
This compatibility has been implemented by having kali-linux-headless
recommend python2
, python-is-python2
and offsec-awae-python2
so that they are installed by default while being removable by users that would like to get rid of them.
To make users aware of this situation, we are displaying a promiment message on login:
┏━[Message from Kali developers]
┃
┃ We have kept /usr/bin/python pointing to Python 2 for backwards
┃ compatibility. Learn how to change this and avoid this message:
┃ ⇒ //www.kali.org/docs/general-use/python3-transition/
┃
┗━[Run “touch ~/.hushlogin” to hide this message]
The hope is that they will read this article and know how do deal with the various issues that they will encounter.
Frequently Asked Questions
I have downloaded a Python script, what should I do?
You need to inspect its shebang line. The shebang line is the first line of a script and it starts with #!
followed by the path of the interpreter that will be used to execute the script.
If the interpreter is /usr/bin/python
, you should read the documentation to see whether the script can run with Python 3. If yes, then you should update the shebang line to point to /usr/bin/python3
. Otherwise you should update it to point to /usr/bin/python2
.
Good shebang lines that can be kept:
#!/usr/bin/python3
#!/usr/bin/python2
#!/usr/bin/env python3
#!/usr/bin/env python2
Bad shebang lines that need to be updated:
#!/usr/bin/python
#!/usr/bin/env python
How can I get rid of the login message?
The message will only be displayed as long as /usr/bin/python
points to the deprecated Python 2. Now that you know
about this situation and that you know how to fix the shebang line of old scripts, you can safely get rid of /usr/bin/python
:
[email protected]:~$ sudo apt remove python-is-python2
Or you can decide to make it point to Python 3:
[email protected]:~$ sudo apt install -y python-is-python3
Either of those actions will get rid of the login message.
Alternatively, if you want to keep /usr/bin/python
pointing to python2
and you still want to disable the message, you can do this:
[email protected]:~$ mkdir -p ~/.local/share/kali-motd
[email protected]:~$ touch ~/.local/share/kali-motd/disable-old-python-warning
I have a Python 2 script that doesn’t run, what should I do?
If your Python 2 script uses modules that are not among those that we ship in the offsec-awae-python2
compatibility package [see
list here], then you can try pyenv
to setup a fully isolated Python 2 environment where you can use pip to install supplementary modules. See our Using EoL Python versions on Kali article.
I want pip for Python 2, how can I get it back?
Try pyenv
. See our Using EoL Python versions on Kali article.
I wrote a Python script, what should I do?
Be nice with end users:
- document clearly whether your code runs with Python 3 or Python 2
- use
/usr/bin/python3
or/usr/bin/python2
as the shebang line, it’s more expressive than/usr/bin/python
and is more likely to have the desired result - update it for Python 3 compatibility if that’s not the case yet