Installing a custom version of Python on Ubuntu can enhance your development environment, allowing you to work with specific project requirements. This guide will walk you through the installation process, setting up pip
, and adding Python to your PATH
.
Step 1: Update Your System
Before installing Python, ensure your system is updated. Open your terminal and run:
sudo apt update
sudo apt upgrade
Step 2: Install Required Dependencies
Install the required packages for building Python from source:
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev
Step 3: Download Python Source Code
Visit the official Python website to find the version you want to install. You can also use wget
to download it directly. Replace X.Y.Z
with your desired version:
wget https://www.python.org/ftp/python/X.Y.Z/Python-X.Y.Z.tgz
Step 4: Extract the Downloaded File
Extract the downloaded tarball:
tar -xvf Python-X.Y.Z.tgz
cd Python-X.Y.Z
Step 5: Configure the Build
Configure the build environment. You can specify the installation path if needed (e.g., /usr/local
):
./configure --enable-optimizations
Step 6: Compile and Install
Compile the source code and install Python. The -j
option can speed up the process by using multiple cores:
make -j $(nproc)
sudo make altinstall
Note: Using make altinstall
prevents overwriting the default python3
binary.
Step 7: Install pip
Once Python is installed, you can install pip
(the package installer for Python) using the following command:
wget https://bootstrap.pypa.io/get-pip.py
sudo /usr/local/bin/pythonX.Y get-pip.py
Replace X.Y
with the version number you installed (e.g., 3.10
).
Step 8: Add Python to PATH
To ensure that your new Python installation and pip
are accessible from anywhere, you need to add their directories to your PATH
. Open your ~/.bashrc
file:
nano ~/.bashrc
Add the following lines at the end of the file:
export PATH="/usr/local/bin/pythonX.Y:$PATH"
Replace X.Y
with your installed version (e.g., 3.10
). Save and exit (CTRL + X
, then Y
, then ENTER
).
Step 9: Apply the Changes
To apply the changes made to your ~/.bashrc
, run:
source ~/.bashrc
Step 10: Verify the Installation
Check if Python and pip
are installed correctly by running:
pythonX.Y --version
pip --version
Conclusion
You have successfully installed a custom version of Python on Ubuntu, along with pip
, and configured your PATH
. You can now start developing your Python projects with your desired version.
Leave a Reply