Introduction
Today, there are two active versions of Python. Although many companies are still using Python 2 for legacy reasons, Python 3 is slowly (but surely) taking over. This was confirmed by the news that Python 2 has no official support as of January 2020.
Since the programming language does not come preinstalled on the new CentOS 8, you can install the version of Python that suits your needs best. Another option would be installing both versions and switching between the two.
This article will show you how to install Python 3 and its predecessor Python 2 on CentOS 8.
Prerequisites
- A CentOS 8 Linux OS
- Access to the root user or a user account with sudo privileges
- Access to a terminal window/command line (Ctrl–Alt–F2)
Update Local Repository (Optional)
You can install Python 3 by downloading the package from the local repository with the DNF package manager. Prior to installing, we recommend updating the repository.
Open a terminal window and update the repository with the command:
dnf update
Install Python 3 on CentOS 8
With the repository up-to-date, type in the following command to download and install Python 3 on your system:
dnf install python3
If you aren’t logged in as the root user, make sure to start the command with the sudo
prefix: sudo dnf install python3
.
Verify whether Python 3 has been installed successfully:
python --version
The output should confirm you have successfully installed Python3.
Install Python 2 on CentOS 8
You can install Python 2 from the CentOS repository with a single command:
dnf install python2
Type y and hit Enter to confirm you want to install.
Verify the installation by prompting the system to show the active Python version with the command:
python2 --version
Run Python on CentOS
There is no default python
command on CentOS 8.
To run Python 3, you need to use the command:
python3
To run Python 2, type:
python2
Set Default Version of Python
If you have more than one version of Python installed on your CentOS 8, you may need to set the default version of Python. Configuring the default version helps applications and programs that require a python
command to navigate to the appropriate location.
Set Python 3 or Python 2 as the system-wide python
command.
To assign Python 3 as the default version, use the command:
alternatives --set python /usr/bin/python3
You can also set Python 2 as the unversioned python
command:
alternatives --set python /usr/bin/python2
You can also remove the unversioned command with:
alternatives --auto
Uninstall Python on CentOS 8
Use DNF to uninstall any Python version.
To remove Python 3, run the command:
dnf remove python3
The command uninstalls Python 3 and removes related dependencies. Confirm you want to remove the listed packages by typing y and hit Enter.
If you want to remove Python 2, use:
dnf remove python2
Again, verify you want to remove the package with y and Enter.
Conclusion
This step-by-step tutorial shows you how simple it is to install Python 3 and Python 2 on CentOS 8. It also includes instructions for setting the default Python version, as well as steps for removing Python from the system. Next, you can install Pip, a package manager for the Python.
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/tech/dev/223110.html