Welcome to today’s article on how to install and configure Python 3.12 on Ubuntu 22.04 / Ubuntu 20.04 / Ubuntu 18.04 Linux system. Our installation is performed from source to ensure we build the latest release of Python 3.12 programming language. Python is an easy to learn and master, interpreted programming language known to simple and highly readable. The first release of Python was in 1991 and it currently has a large and active community of contributors and developers.
At the time of updating this article, Python 3.12.x is the newest major release of the Python programming language. This release contains many new features and optimizations. Some of the new features includes the following.
- Support for the buffer protocol in Python code (PEP 688).
- More flexible f-string parsing, allowing many things previously disallowed (PEP 701).
- A new debugging/profiling API (PEP 669).
- Even more improved error messages. More exceptions potentially caused by typos now make suggestions to the user.
- Support for isolated subinterpreters with separate Global Interpreter Locks (PEP 684).
- Support for the Linux
perf
profiler to report Python function names in traces. - Many large and small performance improvements (like PEP 709 and support for the BOLT binary optimizer), delivering an estimated 5% overall performance improvement.
Install dependencies
Before we begin the installation, let’s update the OS package index.
sudo apt update
Install all the dependencies required to build and run Python 3.12 on Ubuntu.
sudo apt install wget libncurses5-dev build-essential zlib1g-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev libbz2-dev pkg-config -y
Download and install Python 3.12
After installing the package dependencies, download the latest available Python 3.12 gzipped tarball from the Python official release page. We can do this using wget
command.
wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tgz
Extract the downloaded archive file after downloading it.
tar -xf Python-3.12.*.tgz
Change to created directory after file extraction.
cd Python-3.12.*/
Initiate configurations of Python before doing the installation.
./configure --enable-optimizations
Sample output on the execution.
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for Python interpreter freezing... ./_bootstrap_python
checking for python3.12... no
checking for python3.12... no
checking for python3.11... no
checking for python3.10... python3.10
checking Python for regen version... Python 3.10.12
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether gcc accepts -g... yes
checking for gcc option to enable C11 features... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for a sed that does not truncate output... /usr/bin/sed
checking for egrep... /usr/bin/grep -E
checking for CC compiler name... gcc
checking for stdio.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for strings.h... yes
checking for sys/stat.h... yes
checking for sys/types.h... yes
.....
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/Setup.bootstrap
config.status: creating Modules/Setup.stdlib
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
configure: creating Modules/Setup.local
configure: creating Makefile
Let’s now build Python 3.12 on Ubuntu Linux machine. The -j option is used to provide the number of cores available on the system for the build process:
make -j $(nproc)
Here is my command execution output.
...
if test $? -ne 0 ; then \
echo "generate-posix-vars failed" ; \
rm -f ./pybuilddir.txt ; \
exit 1 ; \
fi
./python -E -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform
The necessary bits to build these optional modules were not found:
_dbm _lzma _tkinter
_uuid
To find the necessary bits, look in configure.ac and config.log.
Checked 111 modules (31 built-in, 75 shared, 1 n/a on linux-x86_64, 0 disabled, 4 missing, 0 failed on import)
make[1]: Leaving directory '/root/Python-3.12.2'
After the build is done. Ansible installation can then be initiated by running the next commands.
sudo make altinstall
By using altinstall
instead of install
, the Python binary path will be /usr/bin/python.
Execution output:
.......
Creating directory /usr/local/share/man/man1
/usr/bin/install -c -m 644 ./Misc/python.man \
/usr/local/share/man/man1/python3.12.1
if test "xupgrade" != "xno" ; then \
case upgrade in \
upgrade) ensurepip="--altinstall --upgrade" ;; \
install|*) ensurepip="--altinstall" ;; \
esac; \
./python -E -m ensurepip \
$ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmpdr48x63t
Processing /tmp/tmpdr48x63t/pip-24.0-py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-24.0
Once complete, check the version:
$ python3.12 --version
Python 3.12.2
Install Python Extensions / Modules
In Python, modules provide additional functionality not natively available in Python. Modules promotes code reusability, and general collaboration among developers.
Python Package manager (PIP) is used to install third-party modules. We can install specific Pip for Python version 3.12.
curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
Confirm the installation of Python Pip using the next commands.
$ pip3.12 -V
pip 24.0 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)
Installation of a module can then be done with the command syntax shown here.
sudo pip3.12 install module-name
Here is an example on installing awscli
module.
$ sudo pip3.12 install awscli
...
Downloading awscli-1.32.49-py3-none-any.whl (4.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.4/4.4 MB 31.3 MB/s eta 0:00:00
Downloading botocore-1.34.49-py3-none-any.whl (12.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.0/12.0 MB 49.0 MB/s eta 0:00:00
Downloading PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (724 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 725.0/725.0 kB 19.5 MB/s eta 0:00:00
Downloading s3transfer-0.10.0-py3-none-any.whl (82 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 82.1/82.1 kB 10.3 MB/s eta 0:00:00
Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)
Downloading pyasn1-0.5.1-py2.py3-none-any.whl (84 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 84.9/84.9 kB 12.9 MB/s eta 0:00:00
Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 247.7/247.7 kB 30.9 MB/s eta 0:00:00
Downloading urllib3-2.0.7-py3-none-any.whl (124 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 124.2/124.2 kB 22.0 MB/s eta 0:00:00
Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: urllib3, six, PyYAML, pyasn1, jmespath, docutils, colorama, rsa, python-dateutil, botocore, s3transfer, awscli
Successfully installed PyYAML-6.0.1 awscli-1.32.49 botocore-1.34.49 colorama-0.4.4 docutils-0.16 jmespath-1.0.1 pyasn1-0.5.1 python-dateutil-2.8.2 rsa-4.7.2 s3transfer-0.10.0 six-1.16.0 urllib3-2.0.7
You can list all the installed modules using the command;
$ pip3.12 list
Package Version
--------------- -------
awscli 1.32.49
botocore 1.34.49
colorama 0.4.4
docutils 0.16
jmespath 1.0.1
pip 24.0
pyasn1 0.5.1
python-dateutil 2.8.2
PyYAML 6.0.1
rsa 4.7.2
s3transfer 0.10.0
setuptools 69.1.1
six 1.16.0
urllib3 2.0.7
wheel 0.42.0
...
Conclusion
Python is the basis in building a strong foundation on the understanding of programming concepts and will surely open doors to diverse career opportunities if you are good at it. Installation of Python is the starting point to doing projects that solves varying needs and user needs.
Important links and resources
- Official online Documentation.
- You can report bugs via GitHub Issues.
- Help fund Python and its community.