Getting Started with Python on Linux
Python is one of the most popular programming languages due to its simplicity and versatility. If you’re new to Python and Linux, this guide will help you get up and running quickly so you can start writing your own Python programs.
Why Use Python on Linux?
Linux is an excellent environment for programming because of its open-source nature and strong support for development tools. Python, being cross-platform, runs smoothly on Linux, offering robust features for everything from web development to data analysis and automation. Python’s straightforward syntax makes it perfect for beginners, while its capabilities ensure it remains powerful for advanced users.
Step 1: Checking If Python Is Already Installed
Before installing Python, check if it is already installed on your Linux system. Open a terminal window and type the following command and press Enter:
python3 --version
python3 --version
If Python is correctly installed, you will see the version of Python displayed in the terminal, something like:
python 3.8.10
python 3.8.10
If Python is not installed, or if you see an outdated version, follow the steps below to install the latest version.
Step 2: Installing Python on Linux
To install Python on Linux, you can use the package manager for your distribution. Here are the installation commands for a few popular distributions:
- For Ubuntu/Debian-based systems:For Fedora:
sudo apt update sudo apt install python3
sudo apt update sudo apt install python3
sudo dnf install python3
sudo dnf install python3
sudo pacman -S python
sudo pacman -S python
Once the installation is complete, verify that Python was installed correctly by checking the version again:
python3 --version
python3 --version
Step 3: Installing pip (Python Package Installer)
pip is a package manager for Python that allows you to easily install third-party libraries and packages. To install pip for Python 3, run the following command:
- For Ubuntu/Debian-based systems:For Fedora:
sudo apt install python3-pip
sudo apt install python3-pip
sudo dnf install python3-pip
sudo dnf install python3-pip
sudo pacman -S python-pip
sudo pacman -S python-pip
To verify that pip was installed
pip3 --version
pip3 --version
Step 4: Install Visual Studio Code via Package Manager
If you want to install Visual Studio Code via the package manager and ensure it gets updated automatically, you need to follow the steps to add the Microsoft repository. These steps are necessary to configure your package manager properly, allowing you to receive updates through apt as part of your system’s package management.
- Install Required Dependencies
First, make sure you have the necessary packages to add repositories over HTTPS:sudo apt install software-properties-common apt-transport-https curl
sudo apt install software-properties-common apt-transport-https curl
- Add Microsoft’s GPG Key
Next, download and add Microsoft’s GPG key to authenticate packages from the Microsoft repository:curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor >packages.microsoft.gpg sudo mv packages.microsoft.gpg /usr/share/keyrings/
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor >packages.microsoft.gpg sudo mv packages.microsoft.gpg /usr/share/keyrings/
- Add the Visual Studio Code Repository
Add the official Microsoft repository to your system’s package sources:sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
- Install Visual Studio Code
Update your package list and install Visual Studio Code:sudo apt update sudo apt install code
sudo apt update sudo apt install code
- Launch VS Code
Once installed, you can launch Visual Studio Code by typing:code
code
Step 5: Run Your First Python Script
It's time to run your first Python script! Follow these simple steps:
- Open your code editor (VS Code) and create a new file.
- In the new file, type the following Python code:Save the file with a .py extension (for example, hello.py). If you're using VS Code, simply click the Run button (play button) which is at top right corner.python
print("Hello, world!")
print("Hello, world!")
- Congratulations! You've executed your first Python program.
That's it! You’re all set to start coding in Python.
Happy coding!
Frequently Asked Questions
Is Python already installed on Linux?
Is Python already installed on Linux?
Many Linux distributions come with Python pre-installed. You can check by running python3 --version
in the terminal.
What's the difference between python and python3?
What's the difference between python and python3?
On some systems, python
refers to Python 2.x, while python3
points to Python 3.x. Since Python 2 is outdated, you should always use python3
for new projects.
How do I update Python to the latest version?
How do I update Python to the latest version?
Use your distribution’s package manager. For example, on Ubuntu: sudo apt update && sudo apt upgrade python3
. For cutting-edge versions, consider compiling from source or using pyenv
.
Why should I use pip?
Why should I use pip?
pip allows you to easily install and manage Python libraries and tools from the Python Package Index (PyPI). It's an essential tool for working with third-party modules.
Can I use another editor besides VS Code?
Can I use another editor besides VS Code?
Absolutely. Editors like PyCharm, Sublime Text, or even Vim work great for Python development. VS Code is popular because it's lightweight and beginner-friendly.