Install the latest version of Python on Linux

The new version of Python 3.11 was released yesterday. Officials say that 3.11 is 10-60% faster than 3.10, the standard test set is 1.25x faster, and there are other updates. The question is, how to update to the latest version under Linux.

1. Download the latest version of Python source code from the official website

Go to the official Python website https://www.python.org/downloads/

download latest python

2. Unzip

Unzip the latest version of Python downloaded in the previous step

tar -xvf Python-3.11.0.tar.xz

Go to the unzipped directory

cd Python-3.11.0

3. Install dependencies

If you use Ubuntu/Debian the corresponding dependencies are:

sudo apt-get install build-essential gdb lcov pkg-config \
    libbz2-dev libffi-dev libgdbm-dev libgdbm-compat-dev liblzma-dev \
    libncurses5-dev libreadline6-dev libsqlite3-dev libssl-dev \
    lzma lzma-dev tk-dev uuid-dev zlib1g-dev

Attentive readers may have questions, how do you know that you depend on these installation packages? In fact, the README.rst file in the source code directory after Python decompression mentions the relevant dependency instructions. It is a good habit to read the documentation before installing the software. As shown in the figure below, the link contains instructions for dependent packages.

Python readme

4. Compile

There is no explanation for this step. If you need to change the configuration, you can run ./configure --help to query the help information.

./configure
make

It is worth mentioning that make command's j parameter, such as a system with a 4-core cpu, make -j4 can speed up the compilation

5. Installation ( important steps )

This step is very important, because Python is an important software package of the Linux system. In most cases, direct 'make install' installation is not recommended. This will overwrite the basic version of Python of the system, resulting in unnecessary unexpected situations. It is strongly recommended:

make altinstall

'make altinstall' will install to ${prefix}/bin/python3, and ${prefix}/bin/python3 is linked to ${prefix}/bin/python3.X, so that multiple versions can coexist harmoniously and do not violate each other.

6. Confirm

If you look carefully at the echo of the previous step, the latest version of Python is installed by default to /usr/local/bin/python3.11

~ which python3.11
/usr/local/bin/python3.11

In order to save a few characters, you can make a soft link. If /usr/bin/python exists, you can back it up first and then delete it. Most systems are now upgraded to python3 by default, so choose to link to python instead of python3

sudo ln -s /usr/local/bin/python3.11 /usr/bin/python

8. Install iPython

iPython is easier to use than the interpreter that comes with Python

python -m pip install ipython

9. Final Results

Checked Python and iPython, everything works

➜  ~ python
Python 3.11.0 (main, Oct 25 2022, 11:03:14) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
➜  ~ ipython
Python 3.11.0 (main, Oct 25 2022, 11:03:14) [GCC 12.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.5.0 -- An enhanced Interactive Python. Type '?' for help.

Finally, the official Python documentation also has some related instructions, you can take a look if you are interested: https://docs.python.org/3.11/using/unix.html#getting-and-installing-the-latest-version-of-python

See Also:

Translations: