{"id":44029,"date":"2023-05-26T13:55:56","date_gmt":"2023-05-26T13:55:56","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=44029"},"modified":"2025-05-23T10:31:31","modified_gmt":"2025-05-23T10:31:31","slug":"install-python-3-12-ubuntu","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2023\/05\/install-python-3-12-ubuntu\/","title":{"rendered":"How to Install Python 3.12 (Final) in Ubuntu 22.04 | 20.04"},"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>For developers who want to prepare their project for the latest Python releases, here&#8217;s how to install Python 3.12 in all current Ubuntu releases.<\/p>\n<p>Python 3.12 finally goes stable. It features more flexible f-string parsing, Per-Interpreter GIL, new type annotation syntax for generic classes, support for the Linux <code>perf<\/code> profiler, and many performance improvements, but removed the <code>distutils<\/code> package and wstr from Unicode. See more about <a href=\"https:\/\/docs.python.org\/dev\/whatsnew\/3.12.html\" target=\"_blank\" rel=\"noopener\">Python 3.12<\/a>.<\/p>\n<h3>How to Install Python 3.12<\/h3>\n<p>Python is easy to install in Ubuntu by either using the popular Deadsnakes PPA or building from the source. Choose either one that you prefer.<\/p>\n<h4>Option 1: Install Python 3.12 from PPA<\/h4>\n<p>For Ubuntu 22.04, Ubuntu 20.04, and their derivatives such as Linux Mint 21, the <a href=\"https:\/\/launchpad.net\/~deadsnakes\/+archive\/ubuntu\/ppa\/\" target=\"_blank\" rel=\"noopener\">Deadsnakes PPA<\/a> has made the packages for all supported CPU architecture types: amd64, arm64\/armhf, ppc64el, and s390x.<\/p>\n<p><b>1.<\/b> First, press <b>Ctrl+Alt+T<\/b> on keyboard to open terminal. Then paste the command below and hit run to add PPA:<\/p>\n<pre>sudo add-apt-repository ppa:deadsnakes\/ppa<\/pre>\n<p><i>Type user password (no asterisk feedback) when it asks and hit Enter to continue.<\/i><\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/10\/deadsnake-ppa-jammy.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-43245\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/10\/deadsnake-ppa-jammy-600x262.webp\" alt=\"\" width=\"600\" height=\"262\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/10\/deadsnake-ppa-jammy-600x262.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/10\/deadsnake-ppa-jammy-300x131.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/10\/deadsnake-ppa-jammy-768x335.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/10\/deadsnake-ppa-jammy.webp 780w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><b>2.<\/b> Ubuntu 20.04+ automatically refresh package cache while adding PPA. However, Linux Mint user may need to do this job manually by running command:<\/p>\n<pre>sudo apt update<\/pre>\n<p><b>3.<\/b> Finally, run command to install Python 3.12:<\/p>\n<pre>sudo apt install python3.12<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/apt-python3.12.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-44030\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/apt-python3.12-600x379.webp\" alt=\"\" width=\"600\" height=\"379\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/apt-python3.12-600x379.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/apt-python3.12-300x190.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/apt-python3.12-768x486.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/apt-python3.12-1536x971.webp 1536w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/apt-python3.12-1320x835.webp 1320w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/apt-python3.12.webp 1572w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h4>Option 2: Compile and install Python 3.12 from source<\/h4>\n<p>Don&#8217;t trust third-party repositories or you&#8217;re running Ubuntu 23.04 or Ubuntu 18.04? It&#8217;s easy to build Python from the source tarball.<\/p>\n<p><b>1.<\/b> First download the source tarball from its ftp download page:<\/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\/ftp\/python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python FTP Download Page<\/a><\/div>\n<\/div>\n<p><b>2.<\/b> Then open &#8216;Downloads&#8217; folder, extract the source tarball, finally right-click on source folder and select &#8220;Open in Terminal&#8221;.<\/p>\n<p><b>3.<\/b> When terminal opens, run the commands below one by one to configure and build Python:<\/p>\n<pre>.\/configure --enable-optimizations<\/pre>\n<pre>sudo make -j4 &amp;&amp; sudo make altinstall<\/pre>\n<p>NOTE: You have to first install all build dependency libraries before running last 2 commands. See <a href=\"https:\/\/ubuntuhandbook.org\/index.php\/2021\/10\/compile-install-python-3-10-ubuntu\/\" target=\"_blank\" rel=\"noopener\">this tutorial<\/a> for details.<\/p>\n<h3>Verify:<\/h3>\n<p>Once installed Python 3.12, verify by running command:<\/p>\n<pre>python3.12 --version &amp;&amp; pip3.12 --version<\/pre>\n<h3>Set Python 3.12 as default<\/h3>\n<p>It&#8217;s NOT recommended to set non-preinstalled Python package as default for Python3, since it will break some core applications.<\/p>\n<p>However, you may try to set <code>python3.12<\/code> as <code>python<\/code> by running the commands below one by one:<\/p>\n<ul>\n<li>First, run command to find out where Python 3.12 executable is installed to:\n<pre>whereis python3.12<\/pre>\n<p>It&#8217;s either <code>\/usr\/bin\/python3.12<\/code> or <code>\/usr\/local\/bin\/python3.12<\/code>.<\/li>\n<li>Then, add Python 3.12 as an alternative link to python (replace <code>\/usr\/local\/bin\/python3.12<\/code> according last command output).\n<pre>sudo update-alternatives --install \/usr\/bin\/python python \/usr\/local\/bin\/python3.12 1<\/pre>\n<\/li>\n<li>Finally, run command to set default for <code>\/usr\/bin\/python<\/code> executable if more then one available:\n<pre>sudo update-alternatives --config python<\/pre>\n<\/li>\n<\/ul>\n<h3>Use pip installer for Python 3.12<\/h3>\n<p>Due to <a href=\"https:\/\/peps.python.org\/pep-0668\/\" target=\"_blank\" rel=\"noopener\">PEP 668<\/a>, <code>pip install<\/code> does no longer work like before. The recommended way to use pip is create a virtual environment first, though there are <a href=\"https:\/\/ubuntuhandbook.org\/index.php\/2024\/03\/pip-install-error-ubuntu-2404\/\" target=\"_blank\" rel=\"noopener\">few workarounds<\/a>.<\/p>\n<p>First run command to install the pyvenv binary:<\/p>\n<pre>sudo apt-get install python3.12-venv<\/pre>\n<p>Then, create a virtual environment by running command:<\/p>\n<pre>python3.12 -m venv ~\/.venv\/mypythonapp<\/pre>\n<p>This command will create a <code>.venv<\/code> hidden folder in your user home folder then create a virtual environment called <code>mypythonapp<\/code> (replace the name as you want).<\/p>\n<p>Next, use the source command to get into that virtual environment:<\/p>\n<pre>source ~\/.venv\/mypythonapp\/bin\/activate<\/pre>\n<p>Finally, run <code>pip<\/code> command to install a python package.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/python-venv.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-48963\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/python-venv-700x243.webp\" alt=\"\" width=\"610\" height=\"212\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/python-venv-700x243.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/python-venv-300x104.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/python-venv-768x267.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/05\/python-venv.webp 794w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>Tips:<\/b> Without running <code>source<\/code> command every time, you may optionally add that virtual environment into PATH, so it can be run in anywhere in terminal.<\/p>\n<p>To do so, either run command below, which works for the terminal window only, or paste it into the end of <b>.profile<\/b> file (press Ctrl+H in user home to view it) and log-out and back in.<\/p>\n<pre>export PATH=$HOME\/.venv\/mypythonapp\/bin:$PATH<\/pre>\n<h3>Uninstall Python 3.12:<\/h3>\n<p>If you installed Python 3.12 using the PPA repository, simply open terminal and run command to remove it:<\/p>\n<pre>sudo apt remove --autoremove python3.12<\/pre>\n<p>For the PPA, run command to remove it:<\/p>\n<pre>sudo add-apt-repository --remove ppa:deadsnakes\/ppa<\/pre>\n<p>If you built the Python 3.12 from source tarball, then there&#8217;s no uninstaller script to automate the job.<\/p>\n<p>However, you may manually remove the installed files by running commands:<\/p>\n<pre>cd \/usr\/local\/bin &amp;&amp; sudo rm python3.12* pip3.12 idle3.12 pydoc3.12 2to3-3.12<\/pre>\n<pre>cd \/usr\/local\/lib &amp;&amp; sudo rm -R python3.12 pkgconfig libpython3.12.a<\/pre>","protected":false},"excerpt":{"rendered":"<p>For developers who want to prepare their project for the latest Python releases, here&#8217;s how to install Python 3.12 in all current Ubuntu releases. Python 3.12 finally goes stable. It features more flexible f-string parsing, Per-Interpreter GIL, new type annotation syntax for generic classes, support for the Linux perf profiler, and many performance improvements, but [&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-44029","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-python"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/44029","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=44029"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/44029\/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=44029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=44029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=44029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}