This topic describes how to install Python in different operating systems.
Install Python in Windows
Go to the Python official website. In the top navigation bar, choose Downloads > Windows.
In the Stable Releases section, select a stable version and click a download link. In this example, Python 3.12.3 is selected and Windows installer (64-bit) is downloaded.
Double-click the downloaded installation package to install Python. In this example, the downloaded installation package is python-3.12.3-amd64.exe.
Select Add python.exe to PATH and click Customize installation.
Click Next.
Modify the installation path and click Install.
After the installation is complete, press
Win+R
to open the Run dialog box. Entercmd
in the field and click OK to open Command Prompt.Enter
python
and press the Enter key. If the output is similar to that in the following figure, Python is installed.
Install Python in Linux
CentOS
Go to the Python official website and download the required Python version. In this example, Python 3.11.10 is installed.
wget https://www.python.org/ftp/python/3.11.10/Python-3.11.10.tgz
Decompress the installation package.
tar -xzvf Python-3.11.10.tgz
Install the compilation environment that is required by the source code.
sudo yum -y install gcc sudo yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel libpcap-devel xz-devel libffi-devel
Compile and install Python. Go to the directory to which the installation package is decompressed and specify the installation directory of Python 3 as /usr/python.
cd Python-3.11.10 ./configure --prefix=/usr/python make make install
Run the
which python3 pip3
command to check whether symbolic links exist in the system. If symbolic links exist, delete the symbolic links.rm -rf /usr/bin/python3 /usr/bin/pip3
Recreate and specify symbolic links.
NoteWhen a user accesses a symbolic link, the user actually accesses the file to which the symbolic link points. For example, when you use Python 3, you actually use the Python 3.11 interpreter.
sudo ln -s /usr/python/bin/python3 /usr/bin/python3 sudo ln -s /usr/python/bin/pip3 /usr/bin/pip3
Check the installed Python version.
python3 --version
Check the installed pip version.
pip3 -V
Alibaba Cloud Linux
Go to the Python official website and download the required Python version. In this example, Python 3.11.10 is installed.
sudo curl -O https://www.python.org/ftp/python/3.11.10/Python-3.11.10.tgz
Decompress the installation package.
sudo tar xzf Python-3.11.10.tgz
To compile Python and its extensions, some development tools and libraries are required. Run the following commands to install the required dependencies:
sudo yum groupinstall "Development Tools" -y sudo yum install openssl-devel bzip2-devel libffi-devel -y
Compile and install Python.
cd Python-3.11.10 sudo ./configure --enable-optimizations sudo make altinstall
NoteThe
make altinstall
command is used to prevent the default Python version from being overwritten.Run the
which python3 pip3
command to check whether symbolic links exist in the system. If symbolic links exist, delete the symbolic links.sudo rm /usr/bin/python3 sudo rm /usr/bin/pip3
Recreate and specify symbolic links.
NoteWhen a user accesses a symbolic link, the user actually accesses the file to which the symbolic link points. For example, when you use Python 3, you actually use the Python 3.11 interpreter.
sudo ln -s /usr/local/bin/python3.11 /usr/bin/python3 sudo ln -s /usr/local/bin/pip3.11 /usr/bin/pip3
Check the installed Python version.
python3 --version
Check the installed pip version.
pip3 -V
Ubuntu
Go to the Python official website and select the required Python version.
Update the software package index.
sudo apt update
Download the required Python version. In this example, Python 3.11.10 is downloaded.
wget https://www.python.org/ftp/python/3.11.10/Python-3.11.10.tgz
Decompress the downloaded source code package.
tar -xzf Python-3.11.10.tgz
Install the required dependencies.
sudo apt install -y build-essential checkinstall sudo apt install -y libreadline-gplv2-dev libncursesw5-dev libssl-dev \ libsqlite3-dev tk-dev libgdbm-dev libbz2-dev libffi-dev zlib1g-dev
Go to the directory to which the installation package is decompressed, and configure, compile, and then install Python.
cd Python-3.11.10 ./configure --enable-optimizations make -j $(nproc) sudo make altinstall
Run the
which python3 pip3
command to check whether symbolic links exist in the system. If symbolic links exist, delete the symbolic links.sudo rm /usr/bin/python3 sudo rm /usr/bin/pip3
Recreate and specify symbolic links.
NoteWhen a user accesses a symbolic link, the user actually accesses the file to which the symbolic link points. For example, when you use Python 3, you actually use the Python 3.11 interpreter.
sudo ln -s /usr/local/bin/python3.11 /usr/bin/python3 sudo ln -s /usr/local/bin/pip3.11 /usr/bin/pip3
Check the installed Python and pip versions.
python3 --version pip3 -V
FAQ
What do I do if the "The installer was interrupted before Python could be installed" error message is returned in Windows?
This error message is returned because the Windows installer of Python is interrupted. To resolve this issue, you can delete the downloaded installer, download an installation package again from the Python official website, and then install Python.
What do I do if the "-bash: python3: command not found" error message is returned in Linux?
Check whether Python 3 is installed. You can run the
which python3
command to check the directory in which Python 3 is installed. If a directory such as/usr/bin/python3
is returned, Python 3 is installed.If you have installed Python 3, this error message may be returned because symbolic links are not properly configured. Check whether symbolic links exist in the system. If necessary, delete the symbolic links and configure symbolic links again.
sudo which python3 pip3 rm -rf /usr/bin/python3 /usr/bin/pip3 # Specify the /usr/python/bin/python3 symbolic link as the installation directory of Python 3. sudo ln -s /usr/python/bin/python3 /usr/bin/python3 sudo ln -s /usr/python/bin/pip3 /usr/bin/pip3
What do I do if the "Permission denied" error message is returned in Linux?
This error message is returned because you do not have sufficient permissions to perform an operation. In most cases, you can run the
sudo
command to escalate your permissions. In this case, the commands that require administrator permissions can be run.ImportantRunning the
sudo
command is not always a secure solution. Before you run thesudo
command, we recommend that you identify the root cause of insufficient permissions. If you need to escalate your permissions, make sure that you understand the potential security risks and run the sudo command only when necessary.