In general, the Linux system comes with a python development environment installed by default. But if your Linux does not installed python or you want to install another python version, you can learn how to install it in this article. We will introduce how to install python & pip on Ubuntu, Fedora, and CentOS.
1. How To Check If Python Is Installed In Terminal.
- Start the command line window through the terminal of the system (you can use the shortcut key
"CT + Alt + T"
) and enter thepython
command in the command line window (note that the letter P is lowercase).$ python Python 3.7.4 (default, Aug 13 2019, 15:17:50) [Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
- The above command line shows the python prompt (>>>), which indicates that the python development environment already exists on the Ubuntu system.
- The
python
command start the interactive interpreter of Python. If you want to exit the interactive interpreter, you can press the"Ctrl + D"
shortcut or runexit()
function.>>> exit()
- If it shows the error message like Command ‘python’ not found, that means the default python command is not python, you can try the command python3.
Command 'python' not found, did you mean: command 'python3' from deb python3 command 'python' from deb python-is-python3
- If you want to check whether python3 is installed on the Ubuntu system or not, you can enter python3 in the terminal command line window.
$ python3 Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >
- If the python prompt (>>>) is also displayed on the above command line, it indicates that the python 3 development environment already exists on the Ubuntu system. Execute the
python3
command to start the python 3 development environment.
2. How To Install Python On Linux.
- If you find it does not install python on your Linux OS, or you think the built-in Python version is not newer enough, or you want to install the specified Python version, you can execute the following simple commands.
2.1 On Ubuntu and Debian-based systems.
- Run the below commands if you install Python3 on Ubuntu.
sudo apt update sudo apt install python3.11
- The first command above will update the source addresses listed in
/etc/apt/sources.list
and/etc/apt/sources.list.d
file to get the latest software package. The second command will install Python 3.11.
2.2 On Fedora and Red Hat-based systems.
- Install Python3 on Fedora and Red Hat based OS.
sudo dnf install python3
2.3 On CentOS systems.
- On CentOS 8 and newer versions.
sudo dnf install python3
- On CentOS 7 and older versions.
sudo yum install python3
3. How To Run The Newly Installed Python Version.
- After successfully executing the above command, enter the
python3
command again in the terminal command line window to see whether the python version has been updated or not. - If the python version is not changed to 3.11 and still 3.10, you can run the command python3.11 to run the newly installed python version.
- By default, python executable file is installed in the /usr/bin directory on Linux.
- We can run the command cd /usr/bin to go to the folder.
- And then run the command ls -l python3* to list the python executable file.
$ cd /usr/bin jerry@jerry-VirtualBox:/usr/bin$ ls -l python3* lrwxrwxrwx 1 root root 10 7月 21 15:51 python3 -> python3.10 -rwxr-xr-x 1 root root 5912968 5月 29 19:10 python3.10 -rwxr-xr-x 1 root root 6890080 8月 12 2022 python3.11 -rwxr-xr-x 1 root root 960 1月 25 2023 python3-futurize -rwxr-xr-x 1 root root 964 1月 25 2023 python3-pasteurize
- From the above list, we can see python3 is just a symbolic link file which link to the python3.10 executable file.
- So you can create another symbolic link file python and link it to python3.11 executable file using the below command.
$ sudo ln -s python3.11 python jerry@jerry-VirtualBox:/usr/bin$ ls -l python* lrwxrwxrwx 1 root root 10 7月 27 11:43 python -> python3.11 lrwxrwxrwx 1 root root 10 7月 21 15:51 python3 -> python3.10 -rwxr-xr-x 1 root root 5912968 5月 29 19:10 python3.10 -rwxr-xr-x 1 root root 6890080 8月 12 2022 python3.11
- The command sudo ln -s python3.11 python will create the symbolic link file python and link it to python3.11 executable file.
- Now you can run the command python in terminal and enter the python 3.11 interpreter.
$ python Python 3.11.0rc1 (main, Aug 12 2022, 10:02:14) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
4. How To Install PIP On Linux.
4.1 For Python 3.
- Most modern Linux distributions come with pip pre-installed for Python 3.
- If it’s not installed, you can install it using the package manager.
- For Debian/Ubuntu-based systems, run the command sudo apt install python3-pip.
- For Fedora/Red Hat-based systems, run the command sudo dnf install python3-pip.
4.2 For Python2.
- Note: Python 2 is deprecated and not recommended for new projects.
- If you need pip for Python 2, you can install it with the following commands.
- For Debian/Ubuntu-based systems, run the command sudo apt install python-pip.
- For Fedora/Red Hat-based systems, run the command sudo dnf install python-pip.
5. How To Use PIP On Linux.
- Once Python and pip are installed, you can use pip to install additional Python packages.
- For example, to install the requests package, you can run the command pip install requests.
- You can run the command pip -h to get all the arguments you can use with the command pip.
- The pip executable file is by default installed in the /usr/bin folder also.
- Remember to use pip3 instead of pip if you have both Python 2 and Python 3 installed on your system, to ensure you’re installing the package for Python 3.
References