python 3 - vincios/rpi-setup GitHub Wiki
Overview
Apt's Python 3 version is always out-of-date, so we have to build it from scratch.
Install
-
Start by installing the packages necessary to build Python source
$ sudo apt update $ sudo apt install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev
-
Download the latest release’s XZ compressed source tarball from the Python download page with
wget
orcurl
$ wget https://www.python.org/ftp/python/3.X.Y/Python-3.X.Y.tar.xz
-
When the download is complete, extract the tarball, navigate to the Python source directory and run the configure script
$ tar -xf Python-3.X.Y.tar.xz $ cd Python-3.X.Y $ ./configure --enable-shared --enable-optimizations
[!NOTE] 💡 The script performs a number of checks to make sure all of the dependencies on your system are present. The
--enable-optimizations
option will optimize the Python binary by running multiple tests, which will make the build process slower💡 The OPTIONAL
--enable-shared
option should be the equivalent of install thepython-dev
package from apt.python-dev
contains the header files you need to build python extensions
-
Run
make
to start the build process$ make -j 4
[!NOTE] 💡 Pass to the
-j
argument the number of cores in your CPU (4 for Raspberry Pi 4). You can check this with the commandnproc
-
Once the build is done, install the Python binaries by running the following command as a user with sudo access
$ sudo make altinstall
[!CAUTION] ⚠️ Do not use the standard
make install
as it will overwrite the default system python3 binary
-
(OPTIONAL) If you have launched the command
./configure
with the--enable-shared
option, you must add the python shared libraries to the dynamic linker$ sudo ldconfig -v /usr/local/lib
[!TIP] 💡 Use the
ldconfig
utility is equivalent to add the/usr/local/lib
path to theLD_LIBRARY_PATH
environment variable. But theLD_LIBRARY_PATH
environment variable should be set on each shell login (and is ignored by system services)
-
Clean up downloaded files
$ cd .. $ sudo rm -rf Python-3.X.Y.tar.xz $ sudo rm -rf Python-3.X.Y
Now Python 3 is installed. To use it instead of the system default you have to explicity run python3.X
, such as
$ python3.X --version