
{"id":34850,"date":"2021-10-08T15:48:08","date_gmt":"2021-10-08T15:48:08","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=34850"},"modified":"2023-10-03T13:27:40","modified_gmt":"2023-10-03T13:27:40","slug":"compile-install-python-3-10-ubuntu","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2021\/10\/compile-install-python-3-10-ubuntu\/","title":{"rendered":"How to Compile &#038; Install Python 3.11\/3.10 from Source in Ubuntu"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-34796\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon-250x250.png\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon-250x250.png 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon-300x300.png 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon-600x600.png 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon-768x768.png 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon.png 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This simple tutorial shows how to compile and install Python 3.11, Python 3.10, or other certain Python version in Ubuntu.<\/p>\n<p>For Ubuntu 18.04, Ubuntu 20.04 and Ubuntu 22.04 LTS, there&#8217;s well trusted <a href=\"https:\/\/launchpad.net\/~deadsnakes\/+archive\/ubuntu\/ppa\" target=\"_blank\" rel=\"noopener\">\u201cdeadsnakes\u201d team PPA<\/a> that maintains all the Python packages. For non-LTS releases, you may build the programming language package manually from the source tarball.<\/p>\n<p><b>NOTE:<\/b> This tutorial is tested and works in Ubuntu 20.04, Ubuntu 22.04, and Ubuntu 23.04 in my case.<\/p>\n<h3>1. Preparation:<\/h3>\n<p>Before getting started, you need to install some essential packages for building the computer language package.<\/p>\n<p>Open terminal either by pressing Ctrl+Alt+T on keyboard or by searching from start menu. When it opens, run the command below to install the dependencies:<\/p>\n<pre>sudo apt install wget build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/apt-python-deps.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34851\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/apt-python-deps.png\" alt=\"\" width=\"600\" height=\"162\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/apt-python-deps.png 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/apt-python-deps-300x81.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h3>2. Download Python Tarball:<\/h3>\n<p>You can simply download the latest tarball from the Python website via the link below:<\/p>\n<div class=\"wp-block-buttons aligncenter\">\n<div class=\"wp-block-button is-style-fill\"><a class=\"wp-block-button__link has-vivid-cyan-blue-to-vivid-purple-gradient-background has-text-color has-background\" href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noreferrer noopener\">Download Python<\/a><\/div>\n<\/div>\n<p>In case you&#8217;re running Ubuntu without desktop environment, download the source package by running the <code>wget<\/code> command in terminal:<\/p>\n<pre>wget -c https:\/\/www.python.org\/ftp\/python\/3.11.0\/Python-3.11.0.tar.xz<\/pre>\n<p>For other Python version, go and download via web browser in <a href=\"https:\/\/www.python.org\/ftp\/python\/\" target=\"_blank\" rel=\"noopener\">this page<\/a>. And, change the version number in command accordingly.<\/p>\n<p>And uncompress the tarball either by right-click and selecting &#8216;<b>Extract Here<\/b>&#8216; in file manager, or by running command in terminal:<\/p>\n<pre>tar -Jxf Python-3.11.0.tar.xz<\/pre>\n<h3>3. Configure the source:<\/h3>\n<p>Depends on how you grab the source tarball, either right-click on it in file manager, extract, and open the source folder in terminal via context menu option:<\/p>\n<div id=\"attachment_43249\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-terminal.webp\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-43249\" class=\"size-large wp-image-43249\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-terminal-600x364.webp\" alt=\"\" width=\"600\" height=\"364\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-terminal-600x364.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-terminal-300x182.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-terminal-768x466.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-terminal.webp 920w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-43249\" class=\"wp-caption-text\">Open Python 3.11 source folder in terminal<\/p><\/div>\n<p>If you just did the <code>tar<\/code> command above, then you may navigate to source folder via:<\/p>\n<pre>cd Python-3.11.0\/<\/pre>\n<p><i>The command varies depends on which Python version you downloaded.<\/i><\/p>\n<p>When you&#8217;re in source directory, run command to configure the source with expensive, stable optimizations (PGO, etc.):<\/p>\n<pre>.\/configure --enable-optimizations<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-configure.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-43251\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-configure-600x257.webp\" alt=\"\" width=\"600\" height=\"257\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-configure-600x257.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-configure-300x128.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-configure-768x329.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python311-configure.webp 778w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h3>4. Build and install Python:<\/h3>\n<p>Finally compile and install it into &#8220;\/usr\/local\/bin&#8221; by running command:<\/p>\n<pre>sudo make -j4 &amp;&amp; sudo make altinstall<\/pre>\n<p>Here <code>-j4<\/code> will start 4 threads to speed up the job. Depends on your CPU, you may increase the number or just skip it. And, when <code>make<\/code> command done, <strong>it will ask you to type password<\/strong> for running make install with <code>sudo<\/code> privilege.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-altinstall.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-43253\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-altinstall-600x222.webp\" alt=\"\" width=\"600\" height=\"222\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-altinstall-600x222.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-altinstall-300x111.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-altinstall-768x284.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-altinstall.webp 784w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h3>5. Verify<\/h3>\n<p>If everything&#8217;s done successfully without error, you may verify by running command:<\/p>\n<pre>python3.11 --version<\/pre>\n<pre>python3.11 -m pip --version<\/pre>\n<p>It should output the language version as well as the pip package manager version for you<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/verify-pip311.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-43252\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/verify-pip311-600x196.webp\" alt=\"\" width=\"600\" height=\"196\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/verify-pip311-600x196.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/verify-pip311-300x98.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/verify-pip311-768x251.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/verify-pip311.webp 772w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h3>5. Make Python 3.11 default<\/h3>\n<p><b>IMPORTANT: Change default Python3 in Ubuntu MAY cause issues!!! Do it at your own risk.<\/b><\/p>\n<p>After installation, you may set it as default by creating symbolic links to \/usr\/bin\/python3:<\/p>\n<pre>sudo update-alternatives --install \/usr\/bin\/python3 python3 \/usr\/local\/bin\/python3.10 110<\/pre>\n<pre>sudo update-alternatives --install \/usr\/bin\/python3 python3 \/usr\/local\/bin\/python3.11 100<\/pre>\n<p>In the first command change system default <strong>python3.10<\/strong> to <strong>python3.8 <\/strong>if you&#8217;re on Ubuntu 20.04. Finally, run command below at any time to select which Python to use as default:<\/p>\n<pre>sudo update-alternatives --config python3<\/pre>\n<h3>Uninstall Python 3.11:<\/h3>\n<p>Until you removed the source directory, you may first either navigate to that folder in terminal or right-click and select open folder in terminal, finally run <code>make uninstall<\/code> command to remove Python 3.11:<\/p>\n<pre>sudo make uninstall<\/pre>\n<p>If you&#8217;ve already removed the source, or the previous command does not work for you, try removing all the installed libraries manually via:<\/p>\n<pre>cd \/usr\/local\/bin &amp;&amp; rm -f sudo rm -f 2to3 2to3-3.11 idle3 idle3.11 pip pip3 pip3.10 pip3.11 pydoc3 pydoc3.11 python3 python3.11 python3.11-config python3-config<\/pre>","protected":false},"excerpt":{"rendered":"<p>This simple tutorial shows how to compile and install Python 3.11, Python 3.10, or other certain Python version in Ubuntu. For Ubuntu 18.04, Ubuntu 20.04 and Ubuntu 22.04 LTS, there&#8217;s well trusted \u201cdeadsnakes\u201d team PPA that maintains all the Python packages. For non-LTS releases, you may build the programming language package manually from the source [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":34796,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[2045],"class_list":["post-34850","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-python"],"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/34850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=34850"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/34850\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/34796"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=34850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=34850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=34850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}