Are you ready to take advantage of the latest features in Python 3.12? This guide will walk you through the process of installing Python 3.12.8 on Ubuntu 22.04.4 LTS, setting it as the default version, and getting started with coding right away.
🛠️ Install Required Dependencies
Before we can compile and install Python 3.12, we need to make sure all the necessary tools and libraries are installed on your system. This step is crucial as it ensures that the Python installation process runs smoothly.
Run the following commands to update your package list and install the required dependencies:
sudo apt update && sudo apt install -y build-essential libssl-dev zlib1g-dev libncurses5-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
📥 Download, Extract, and Navigate to the Directory
Next, we’ll download the Python 3.12.8 source code, extract the files, and navigate to the directory where the source code is located. These steps are essential to prepare the environment for building Python from source.
wget https://www.python.org/ftp/python/3.12.8/Python-3.12.8.tgz && tar -zxvf Python-3.12.8.tgz && cd Python-3.12.8
🏗️ Configure, Compile, and Install Python
With the source code ready, it’s time to configure the build environment, compile the code, and install Python 3.12. This step ensures that Python is optimized for your specific system.
./configure --enable-optimizations && make -j$(nproc) && sudo make altinstall
🔗 Set Up Symbolic Links
To make Python 3.12 the default version, you’ll need to create symbolic links. This will allow you to run Python and pip without specifying the full version number each time.
sudo ln -sf /usr/local/bin/python3.12 /usr/bin/python3 && sudo ln -sf /usr/local/bin/pip3.12 /usr/bin/pip3
✅ Verify Your Installation
Now that Python 3.12 is installed, let’s verify that everything is working properly. Run the following commands to check the installed versions of Python and pip:
python3 --version && pip3 --version
You can also test the installation by installing a Python package, such as tqdm
:
pip3 install tqdm
📝 Running Your First Python Script
With Python 3.12 installed, you can immediately start writing and executing Python scripts. Let’s create a simple “Hello, World!” script to get started:
- Open your favorite text editor and create a new file named
hello.py
. - Add the following code to the file:
- Save the file and exit the editor.
- Run the script using Python 3.12:
print("Hello, World!")
python3 hello.py
You should see the output:
Hello, World!
🎉 Congratulations! You have successfully installed Python 3.12.8 on your Ubuntu 22.04.4 LTS system and run your first Python script. Happy coding! 💻✨