Before starting, it is recommended to update the APT packages cache to the latest version. You can update them by running the following command:
apt-get update -yNext, you will need to install some packages required to install Python 3.9. Run the following command to install all dependencies:
apt-get install software-properties-common apt-transport-https gnupg2 -yBy default, Python version 3.9 is not available in the Ubuntu standard repository. So you will need to add the dead snakes PPA to your system
Run the following command to add the dead snakes PPA to your system:
add-apt-repository ppa:deadsnakes/ppaOnce the repository is added, update the repository cache and install the Python 3.9 with the following command:
apt-get update -y
apt-get install python3.9 -yOnce Python 3.9 has been installed, you can verify the installed version of Python using the command below:
python3.9 --version
#output Python 3.9.1If an older version of Python3.8 is already installed in your system then you will need to set your default Python version to Python 3.9.
First, check the default Python version using the following command:
python3 --version
#output Python 3.8.10The above output indicates that you have multiple version of Python in your system.
You can also check all Python versions in your system using the following command:
ls /usr/bin/python*
#output /usr/bin/python3 /usr/bin/python3.8 /usr/bin/python3.9Next, you will need to update your update-alternatives before setting the default Python version. You can update it with the following command:
update-alternatives --install /usr/bin/python3 python /usr/bin/python3.9 1
update-alternatives --install /usr/bin/python3 python /usr/bin/python3.8 2Next, verify whether both versions are updated or not with the following command:
update-alternatives --list python
#output /usr/bin/python3.8
#output /usr/bin/python3.9Next, set your default Python version to 3.9 using the following command:
update-alternatives --config pythonYou will be asked to set the default Python version as shown below:
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.8 2 auto mode
1 /usr/bin/python3.8 2 manual mode
2 /usr/bin/python3.9 1 manual mode
Press to keep the current choice[*], or type selection number: 2Type 2 and press Enter to set the default Python version. You should get the following output:
update-alternatives: using /usr/bin/python3.9 to provide /usr/bin/python (python) in manual modeNow, you can verify your default Python version using the following command:
python3 --version
# output Python 3.9.1Running pip in terminal will not work, as the current pip is not compatible with Python3.10 and python3-apt will be broken, that will generate an error like
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg ModuleNotFoundError: No module named 'apt_pkg'To fix this first remove the current version of python3-apt by running
apt remove --purge python3-aptThen do some cleanup
apt autocleanFinally, reinstall python3-apt by running
apt install python3-aptapt install python3.9-distutilsinstall pip by running
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.9 get-pip.pypip
#output pip --versioncurl -sSL https://install.python-poetry.org | python3 -
vim ~/.bashrc
export PATH="/root/.local/bin:$PATH"
source ~/.bashrcpoetry --version
#output Poetry (version 1.3.2)git clone repo
cd /home/readmin/
git clone http://172.31.251.105/backend/monthly-report-crawler.gitModify poetry config
poetry config virtualenvs.in-project trueCreate virtual env
poetry env use python3.9Install package
poetry install