How to Install Python 3.12 on Ubuntu Using a Virtual Environment (Without Breaking Anything!)
If you want to use Python 3.12 on Ubuntu but don’t want to mess up your system or break important tools, you're in the right place! In this blog, we’ll show you how to safely install Python 3.12 without making it the system default, using something called a virtual environment. This will allow you to use Python 3.12 for your projects while keeping the system Python version (the one Ubuntu uses) intact.
Let’s get started!
Why Should You Use a Virtual Environment?
Before we jump into the installation steps, it’s important to understand why we recommend using a virtual environment.
No Risk of Breaking the System: Ubuntu uses Python for system tools like
apt
(the package manager), and changing the system Python version could break these tools. By using a virtual environment, you avoid affecting anything on the system.Keep Your Projects Organized: Virtual environments allow you to use different Python versions for different projects. This way, you can have one project running Python 3.12 and another project using an older version, all without conflict.
Easy to Manage: Virtual environments are easy to set up and remove. You can create and destroy them as needed, making it simple to manage different Python setups.
Step 1: Install Python 3.12
The first thing we need to do is install Python 3.12 on your system. But don't worry—this won't change your default system Python!
Update Your System: Before installing anything, it's always good to make sure your system is up-to-date. Open your terminal and run these commands:
bashCopy codesudo apt update sudo apt upgrade -y
This will update all of your installed software and packages.
Add the Deadsnakes PPA: The latest versions of Python (like Python 3.12) may not be available in Ubuntu's default repositories, so we will use the Deadsnakes PPA. This PPA is a trusted source that offers newer versions of Python.
To add it, run:
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update
Install Python 3.12: Now, you can install Python 3.12 by running:
sudo apt install python3.12
Verify Python 3.12 Installation: To make sure Python 3.12 is installed correctly, check the version by running:
python3.12 --version
You should see something like:
Python 3.12.x
Now, Python 3.12 is installed, but we haven’t changed the default version yet. This is a safe installation because Ubuntu will still use its default Python version for system tasks.
Step 2: Create a Virtual Environment for Python 3.12
Now that Python 3.12 is installed, let’s create a virtual environment so we can use Python 3.12 without affecting anything else on your system.
Create a Virtual Environment: Choose a folder where you want to keep your virtual environments. For example, let's create a folder called
myenv
:mkdir ~/myenv cd ~/myenv
Now, create the virtual environment using Python 3.12:
python3.12 -m venv mypythonenv
This will create a folder called
mypythonenv
insidemyenv
, which contains a clean Python environment.Activate the Virtual Environment: To use the Python 3.12 environment, you need to activate it. Run:
source mypythonenv/bin/activate
After activation, your terminal prompt will change to show that you are inside the virtual environment. It will look something like this:
(mypythonenv) user@ubuntu:~/myenv$
Now, any Python command you run will use Python 3.12 from the virtual environment, not the system Python.
Step 3: Install Packages in Your Virtual Environment
Now that you’re inside your virtual environment, you can install any Python packages you need, and they will only affect this environment—not your system.
For example, to install the popular requests
package, you can run:
pip install requests
To check that it’s installed, run:
pip list
This will show you the list of packages installed in your virtual environment.
Step 4: Deactivate the Virtual Environment
Once you’re done working in the virtual environment, you can deactivate it with the following command:
deactivate
This will return you to the system Python environment, and any Python commands you run will now use the default Python version again.
Step 5: Managing Multiple Virtual Environments
You can create multiple virtual environments for different projects, each with its own Python version and dependencies. For example, if you want another project using Python 3.9, you can create another virtual environment with that version:
python3.9 -m venv myprojectenv
Activate it as needed:
source myprojectenv/bin/activate
Conclusion
By using a virtual environment, you can easily use Python 3.12 on Ubuntu without worrying about breaking your system tools or package managers. You can create and manage multiple Python environments for different projects, and everything stays organized and isolated.
This method keeps your system Python safe, allows you to use the latest Python versions, and ensures that your projects have the exact dependencies they need.
If you ever want to remove a virtual environment, simply delete the folder:
rm -rf mypythonenv
That's it! Now you can work with Python 3.12 on Ubuntu without any risk of breaking your system.