How to Install Python on Ubuntu?
So, you’ve decided to have Python on your Ubuntu system? Great choice!. In this tutorial, we’ll show you different methods to install Python on Ubuntu
system, ensuring you have everything you need to start your Python coding journey.
Before we get started, let’s make sure you have a few things in place:
- A working Ubuntu system.
- A stable internet connection to download the necessary packages.
- Basic knowledge of using the terminal/command line.
Let’s dive in!
Step 1: Update System Packages
First things first, let’s make sure your system is up to date. Open the terminal and enter the following command:
sudo apt update
This command updates the package lists for upgrades and new package installations. You may need to enter your password to proceed.
Step 2: Install Python
Ubuntu comes with Python pre-installed, but it’s essential to ensure that you have the latest version or the specific version you need for your projects. To install Python, enter the following command:
sudo apt install python3
This command installs Python 3
. The terminal may prompt you to confirm the installation by typing ‘y
‘ and pressing Enter. Once the installation is complete, you can verify the installation by running:
python3 --version
You should see the Python version number displayed in the output.
Step 3: Installing Additional Tools (Optional)
Depending on your Python development needs, you might want to install some additional tools and packages. Here are a couple of examples:
Installing pip
Pip is a package manager for Python that allows you to install and manage additional libraries and dependencies easily. To install pip, use the following command:
sudo apt install python3-pip
Installing Virtualenv
Virtualenv enables you to create isolated Python environments for different projects. It’s a useful tool for managing dependencies and keeping your project environments separate. Install Virtualenv by running:
pip3 install virtualenv
Step 4: Testing the Installation
To ensure that Python is installed correctly, let’s write a simple “Hello, World!
” program. Open a text editor and create a file named hello.py
. Insert the following code:
Save the file and navigate to the directory where it’s located using the terminal. To execute the script, run the following command:
python3 hello.py
If everything is set up correctly, you should see Hello, World!
printed in the terminal. Congratulations! You have successfully installed Python on your Ubuntu system. Now, let’s take it a step further and explore an alternative method for installing Python using pyenv.
Install Python on Ubuntu Using pyenv
Let’s dive in and continue our Python installation journey!
Step 1: Install Dependencies
To ensure a successful installation of pyenv, we need to install the necessary dependencies. Open the terminal and execute the following command:
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
This command will install the required dependencies for pyenv to function correctly. If prompted, enter your password to proceed.
Step 2: Install pyenv
With the dependencies in place, we can now proceed to install pyenv. Run the following commands one by one:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init --path)"\nfi' >> ~/.bashrc exec "$SHELL"
These commands clone the pyenv
repository from GitHub, add the necessary environment variables to your bashrc file, and reload the shell to apply the changes.
Step 3: Configure pyenv
Now that pyenv is installed, let’s configure it to use the latest Python versions available. Run the following commands:
pyenv install --list
This command lists all the available Python versions that you can install using pyenv. Choose the version you want (e.g., 3.9.6) and install it by running:
pyenv install 3.9.6
Replace 3.9.6
with the desired Python version. The installation process may take a few minutes, depending on your system and internet speed.
Step 4: Set Global and Local Python Versions
Once the installation is complete, set the global Python version that will be used by default:
pyenv global 3.9.6
Additionally, if you want to set a specific Python version for a particular project, navigate to the project’s directory in the terminal and run:
pyenv local 3.9.6
This command sets the local Python version for that specific project, overriding the global version.
Step 5: Test Python Installation
To verify that Python is installed correctly using pyenv
, let’s write a simple “Hello, pyenv!
” program. Create a file named hello.py
using a text editor and insert the following code:
Save the file and execute the script by running:
python hello.py
If everything is set up correctly, you should see Hello, pyenv!
printed in the terminal.
Where will pyenv install Python?
When you use pyenv to install Python on your system, it installs Python versions in the directory specific to pyenv. By default, pyenv installs Python versions within the ~/.pyenv/versions/
directory. Each installed Python version will have its own subdirectory within this directory.
For example, if you install Python 3.9 using pyenv, it will be installed in the following directory:
~/.pyenv/versions/3.9.0/
This structure allows pyenv to manage multiple Python versions on your system and easily switch between them when needed. You can also install different versions of Python side by side and create isolated virtual environments using tools like pyenv-virtualenv.
Note that the exact path may vary depending on your system configuration and how you have set up pyenv.
Installing Python on Ubuntu Using Deadsnakes
If you’re looking to install specific versions of Python on your Ubuntu system, deadsnakes
provides a convenient solution. The deadsnakes PPA (Personal Package Archive) allows you to install and manage different Python versions with ease.
Let’s dive into the steps and install Python on Ubuntu using deadsnakes!
Step 1: Add the deadsnakes PPA
To get started, we need to add the deadsnakes PPA to our system. Follow these instructions:
- Open a terminal by pressing
Ctrl + Alt + T
. - Run the following command to add the deadsnakes PPA:
sudo add-apt-repository ppa:deadsnakes/ppa
- Enter your password when prompted and press Enter to confirm adding the PPA.
- The system will update the package lists, and you will see some information about the PPA being added.
Step 2: Update Package Lists
Before installing Python, let’s update the package lists to ensure we have the latest information. Run the following command in the terminal:
sudo apt update
This command will fetch the latest package information from the added PPA and other repositories.
Step 3: Install Python
Now that the deadsnakes PPA is added, we can install Python using the package manager. Follow these instructions:
- In the terminal, run the following command to install Python:
sudo apt install pythonX.X
Replace X.X
with the desired version of Python you want to install. For example, to install Python 3.9, you would run sudo apt install python3.9
.
- Confirm the installation by typing Y and pressing Enter.
- The package manager will download and install the specified version of Python on your Ubuntu system.
Step 4: Verify the Python Installation
To verify that Python is installed correctly, open a terminal and run the following command:
pythonX.X --version
Replace X.X
with the version you installed. For example, to check the version of Python 3.9, you would run python3.9 --version
.
If the installation was successful, the terminal should display the installed Python version.
Step 5: Install pip
Pip is the package installer for Python, and it is often used to install additional Python packages and libraries. To install pip, run the following command:
sudo apt install pythonX.X-pip
Replace X.X
with the Python version you installed. For example, to install pip for Python 3.9, you would run sudo apt install python3.9-pip
.
Congratulations! You have explored three different methods for installing Python on Ubuntu: using the official repository, utilizing pyenv for version management, and leveraging deadsnakes to install specific Python versions.
By installing Python through the official repository, you ensure a stable and reliable version that is well-maintained and supported by the Ubuntu community. This method is recommended for most users who prefer a hassle-free installation process and easy access to the latest stable Python release.
For those seeking more flexibility and control over their Python environment, using pyenv
allows you to manage multiple Python versions effortlessly. With pyenv
, you can switch between different Python versions
, create isolated environments, and experiment with various libraries
and packages
without affecting your system’s default Python installation.
If you require specific Python versions that are not available in the official Ubuntu repositories, deadsnakes
comes to the rescue. The deadsnakes PPA provides a convenient solution for installing and managing different Python versions on Ubuntu
. It enables you to work with the Python version of your choice and take advantage of specific features and enhancements available in those releases.
Now that you have a comprehensive understanding of the installation methods, choose the one that suits your needs best and embark on your Python journey with confidence. Happy coding with Python on Ubuntu!