Python 3.9.2 Installation on Ubuntu
- Check whether the Python development environment that meets the version requirements is installed. For details, see Checking the OS Requirements and Environment. This section uses Python 3.9.2 as an example.
Run the python3 --version command. If the command output meets the Python version requirements (3.9.0 to 3.9.2), skip this section. Otherwise, perform the following steps to install the required Python version:
- Install Python dependencies.
sudo apt-get install -y make zlib1g zlib1g-dev build-essential libbz2-dev libsqlite3-dev libssl-dev libxslt1-dev libffi-dev openssl python3-tk
libsqlite3-dev must be installed before the Python installation. If the Python environment has been installed in the user's OS before the libsqlite3-dev installation, you need to rebuild the Python environment. If python3-tk fails to be installed, resolve the issue with the solution described in How Do I Handle the Error Message Displayed During python3-tk Installation?
- Install Python 3.9.2.
- Run the wget command to download the source package of Python 3.9.2 to any directory on the AMCT server:
wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
- Go to the download directory and decompress the source package:
tar -zxvf Python-3.9.2.tgz
- Go to the decompressed folder and run the following configuration, build, and installation commands:
cd Python-3.9.2 ./configure --prefix=/usr/local/python3.9.2 --with-ssl-default-suites=openssl --enable-shared CFLAGS=-fPIC make sudo make install
The --prefix option specifies the Python installation path. You can change it as required. The --enable-shared option is used to compile the dynamic library.
This document uses --prefix=/usr/local/python3.9.2 as an example. After executing the above commands, find the installation package in the /usr/local/python3.9.2 directory.
- Set the Python 3.9.2 environment variables.
- If Python is installed by the root user:
AMCT is installed by the root user. Run the following commands in the current terminal window to set environment variables:
# Set the Python 3.9.2 library path. export LD_LIBRARY_PATH=/usr/local/python3.9.2/lib:$LD_LIBRARY_PATH # If multiple Python 3 versions exist in the user environment, specify Python 3.9.2. export PATH=/usr/local/python3.9.2/bin:$PATH
If the running user is the root user, it is recommended not to modify the .bashrc file. Otherwise, the Python tools provided by other systems may be unavailable. If you want to use the default tool, open another terminal window.
- If Python is installed by a non-root user:AMCT is also installed by the non-root user. Run the vi ~/.bashrc command in any directory to open the .bashrc file, and append the following lines to the file:
# Set the Python 3.9.2 library path. export LD_LIBRARY_PATH=/usr/local/python3.9.2/lib:$LD_LIBRARY_PATH # If multiple Python 3 versions exist in the user environment, specify Python 3.9.2. export PATH=/usr/local/python3.9.2/bin:$PATH
Run the :wq! command to save the file and exit. Run the source ~/.bashrc command for the modification to take effect immediately.
- If Python is installed by the root user:
- Run the wget command to download the source package of Python 3.9.2 to any directory on the AMCT server:
- After the installation is complete, run the following commands to check the installed version. If the required version information is displayed, the installation is successful.
python3 --version pip3 --version