Python comes pre-installed on most Ubuntu systems, but you may need to install a newer version or set up the development tools

Ubuntu includes Python 3 by default, though the version varies depending on your Ubuntu release. If you need a specific version, want the latest release, or plan to write and test code, you will need to install additional packages beyond what comes built-in. The fastest route is the Ubuntu package manager, which handles dependencies automatically. If you need a version not in Ubuntu's repositories, you can compile from source, though that takes longer and requires more setup.

Before you start, open a terminal. You can do this by pressing Ctrl+Alt+T on most Ubuntu desktop systems, or by searching for "Terminal" in your applications menu. All the commands below run in the terminal.

Key Takeaways

  • Check what version of Python you already have by typing python3 --version in the terminal.
  • Install Python and development tools from Ubuntu's repositories using sudo apt update followed by sudo apt install python3 python3-pip python3-venv.
  • The package manager method works for most people and takes a few minutes; compiling from source is only necessary if you need a version Ubuntu does not offer.
  • After installation, verify it worked by typing python3 to open the interactive shell, then type exit() to close it.

Check what version of Python you already have

Open your terminal and type the following command:

python3 --version

Press Enter. The terminal will show you the version number — for example, "Python 3.10.12" or "Python 3.11.4". If you see a version number and it is recent enough for what you need, you may already be done. Many Python projects work with any version from 3.8 onward.

If the terminal says "command not found" or shows a version older than 3.8, continue to the next section. If you see a version that works for you, you can skip to the section on installing pip and venv, which are the tools you actually use to manage code and packages.

Install Python 3 and development tools using apt

This is the standard method for Ubuntu. Type these commands one at a time, pressing Enter after each:

sudo apt update

This refreshes Ubuntu's list of available packages. It will ask for your password — type it and press Enter. You will not see the characters as you type, which is normal.

Next, type:

sudo apt install python3 python3-pip python3-venv

Press Enter. The terminal will show you how much disk space this will use and ask "Do you want to continue? [Y/n]". Type Y and press Enter. The installation will take a few minutes depending on your internet speed.

Here is what each package does: python3 is the interpreter itself. python3-pip is the package manager — the tool you use to read and install libraries other people have written. python3-venv lets you create isolated environments so different projects do not interfere with each other's packages.

Install a specific Python version from Ubuntu repositories

If you need a version newer than what apt install python3 provides, Ubuntu's repositories may still have it. First, check what versions are available:

apt-cache search python3 | grep "^python3\."

This shows all Python 3 versions in the repositories. Look for the version you need — for example, python3.11 or python3.12. If you see it, install it alongside your existing Python:

sudo apt install python3.11

Replace "3.11" with whichever version you found. You can have multiple versions installed at the same time. To run a specific version, type python3.11 instead of python3.

Compile Python from source if you need a version Ubuntu does not offer

If the version you need is not in Ubuntu's repositories, you can read and compile it yourself. This takes longer and requires build tools, but it works for any recent Python release.

First, install the build dependencies:

sudo apt install build-essential libssl-dev libffi-dev python3-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget

Next, read the Python source code. Go to python.org/downloads and find the version you need. Copy the read link for the gzipped source tarball. Then in your terminal, type:

cd /tmp

wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

Replace the URL with the actual link you copied. Once the read finishes, extract it:

tar -xzf Python-3.12.0.tgz

cd Python-3.12.0

Now configure and compile:

./configure --enable-optimizations

make -j $(nproc)

This will take several minutes. Once it finishes, install it:

sudo make altinstall

Use altinstall instead of install — it prevents this version from overwriting your system Python. You can then run it as python3.12 (or whichever version you compiled).

Verify the installation and test Python

After installation, confirm it worked. Type:

python3 --version

You should see the version number. Now open the Python interactive shell to make sure it actually runs:

python3

The terminal will change to show >>> and a cursor. This is the Python prompt. Type:

print("Hello, Python")

Press Enter. Python will print Hello, Python back to you. Type:

exit()

You are back at your normal terminal prompt. This confirms Python is installed and working.

Set up pip and virtual environments for your projects

Now that Python is installed, you need pip (the package manager) and venv (the tool for isolated project environments). If you installed using apt, both came with the packages above. Verify pip works:

pip3 --version

You should see a version number. Before you start a new project, create a virtual environment so packages for that project do not affect your system Python. Create a folder for your project:

mkdir my_project

cd my_project

Then create the virtual environment:

python3 -m venv venv

set up it:

source venv/bin/set up

Your terminal prompt will change to show (venv) at the start. Now when you use pip to install packages, they go into this project's environment only. When you are done working, type deactivate to exit the virtual environment.

Frequently Asked Questions

Do I need to uninstall the old Python before installing a new one?

No. You can have multiple versions installed at the same time. The system Python (usually the oldest version) stays in place, and newer versions coexist with it. You run a specific version by typing its full name — python3.11 instead of python3.

What is the difference between python3 and python?

On most modern Ubuntu systems, python3 is available but python (without the 3) may not be. Python 2 is no longer maintained, so Ubuntu removed it from newer releases. Always use python3.

Why do I need a virtual environment?

Virtual environments isolate each project's packages so they do not conflict. If one project needs version 1.0 of a library and another needs version 2.0, virtual environments let both run on the same computer without breaking each other.

Can I remove Python after I install it?

You can remove it with sudo apt remove python3, but this may break Ubuntu system tools that depend on Python. It is safer to leave it installed and just not use it if you do not need it.

What should I do if the installation fails?

The most common cause is a missing dependency. Run sudo apt update again, then try the install command again. If you are compiling from source, check that all build tools installed correctly by running the build-essential install command a second time.