본문 바로가기

[Linux] Upgrading to Python 3.9.16 and Setting Up Essential Libraries

Gomgom Kim 2024. 8. 27.

Overview

Let's learn how to upgrade the Python version to 3.9.16 and install the libraries required for AI and database work, specifically OpenCV and MySQL Connector.

Upgrading to Python 3.9.16

The first step is to upgrade to Python 3.9.16.

This version is necessary to use the AI library cv2 and perform database operations. Follow the steps below.

1. Check Your Current Python Version

python --version

2. Update Package Lists for Linux Distributions

sudo apt update

This command updates the package list in your Linux distribution, allowing you to find the latest version information of installable packages.

3. Upgrade Installed Packages to the Latest Version

sudo apt upgrade

4. Install Required Packages to Compile and Install Python

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

5. Download Source Code for Python 3.9.16

wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz

This command downloads the Python 3.9.16 source code, allowing you to use it immediately without needing to download or move the .tgz file separately.

6. Unzip the Downloaded Source Code File

tar -xf Python-3.9.16.tgz

7. Navigate to the Python 3.9.16 Source Code Directory

cd Python-3.9.16

8. Configure Settings Before Compiling Python

./configure --enable-optimizations

The --enable-optimizations option compiles Python with optimizations applied.

9. Compile the Source Code

make -j $(nproc)

The -j $(nproc) option allows the compilation to utilize all available CPU cores.

10. Install the Compiled Python

sudo make altinstall

The altinstall option installs the new version without replacing the default Python binary.

11. Remove Old Python Binaries Set to /usr/bin/python

sudo rm /usr/bin/python

It's crucial to delete the existing binary to avoid conflicts with the new version.

12. Link the Newly Installed Python 3.9 to /usr/bin/python

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

This ensures that Python 3.9 runs when you use the python command.

13. Verify the Python Version Upgraded to 3.9.16

python --version

Installing OpenCV

OpenCV is a powerful open-source library that includes hundreds of computer vision algorithms.

It is essential for implementing or utilizing AI. Follow the steps below.

1. Remove the Existing libapache2-mod-php7.3 Package

sudo apt purge libapache2-mod-php7.3

When installing the OpenCV library, errors may occur if the libapache2 package is not up to date.

Therefore, it is necessary to delete and reinstall the existing installation. This command completely removes the libapache2-mod-php7.3 package from the system.

'Purge' not only deletes the package but also removes all associated configuration files.

2. Reinstall the libapache2-mod-php7.3 Package

sudo apt install libapache2-mod-php7.3

Note: This package is a module that allows you to use PHP 7.3 on the Apache web server.

3. Install Python 3 Bindings of the OpenCV Library

sudo apt install python3-opencv

4. Install pip, the Python Package Manager

sudo apt install python-pip

Using pip, you can easily install various Python packages.

5. Install the opencv-contrib-python Package

pip install opencv-contrib-python

This package provides not only the main functions of OpenCV but also additional features.

Install this to use the cv2 library within your code.

Installing MySQL Connector

MySQL Connector/Python is a standardized database driver provided by MySQL.

The results obtained through AI can be stored in a database using this tool. You can install it with the command below.

pip install mysql-connector-python

Conclusion

You've now upgraded to Python 3.9.16 and set up the core libraries for AI and database work. The next step would be to create an AI and database project.

It would be a good idea to import the module in a Python script to check if the installation was successful. If no errors occur, everything is ready.

댓글