{"id":2428,"date":"2023-02-12T00:26:51","date_gmt":"2023-02-11T21:26:51","guid":{"rendered":"https:\/\/joseph.zikusooka.com\/?p=2428"},"modified":"2023-02-12T22:33:28","modified_gmt":"2023-02-12T19:33:28","slug":"how-to-install-the-latest-python-3-version-on-linux-using-sources","status":"publish","type":"post","link":"https:\/\/joseph.zikusooka.com\/?p=2428","title":{"rendered":"How to install the latest Python 3 version on Linux using sources"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>Python 3.11.0 was officially released on October 24 2022. However, most Linux distributions have not yet upgraded to the latest Python version i.e. Python 3.11.2 (released on February 8 2023).   Please visit <a rel=\"noreferrer noopener\" href=\"https:\/\/www.python.org\/doc\/versions\/\" target=\"_blank\">this page<\/a> to see the most recent versions.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"541\" height=\"230\" src=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/latest_release_page-1.png\" alt=\"\" class=\"wp-image-2471\" srcset=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/latest_release_page-1.png 541w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/latest_release_page-1-300x128.png 300w\" sizes=\"auto, (max-width: 541px) 100vw, 541px\" \/><\/figure>\n\n\n\n<p>The jump from versions 3.10.X to 3.11.X brought with it significant changes and improvements to Python code.  Among the improvements  is that Python is now 10 &#8211; 60% faster than before. That\u2019s a big and noticeable speed boost.  So beyond the usual reasons for upgrades i.e. security etc, I really wanted this version on my Linux desktop as soon as the final release was made publicly available.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-rounded\"><img loading=\"lazy\" decoding=\"async\" width=\"488\" height=\"297\" src=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/comprehension.png\" alt=\"\" class=\"wp-image-2455\" srcset=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/comprehension.png 488w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/comprehension-300x183.png 300w\" sizes=\"auto, (max-width: 488px) 100vw, 488px\" \/><\/figure>\n\n\n\n<p>If you are like me and don\u2019t like waiting for your favourite open-source program to be supported on your Linux distribution, here is how to install the latest Python 3 version on Linux using sources.<\/p>\n\n\n\n<p>Before you begin, check the current version on your Linux system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python  --version<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"754\" height=\"91\" src=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/python_version_check-1.png\" alt=\"\" class=\"wp-image-2474\" srcset=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/python_version_check-1.png 754w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/python_version_check-1-300x36.png 300w\" sizes=\"auto, (max-width: 754px) 100vw, 754px\" \/><\/figure>\n\n\n\n<p>Currently the version of Python on my Linux system is three (3) releases behind. So it is time for me to upgrade to the most recent version.<\/p>\n\n\n\n<p><strong>CAUTION: <\/strong> Due to the many dependencies on the currently installed Python common package, I highly recommend that you do not remove the existing version. Instead, we will install the latest version in an alternate location e.g. \/<em>usr\/<\/em>local<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Prerequisites<\/strong><\/h3>\n\n\n\n<p>Please ensure that your Linux system has the following packages installed:<\/p>\n\n\n\n<p><strong>On Ubuntu or Debian:<\/strong><\/p>\n\n\n\n<p>sudo apt-get install build-essential<\/p>\n\n\n\n<p>sudo apt-get install autoconf automake gdb libffi-dev zlib1g-dev libssl-dev libncurses5-dev libgdbm-dev libnss3-dev libreadline-dev libsqlite3-dev<\/p>\n\n\n\n<p><strong>On Fedora or Rocky Linux:<\/strong><\/p>\n\n\n\n<p>sudo dnf groupinstall &#8220;Development Tools\u201d<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>sudo dnf install openssl-devel libffi-devel bzip2-devel sqlite-devel<\/p>\n\n\n\n<p>* You will also need wget which is generally included by default on most Linux distributions<\/p>\n\n\n\n<p>Visit the Python downloads <a href=\"https:\/\/www.python.org\/downloads\/source\/\" target=\"_blank\" rel=\"noreferrer noopener\">page<\/a> for sources to get the direct link to latest Python source package. In this tutorial I will be using the XZ compressed source tarball i.e. Python-3.11.2.tar.xz<\/p>\n\n\n\n<p>Download the latest Python package to a temporary directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget  -c  -P \/tmp  https:\/\/www.python.org\/ftp\/python\/3.11.2\/Python-3.11.2.tar.xz<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"371\" src=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/download_tarball.png\" alt=\"\" class=\"wp-image-2442\" srcset=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/download_tarball.png 782w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/download_tarball-300x142.png 300w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/download_tarball-768x364.png 768w\" sizes=\"auto, (max-width: 782px) 100vw, 782px\" \/><\/figure>\n\n\n\n<p>Unpack the Python 3 tarball:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar xvf \/tmp\/Python-3.11.2.tar.xz -C \/usr\/src<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"591\" height=\"503\" src=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/unpack_tarball.png\" alt=\"\" class=\"wp-image-2443\" srcset=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/unpack_tarball.png 591w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/unpack_tarball-300x255.png 300w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/><\/figure>\n\n\n\n<p>Compile and build Python:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd  \/usr\/src\/Python-3.11.2\n\n.\/configure  --prefix=\/usr\/local  --enable-shared  --enable-optimizations  --with-system-expat  --with-system-ffi<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"802\" height=\"469\" src=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/configure.png\" alt=\"\" class=\"wp-image-2441\" srcset=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/configure.png 802w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/configure-300x175.png 300w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/configure-768x449.png 768w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/figure>\n\n\n\n<p><strong>IMPORTANT: <\/strong> Ensure the prefix i.e. \u2018\/usr\/local\u2019 you use is different from that used by your Linux distribution otherwise you will overwrite the default Python 3 installation!<\/p>\n\n\n\n<p><strong>Optional: <\/strong>You can make some of your modules a permanent part of the Python interpreter. To do so, simply modify the file: Modules\/Setup.local before running make. To see my my local setup, see the appendix.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo  make<\/code><\/pre>\n\n\n\n<p>TIP: you can add \u201c<strong>&#8211; j $(getconf _NPROCESSORS_ONLN)<\/strong>\u201d to the make command in order to speed things up i.e. sudo make -j $(getconf _NPROCESSORS_ONLN)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"781\" height=\"456\" src=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/make.png\" alt=\"\" class=\"wp-image-2440\" srcset=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/make.png 781w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/make-300x175.png 300w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/make-768x448.png 768w\" sizes=\"auto, (max-width: 781px) 100vw, 781px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo  make  altinstall<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"781\" height=\"377\" src=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/make_altinstall.png\" alt=\"\" class=\"wp-image-2439\" srcset=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/make_altinstall.png 781w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/make_altinstall-300x145.png 300w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/make_altinstall-768x371.png 768w\" sizes=\"auto, (max-width: 781px) 100vw, 781px\" \/><\/figure>\n\n\n\n<p>Add the following export command to your shell initialization file:<\/p>\n\n\n\n<p><strong>Using Bash shell:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo  \u201cexport  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:\/usr\/local\/lib\u201d  &gt;&gt;  ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>Then source the file for now:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>source  ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>To verify if you now have the latest Python 3 (including pip installer) versions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/usr\/local\/bin\/python3.11 \u2013version\n\n\/usr\/local\/bin\/pip3.11 \u2013version<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"792\" height=\"276\" src=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/setup.png\" alt=\"\" class=\"wp-image-2437\" srcset=\"https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/setup.png 792w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/setup-300x105.png 300w, https:\/\/joseph.zikusooka.com\/wp-content\/uploads\/2023\/02\/setup-768x268.png 768w\" sizes=\"auto, (max-width: 792px) 100vw, 792px\" \/><\/figure>\n\n\n\n<p>Congratulations! You now have the latest Python 3 version on your Linux system.<\/p>\n\n\n\n<p><strong>NOTE: <\/strong> When your distribution eventually catches up, you can remove this installation by issuing the &#8216;<strong>make uninstall<\/strong>&#8216; command in the installation directory i.e. \/usr\/src\/Python3.11<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Appendix<\/h3>\n\n\n\n<p>Here&#8217;s my sample  .\/Modules\/Setup.local file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array -DPy_BUILD_CORE_MODULE arraymodule.c\n_contextvars _contextvarsmodule.c\n_struct -DPy_BUILD_CORE_MODULE _struct.c\n_random _randommodule.c -DPy_BUILD_CORE_MODULE\n_elementtree -I$(srcdir)\/Modules\/expat -DHAVE_EXPAT_CONFIG_H -DUSE_PYEXPAT_CAPI _elementtree.c\n_pickle -DPy_BUILD_CORE_MODULE _pickle.c\n_datetime _datetimemodule.c\n_zoneinfo _zoneinfo.c -DPy_BUILD_CORE_MODULE\n_bisect _bisectmodule.c\n_asyncio _asynciomodule.c\n_json -I$(srcdir)\/Include\/internal -DPy_BUILD_CORE_BUILTIN _json.c\nunicodedata unicodedata.c -DPy_BUILD_CORE_BUILTIN\nfcntl fcntlmodule.c\nspwd spwdmodule.c\ngrp grpmodule.c\nselect selectmodule.c\nmmap mmapmodule.c\n_csv _csv.c\n_socket socketmodule.c\ntermios termios.c\nresource resource.c\n_posixsubprocess  -DPy_BUILD_CORE_BUILTIN _posixsubprocess.c\naudioop audioop.c\n_md5 md5module.c\n_sha1 sha1module.c\n_sha256 sha256module.c -DPy_BUILD_CORE_BUILTIN\n_sha512 sha512module.c -DPy_BUILD_CORE_BUILTIN\n_sha3 _sha3\/sha3module.c\n_blake2 _blake2\/blake2module.c _blake2\/blake2b_impl.c _blake2\/blake2s_impl.c\nsyslog syslogmodule.c\nbinascii binascii.c\nzlib zlibmodule.c -I$(prefix)\/include -L$(exec_prefix)\/lib -lz\npyexpat expat\/xmlparse.c expat\/xmlrole.c expat\/xmltok.c pyexpat.c -I$(srcdir)\/Modules\/expat -DHAVE_EXPAT_CONFIG_H -DXML_POOR_ENTROPY -DUSE_PYEXPAT_CAPI\n_multibytecodec cjkcodecs\/multibytecodec.c<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to install the latest python version using sources on Linux<\/p>\n","protected":false},"author":1,"featured_media":2453,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"footnotes":""},"categories":[10,19],"tags":[82,83,84],"class_list":["post-2428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-tutorial","tag-python","tag-python3","tag-source"],"_links":{"self":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts\/2428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2428"}],"version-history":[{"count":24,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts\/2428\/revisions"}],"predecessor-version":[{"id":2479,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts\/2428\/revisions\/2479"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/media\/2453"}],"wp:attachment":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}