How to install Python on Kali Linux
Installing Python on Kali Linux is very easy as you have to follow a few steps.
You install Python on Kali Linux by opening a Terminal window and running the following commands:
sudo apt update && sudo apt upgrade -y
sudo apt install python3 python3-pip
# confirm you have installed Python
python3 --version
Most programmers would prefer to develop their programs on a Windows machine and later deploy the applications on a Linux instance.
I ask myself, why would they do that?
Why should I do that too? Can’t I just use a Linux operating system for local and deployment purposes?
Well, I prefer Linux. Linux offers too much when it comes to programming.
On top of that, I like being different and taking on challenges others prefer to avoid, like using Linux.
Most people seem to be intimidated by a Linux Terminal. I know it seems tedious and mind-intensive to remember all the commands. Well, that is where the fun is.
Being creative like me should mean that you would never settle for less. You learn and experience new things every day.
That’s why we have come to this tutorial, where you will learn how to install Python on a Kali Linux machine or any other Debian-based operating system such as Ubuntu.
Kali Linux is easy to learn, depending on how you take it. I take it lightly, and with every new problem comes a new experience of having to research and troubleshoot the problem.
I am talking too much. Let’s get right into the purpose of this tutorial.
Right, installing Python on Kali Linux.
How to install Python on Kali Linux using the Terminal
To get started, ensure
- You have installed Kali Linux on your machine. If you haven’t done that, check this article on How to install Kali Linux alongside Windows.
- Access to the internet.
- Probably, you need to be sober. Do not drink and drive a Kali machine. For me, I prefer my brain cells to be working right. So, should you … right?
Hoping you have all the things ready, open a new Terminal window and start typing the following:
sudo apt update && sudo apt upgrade -y
Wait for the system to complete upgrading.
Do not judge me, but I sometimes place my laptop where everybody can see the text printed on the Terminal and wait for the moment they say …
“Oooh mighty Steven. Hack my girlfriend’s phone, please!”
Little do they know … I know nothing.
Well, after a successful upgrade, it is time to install the latest Python version on your system.
To do that, type the following:
sudo apt install python3
The command will install the latest Python version on the Debian distribution release.
Remember, we executed the command, sudo apt update
.
The command served to update the sources list to the latest distribution release with all the operating system’s packages, libraries, utilities, and drivers. Python is one of them.
If you miss running the command, your package sources list will have outdated package versions.
After the command executes successfully, run the following commands to confirm that the latest Python version has been installed on your system.
python –version
You should see a textual output indicating the Python version installed on your system.
On some occasions, you may find the latest version advertised on the python.org website is not the same as the one installed on your system.
Well, this comes back to the sudo apt update
command.
Maintainers test each python version for compatibility with the OS and applications that depend on it. If the OS used the latest Python version immediately after its release, it would lead to compatibility and dependency issues.
Thus, testing the version before adding it to the distribution release is necessary.
Don’t worry, though. That version will work just fine as the latest version.
What if you want to install a specific version of Python on Kali Linux?
How to install specific Python version in Kali Linux
Well, you may install a specific Python version by downloading it from the sources, compiling it, and then installing it. To do that, you have to follow these steps.
Step 1: Install the required packages
You have to install additional packages before installing Python.
To install the required packages, run the following commands:
sudo apt-get install build-essential checkinstall libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
To learn more on how Linux commands such as apt
and sudo
, you should check out this ridiculously simple course:
Linux for Beginners: Linux Command Line Basics
Looking to master the Linux command line interface?
The “Linux Command Line Basics” course is the ultimate solution to all your Linux woes. Designed for beginners, this course covers the essentials of Linux, from understanding the directory structure to managing files and directories and performing user management tasks.
With interactive video tutorials, hands-on exercises, and a supportive community of learners, our experts will guide you through this course.
By the end of it, you’ll be a pro at navigating the Linux command line interface and gain a deeper understanding of this powerful operating system.
Step 2: Download and extract Python tarball
Head over to Python.org and note the Python filename of the Python version you intend to install.
For example, if you want to install Python version 3.8, the filename will be Python-3.8.0.tgz.
Navigate into the /usr/src
folder on your Linux machine and download the Python archive.
The usr/src
folder holds files used by programs to compile or build packages.
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
After downloading, extract the Python archive by running the following command:
sudo tar xzf Python-3.8.0.tgz
Step 3: Compile the downloaded Python source code and install it on your Linux system
Run the following commands to install Python 3.8 on your Linux:
cd Python-3.8.0
sudo ./configure
sudo make altinstall
If you were to replace the original Python binary file installed on /usr/bin/python
, you would likely mess up with the compatibility and dependencies of already installed OS packages.
The safest way is to use altinstall
to prevent replacing the default Python file.
Step 4: Confirm that you have installed Python 3.8 on your Linux system
Type the following to check the Python version installed on your Kali Linux machine:
python3.8 --version
Press Enter
The command should display the Python version that you previously downloaded, compiled, and installed.
However, you should specify it on the terminal whenever you want to use that particular version.
For example, typing python3.8
opens the version specified after the word ‘python’ – the Python version that was compiled and installed.
If you want to use the original Python binary file, use the keyword ‘python’ without specifying the version.
Where is Python installed on Linux?
Well, to determine the installation location for your Python version, use the command ‘whereis’.
For example, to know where the original Python binary file is installed, type:
whereis python
On the other hand, to know where a specific Python version is installed, add the Python binary version at the end of the ‘python’ keyword. For example, to know the installation directory for Python binary version 3.8, type:
whereis python3.8
You will notice that the original Python binary file resides inside the /usr/bin folder. Python is installed on Linux inside the /usr/bin folder, where most distribution-managed user programs live.
The binary file is stored inside the /usr/local/bin folder for a specific Python version. The /usr/local/bin folder stores binary files for locally compiled packages – not distributed by the package manager such as apt.
Well, you have installed Python on your system.
How do I open and use it to run my Python scripts?
How to run Python programs on Linux
Well, there are two ways to run python statements on Linux
- By using the Linux Terminal
- By using an integrated development environment (IDE)
How to run Python on a Linux Terminal.
Using the Terminal to run Python scripts is the easiest. However, that depends. You should be familiar with navigating the Linux filesystem using the Terminal.
Check my article outlining the most basic commands you need to start using Linux.
Open the Terminal and type the following
python
Press enter
The Terminal window should display the Python version installed on your system and some other information.
Don’t worry about that, though. Focus on the three greater than (>>>) symbols.
The symbols indicate a Python prompt. A prompt waits for the user to enter something and then executes provided it follows Python syntax.
If you see that, you are ready to use Python on your system. Print a little message, like
print(“Hello earthlings”)
OR
print(
"""
Steve, you should leave your girlfriend if she is cheating.
Do not hack her phone.
It is a
violation of private property oh - privacy private privacy,
daang! whatever
"""
)
Wait, it wasn’t my girlfriend.
Well, that is one way you can use the Terminal to interact with Python.
However, using the Python prompt can be cumbersome, especially when writing large programs. Besides, what if you were to use a Python script?
Running Python scripts using the Terminal on Linux
Well, if you have a Python script, you can execute it by opening a new Terminal window, navigating to the script’s location, and executing it.
For example, your script, named script.py, is stored in the Desktop folder.
To navigate, you would type the following in the Terminal:
cd ~/Desktop
Then type the following to execute the script:
python script.py
Replace script.py with the name of your script.
Using python filename.py
executes the valid Python statements in that file.
Mastery in Python – For Beginners from Zero to Hero!
Python is a beginner-friendly Python programming language. However, it can take you a very long time to master the language if you do not have the right learning approaches.
Because you are smart- you should always take the shortest route; like I did with learning through the right course.
With this course, I guarantee you will be a master Python programmer who can take any coding/job challenge like me.
The only thing that it takes is to click on the link (Affiliate link) and make the right decision to invest in a top-quality Python course today!
Executing Python scripts using IDE on Linux
Another way to execute a Python script is to use an IDE.
Why you should use an IDE when developing your Python apps
- IDEs increase productivity by reducing the setup time. Besides, you do not need to spend too much on the syntax of a programming language.
- IDEs are easy to use – you don’t have to open a new Terminal whenever you want to execute your script.
- IDEs provide formatting and check errors as you write the code. IDEs assist in writing quality code.
- You are transitioning from a Graphical rich OS such as Windows and haven’t familiarized yourself with the Terminal.
What is the best IDE I could use when programming with Python on a Linux machine?
Well, here’s my article, which has three common IDE I use.
Generally, these IDEs are Visual Studio Code, Pycharm, and Spyder.
Any of the three should get you started.
However, let me show you how to install and use Spyder to execute your Python scripts.
Open the Terminal and type the following:
sudo apt update && sudo apt install spyder
The command installs the latest Spyder version on your Linux machine.
To start using Spyder, press the super key (the key with the Windows logo on your machine) and start typing spyder.
You should see a window like the one below with the Spyder app icon appearing after typing the spyder.
Click on the icon to open it.
If you cannot see the icon, open the Terminal and type the following
sypder
Press Enter
The command should open Spyder app.
Once the Spyder app opens, you use the left pane to write your script.
To execute the script, use the little play button at the top toolbar.
Alternatively, press the F5 function key to execute your Python script.
The script output should be displayed inside the right window pane.
How to install pip in Kali Linux
If you have installed Python on your Linux machine, mostly likely pip will be installed.
However, you can always install pip on Linux using the following command:
sudo apt install python3-pip
Check the pip version installed on your system by typing:
pip --version
The command should display the pip version installed on your system.
Why install pip on Linux
Well, you may ask: Why should I need pip installed on my Linux system?
Well, you will need pip to install and manage packages not officially distributed in the Python standard library.
Just like the Debian distribution that may not supply the latest Python Version immediately, the Python standard library may lack some packages you may need for your projects. For example, NumPy.
The packages not in the Python standard library are not installed when installing Python on your Linux machine.
You will have to install them manually using the pip package manager.
PIP:
- Pip makes the installation of extra packages that aren’t part of the Python standard library easier
- Pip allows easy uninstalling of packages
- Pip resolves dependencies automatically. Pip checks if all the dependencies are installed before installing a package. If some are missing, pip installs the first.
- Pip allows one to easily create a requirements.txt file that contains a list of all the packages and dependencies required in a project.
From this point onward, familiarise yourself with the Linux system. Besides, learn a Python web framework if you want to be a web developer.
Whenever you may want to deploy your application, you will not have to learn the Linux filesystem and navigation afresh.
You will have familiarized yourself with using it, and deploying your application will be easy.
If you create a web application using Django, check out this article. The article has all the information you need to deploy Django on a Linux server.
That’s it for this tutorial.
You are now ready to create a bad [peep] Python application.