Install git
$ sudo apt install git
Install Python build dependencies
Python includes a set of modules that it builds by linking to other popular open source projects. Many of those projects are included in Debian by default as executable programs, but the development files (headers, libraries) necessary to link them into Python are packaged separately and not included in a default Debian install.
Some of these libraries are actually required for the python build/install steps to complete successfully:
| Library | Python Module | Dev Package |
| ---------------------------------- | ------------- | ------------ |
| https://www.zlib.net/ | `zlib` | `zlib1g-dev` |
| https://www.sourceware.org/libffi/ | `_ctypes` | `libffi-dev` |
The entries in the 'Dev Package' column are the names of packages containing the development files our python build needs.
Each of these 'dev' packages has a corresponding binary package that is probably already installed. So you're likely not going to be adding new pieces of software to your system by installing these. You're just installing the files necessary to compile new pieces of software (in our case a newer python) so that it can use them. (Also note that these 'dev' packages all have their corresponding binary packages as dependencies, so installing a dev package will ensure that its binary package is also installed.)
So let's install them:
sudo apt install zlib1g-dev libffi-dev
Next we have the OpenSSL library. Python considers OpenSSL optional, but you probably want it. For example, even using Python's package installation
tools can run into trouble when fetching https urls if SSL/TLS support is missing.
| Library | Python Module | Dev Package |
| ------------------------ | ------------- | ----------- |
| https://www.openssl.org/ | `_ssl` | `libssl-dev |
Let's install it:
sudo apt install libssl-dev
The next set of packages fall more in the 'optional' category:
| Library | Python Module(s) | Dev Package |
| ---------------------------------------------------- | ----------------------------- | ------------------ |
| http://www.bzip.org/ | `_bz2` | `libbz2-dev` |
| https://www.gnu.org/software/ncurses/ | `_curses` and `_curses_panel` | `libncursesw5-dev` |
| https://www.gnu.org.ua/software/gdbm/ | `_dbm` and `_gdbm` | `libgdbm-dev` |
| https://tukaani.org/xz/ | `_lzma` | `liblzma-dev` |
| https://www.sqlite.org/ | `_sqlite3` | `libsqlite3-dev` |
| https://www.tcl.tk/software/tcltk/ | `_tkinter` | `tk-dev` |
| https://github.com/karelzak/util-linux | `_uuid` | `uuid-dev` |
| https://tiswww.case.edu/php/chet/readline/rltop.html | `readline` | `libreadline-dev` |
Python can build and install without these, and your applications might not need them. On the other hand, if you install them, the 'make' step shouldn't report any modules it was unable to build, and the binaries are already included in a default Debian install.
The command to copy-paste if you'd like to go ahead and install all of the above 'optional' packages is:
sudo apt install libbz2-dev libncursesw5-dev libgdbm-dev liblzma-dev libsqlite3-dev tk-dev uuid-dev libreadline-dev
Build and install python from source
- clone the python git repository.
$ git clone [email protected]:python/cpython.git
Cloning into 'cpython'...
[...]
Resolving deltas: 100% (592215/592215), done.
- Checkout the version you want to install (in this case, 3.7.2) using the git tag name.
$ cd cpython/
cpython$ git checkout v3.7.2
Note: checking out 'v3.7.2'.
[...]
HEAD is now at 9a3ffc0492... 3.7.2final
cpython$
- Choose an install prefix. That is, the path the compiled and linked project will be installed into. In this case I'll use
$HOME/python/v3.7.2. I'm including the version number since I'll want to install other versions in the future, and keep them separate. Run theconfigurescript with this path as the--prefixargument. (If you don't provide a --prefix argument, it will default to/usr/local).
cpython$ ./configure --prefix=$HOME/python/v3.7.2
[...]
checking for inflateCopy in -lz... yes
[...]
checking for openssl/ssl.h in /usr... yes
[...]
The configure script will check a bunch of things, many of which are unnecessary. The ones shown in the above output, though, can be considered necessary.
- Build python by running
make. If we included the minimum set of dependencies necessary to build and install python, the output will tell you near the end what modules were not built:
cpython$ make
[...]
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_dbm _gdbm _lzma
_sqlite3 _tkinter _uuid
readline
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The make output describes this as a list of 'optional' modules but again, at least for this particular version of python, it probably won't build and install successfully without the zlib, and _ctypes modules.
- Install python by running
make install. We've already provided the install location back in theconfigurestep. If your install location is a root-owned directory (e.g./usr/local), prefix this command withsudo. Since in this case I'm installing to a user-owned directory, I don't want to do that.
cpython$ make install
Creating directory /home/python/v3.7.2/bin
Creating directory /home/python/v3.7.2/lib
[...]
- You should now have an executable you can run from the
bindirectory underneath the install prefix namedpython3. (Note that this installer doesn't put anything atbin/python; justbin/python3.)
HOME/python/v3.7.2/bin/python3
Python 3.7.2 (v3.7.2:9a3ffc0492, Mar 10 2019, 19:35:56)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$
Answer from Charlie on Stack ExchangeHello guys,
I'm very struggling installing Python3.7 on a very old Debian distrib.
First, I cannot use a more recent version of debian, its a very old PC with a soft that cannot be upgraded so I'm stuck with Debian jessie.
The program I wrote need at least a Python 3.7 so I'm using a Docker image of debian jessie to build the application as standalone using Pyinstaller but I'm not able to go that far...
I'v followed some tutorials to do so like : https://kitabi.eu/blog/2019-06-26-python-install/ or https://jameskiefer.com/posts/installing-python-3.7-on-debian-8/
But I cant download packages with pip, I got a SSLError("Can't connect to HTTPS URL because the SSL module is not available")
What am I doing wrong ? Do you have some solutions ? Like an existing Docker image of debian jessie with python 3.7 already installed ? lol
Thanks in advance, I'm a bit desesperate at this point o7
Python 3.7 on Debian 9 from package manager - Unix & Linux Stack Exchange
linux - Install Python 3.7.x on Windows Subsytem Debian 9.6 - Stack Overflow
Installing Python 3.7 on old Debian (jessie 8)
Debian 10 Buster using Python 3.7
Videos
Hello guys,
I'm very struggling installing Python3.7 on a very old Debian distrib.
First, I cannot use a more recent version of debian, its a very old PC with a soft that cannot be upgraded so I'm stuck with Debian jessie.
The program I wrote need at least a Python 3.7 so I'm using a Docker image of debian jessie to build the application as standalone using Pyinstaller but I'm not able to go that far...
I'v followed some tutorials to do so like : https://kitabi.eu/blog/2019-06-26-python-install/ or https://jameskiefer.com/posts/installing-python-3.7-on-debian-8/
But I cant download packages with pip, I got a SSLError("Can't connect to HTTPS URL because the SSL module is not available")
What am I doing wrong ? Do you have some solutions ? Like an existing Docker image of debian jessie with python 3.7 already installed ? lol
Thanks in advance, I'm a bit desesperate at this point o7
I would say there is no Debian equivalent to Ubuntu's deadsnakes PPA
Under Debian, using Ubuntu packages or repositories is not recommended. As this post appears in search engines, I propose here an answer that is less dangerous for a Debian system.
Installing Python manually is possible. As an example, you can use the following instructions to install 3.5.2 version
Prerequisites
Install dependencies :
sudo apt-get update && sudo apt-get install libssl-dev openssl
Building Python
You can build Python in a specific folder using the --prefix parameter from configure command:
wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
tar zxf Python-3.5.2.tgz
cd Python-3.5.2/
./configure --prefix=/usr/local
make
sudo make install
Instead of /usr/local, one can use another base directory. As an example:
sudo mkdir /opt/python-3.5.2
./configure --prefix=/opt/python-3.5.2
Selecting python version
Using PATH environment variable can help choosing the right python version to use. But one can also use symlinks:
sudo ln -s /opt/python-3.5.2/bin/python3.5 /usr/local/bin/python3
sudo ln -s /opt/python-3.5.2/bin/pip3.5 /usr/local/bin/pip3
Using -f option will allow you to replace existing symlinks
Note: For python 3.9.16 (possibly any +3.9 version) you also might need to install the library: libffi-dev so the dependencies would be:
sudo apt-get update && sudo apt-get install libssl-dev openssl libffi-dev
Using the PPA
You can use the PPA on Debian. Pick an Ubuntu version that's from slightly before your Debian version, and it should have all the necessary libraries. For wheezy, the oneiric PPA seems ok (but it lacks more recent Python versions). For jessie, the trusty PPA should work.
To add a PPA on Debian,
Download and add the PPA signing key with:
gpg --keyserver keyserver.ubuntu.com --recv-keys F23C5A6CF475977595C89F51BA6932366A755776 gpg --export F23C5A6CF475977595C89F51BA6932366A755776 | sudo tee /usr/share/keyrings/ppa-deadsnakes.gpg > /dev/nullThen create a file
/etc/apt/sources.list.d/ppa-deadsnakes.listcontaining:deb [signed-by=/usr/share/keyrings/ppa-deadsnakes.gpg] https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu/ trusty main deb-src [signed-by=/usr/share/keyrings/ppa-deadsnakes.gpg] https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu/ trusty mainFinally run
apt-get updateand install the desired packages.
If you can't get the PPA working for some reasons (maybe you can't find a version that works with the libraries you have), you can download the source and recompile them for your distribution.
Using a chrooted system
What I usually do to test compatibility with other versions is to run older or newer distributions in a chrooted system. For example, you could install various versions of Ubuntu with the Python versions you're interested in, or you could install trusty in a chroot and install the PPA there. For more information, see my schroot guide.