Python is one of the most popular programming languages in the world—favored for its readability, simplicity, and vast ecosystem of libraries and tools. Whether you're building web applications, automating tasks, analyzing data, or exploring machine learning, Python provides the tools and clarity to help you move fast and write clean code.
In this guide, we’ll walk through how to install Python on Linux, manage versions with pyenv or system packages, and use virtual environments to isolate your project dependencies. We’ll also show you how to use pip, write your first script, and set up an IDE or text editor to streamline your Python development workflow.
Before installing Python, make sure your system has the basic development tools required to build and manage Python environments. These include build tools, libraries, and a package manager like apt
on Debian-based systems.
Install the essentials with:
sudo apt update sudo apt install -y build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev curl \ llvm libncursesw5-dev xz-utils tk-dev libxml2-dev \ libxmlsec1-dev libffi-dev liblzma-dev
Ubuntu often comes with Python pre-installed. However, you may want a newer version or more control over versions. To check what's installed:
python3 --version
If you need to install or upgrade:
sudo apt install python3 python3-pip
This installs Python 3 and pip, the Python package manager.
pyenv is excellent for managing multiple Python versions. It lets you switch versions globally or per-project. To install it:
curl https://pyenv.run | bash
Then add the following lines to your ~/.bashrc, ~/.zshrc, or equivalent:
export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init --path)" eval "$(pyenv virtualenv-init -)"
Restart your shell and install a version:
pyenv install 3.12.1 pyenv global 3.12.1
Alternatively, venv (included with Python 3.3+) lets you isolate project dependencies:
python3 -m venv myenv source myenv/bin/activate
Use pyenv for managing Python versions, and venv for per-project environments.
To verify that Python and pip were installed correctly, run the following commands in your terminal:
python3 --version pip3 --version
You should see output like Python 3.12.1 and a corresponding pip version. If either command fails, revisit your installation steps or check your PATH configuration.
Python uses pip
to install packages. For isolated, user-level installs of CLI tools, pipx
is also useful. First, ensure pip is up to date:
python3 -m pip install --upgrade pip
Install pipx
with:
python3 -m pip install --user pipx python3 -m pipx ensurepath
To create a virtual environment for a project, use:
python3 -m venv venv source venv/bin/activate
Once activated, packages installed with pip stay isolated from the system Python environment. Deactivate with:
deactivate
The Python shell—also known as the REPL (Read-Eval-Print Loop)—lets you try out Python code interactively. Start it with:
python3
You’ll enter a prompt like:
>>>>
Type Python expressions directly and see their results. Exit the REPL with exit() or by pressing Ctrl+D.
To write your first Python script, create a new file called hello.py using your favorite text editor:
print("Hello, world!")
Save the file and run it from the terminal:
python3 hello.py
This should output: Hello, world!
Popular editors for Python include VS Code, PyCharm, Sublime Text, and even Vim. Make sure your editor is configured to:
For VS Code, install the Python extension by Microsoft and configure your interpreter via the Command Palette (Ctrl+Shift+P → “Python: Select Interpreter”).
Start by creating a directory for your project:
mkdir my_project cd my_project
Set up a virtual environment:
python3 -m venv venv source venv/bin/activate
Create your Python script(s) inside the directory. If your project will have dependencies, create a requirements.txt file to list them, then install with:
pip install -r requirements.txt
To build your Python skills, start with the official tutorial at docs.python.org/3/tutorial/. Other great resources include:
You can also explore Python’s vast ecosystem of libraries, from web frameworks like Flask and Django to data tools like NumPy and pandas. Don't forget to check out our Code Visualization course!