Installing Python on Linux
Python is pre-installed on many Linux distributions, but it’s often necessary to install the latest version manually or manage multiple versions of Python. This guide will walk you through installing Python on Linux, setting up a virtual environment, and verifying the installation.
1. Checking Python Pre-installed on Linux
Many Linux distributions come with Python pre-installed. Before you install Python manually, check if it's already available on your system and what version is installed.
Step-by-Step Instructions:
-
Open a Terminal:
- Press
Ctrl + Alt + T
to open the terminal on your Linux system.
- Press
-
Check the Python Version:
- Run the following command to check if Python 3 is already installed:
python3 --version
- This will show the installed Python version (e.g.,
Python 3.x.x
). If it's already installed, you can skip to the Virtual Environments section.
- Run the following command to check if Python 3 is already installed:
If Python 3 is not installed or if you want to install a different version, follow the steps below to install Python manually.
2. Installing Python on Linux
If Python is not installed or you want a specific version, you can install it using your Linux distribution's package manager. The commands vary depending on the distribution you're using (e.g., Ubuntu, Debian, CentOS, Fedora).
Installation on Ubuntu/Debian-Based Systems
-
Update the Package List:
- Run the following command to update the package list on your system:
sudo apt update
- Run the following command to update the package list on your system:
-
Install Python 3:
- To install Python 3, use the following command:
sudo apt install python3
- To install Python 3, use the following command:
-
Install pip:
- pip is Python's package manager. To install pip, run:
sudo apt install python3-pip
- pip is Python's package manager. To install pip, run:
Installation on Fedora/CentOS-Based Systems
-
Update the Package List:
- On Fedora, CentOS, or RedHat systems, update the package list:
sudo dnf update
- On Fedora, CentOS, or RedHat systems, update the package list:
-
Install Python 3:
- To install Python 3, use the following command:
sudo dnf install python3
- To install Python 3, use the following command:
-
Install pip:
- pip is Python's package manager. To install pip, run:
sudo dnf install python3-pip
- pip is Python's package manager. To install pip, run:
3. Verifying the Python Installation
After installing Python, it's important to verify that it was installed correctly and is accessible from the terminal.
Step-by-Step Instructions:
-
Check the Python Version:
- Run the following command to check the installed version of Python:
python3 --version
- This should display the version of Python you just installed (e.g.,
Python 3.x.x
).
- Run the following command to check the installed version of Python:
-
Check pip:
- Verify that pip was installed correctly by running:
pip3 --version
- This will display the version of pip installed.
- Verify that pip was installed correctly by running:
If both commands return the correct versions, Python is successfully installed and ready to use.
4. Setting Up a Virtual Environment
Creating virtual environments allows you to isolate Python packages for each project. This is especially useful in managing dependencies across different projects.
Step-by-Step Instructions:
-
Install
venv
(if not installed):- Python 3 includes the
venv
module for creating virtual environments. If it's not installed, use the following command:sudo apt install python3-venv # On Ubuntu/Debian
sudo dnf install python3-venv # On Fedora/CentOS
- Python 3 includes the
-
Create a Virtual Environment:
- Navigate to the directory where you want to create your project, and run the following command to create a virtual environment:
python3 -m venv myenv
- This creates a virtual environment named
myenv
.
- Navigate to the directory where you want to create your project, and run the following command to create a virtual environment:
-
Activate the Virtual Environment:
- To activate the virtual environment, run:
source myenv/bin/activate
- After activation, the prompt will change to show
(myenv)
, indicating you are now working inside the virtual environment.
- To activate the virtual environment, run:
-
Deactivate the Virtual Environment:
- When you're done working in the virtual environment, deactivate it by typing:
deactivate
- When you're done working in the virtual environment, deactivate it by typing:
5. Installing Python Packages
Once Python is installed, you can install external packages using pip.
Step-by-Step Instructions:
-
Install a Package:
- To install a Python package using pip, use the following command:
pip3 install package_name
- For example, to install the
requests
library:pip3 install requests
- To install a Python package using pip, use the following command:
-
Verify the Installed Packages:
- To see a list of installed packages, run:
pip3 list
- To see a list of installed packages, run:
6. Managing Multiple Python Versions (Optional)
If you need to manage multiple versions of Python, you can use tools like pyenv
to easily switch between different versions.
Installing pyenv
-
Install Dependencies:
- First, install the required dependencies for building Python versions:
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
- First, install the required dependencies for building Python versions:
-
Install pyenv:
- Run the following commands to install pyenv:
curl https://pyenv.run | bash
- Run the following commands to install pyenv:
-
Update Shell Configurations:
- Add the following lines to your
~/.bashrc
or~/.zshrc
to configure your shell to use pyenv:export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
- Add the following lines to your
-
Install and Use Different Python Versions:
- Once pyenv is installed, you can install different Python versions:
pyenv install 3.x.x
pyenv global 3.x.x # Set the global default Python version
- Once pyenv is installed, you can install different Python versions:
Conclusion
You’ve successfully installed Python on your Linux machine, verified the installation, set up virtual environments, and learned how to install Python packages. With Python installed, you're ready to start writing code and working on data science, automation, or other projects.