If you’re on a newer version of Debian or pretty much any other Linux distribution, you will already have Python installed in your system. But maybe you don’t have the latest one. Let’s start by checking which version is installed, and how to get the latest one if you don’t already have it installed in the system.
Installing Python on Debian/Ubuntu
We’ll begin by verifying if we already have Python and the version. Since Python 3 isn’t backward compatible, Linux systems named both the Python packages differently.
The package for Python 2 is named python and the package for Python 3 is named python3.
Let’s check if we have python3 in our systems. Just enter the python3 command to see what output you get. If you get the python interpreter, you are all set up! If you get an error like the one below, read on.
Downloading and Installing Python 3 using Apt command
You would not need to download and build the Python package from the source because Debian and other Debian based Linux distributions already have a precompiled package of the latest Python release.
root@HowLinux:~# apt update
root@HowLinux:~# apt install python3 python3-pip
Verifying Our Installation
Let’s run the python3 command again and check if we have the latest version in.
Great, you now have Python 3.7 installed on your system! You can now run your Python scripts. But wait, right now the latest version is 3.8.1.
How to install Python Latest Version on Debian/Ubuntu?
Currently, the latest source is not available in the stable repository of Debian. But if you need to have the latest version of Python in your system, let’s get our hands dirty and build the code from source.
1. Getting Tools To Compile Source
First, we’ll need some tools. Let’s install the packages using the apt command below.
root@HowLinux:~# apt install wget build-essential libgdbm-dev libncurses5-dev libnss3-dev zlib1g-dev libssl-dev libreadline-dev libffi-dev
Once done, you can move to the next step.
2. Downloading Python 3.8.1 Source Code
We downloaded the wget command in the previous step. Now let’s use the command to download the Python 3.8.1 source code.
root@HowLinux:~# wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
This will download the package in your current folder.
3. Extracting the Source File
The downloaded file Python-3.8.1.tgz is a GZipped file. Debian, Ubuntu, and most other versions of Linux come with the tar command preinstalled. The tar command will allow us to extract this package.
root@HowLinux:~# tar -xfv Python-3.8.1.tgz
The v in the above command options stands for verbose and will print out every single file that is being extracted. If you do not need to see the files, you can remove the v and only keep -xf in the options.
4. Configuring the Source Files
Extracting the package will create a new folder named Python-3.8.1. Let’s cd into the folder and start working on the next step. Once you’re in the directory we’ll run the configure file that is present in the folder.
This is an executable file. We’ll run it by typing ./configure –enable-optimizations.
If you see “Creating Makefile” in the end, you can move to the next step. If there is an error, see what the error says. It will mostly be due to a missing package and the error will mention any missing packages. All you’d need to do is install the package using apt install.
5. Installing the Latest Python Package
Alright, now that you’re done with configuring the package, it’s time to complete the install. You can use make install command but we’d recommend using make altinstall instead.
The make install command replaces the Python3 package whereas the make altinstall will create another package named python3.8.
root@HowLinux:~/Python3.8/Python-3.8.1# make altinstall
This command will take time so you can have a cup of coffee in the meanwhile. Once successful, you should see this at the end of the output.
6. Verifying Python 3.8.1 Install
Let’s check if the package was successfully installed. Run python3.8 -V in your terminal. If you get the below output, congratulations! You have successfully installed the latest version of Python in your Debian or other Linux system.
Great job! You just learned how to install Python on Debian or Ubuntu. You can now work with multiple installations of Python versions. Along with that, you also learned to compile packages from source. So if you need to download and install a package from source later, feel free to refer to this guide.