In this article, we’ll take a look at how to install Python in Alpine Linux with and without docker support.
- Alpine Linux
- Sudo access
What is Alpine Linux?
Alpine is an independent general-purpose Linux distribution that is primarily designed for users who demand security, simplicity, and resource efficiency. Unlike other distributions, Alpine uses less storage space which can be a maximum of 140 MB. Also, with security factors in mind, Alpine compiles all userland binaries as Position Independent Executables (PIE) which makes it secure from zero-day attacks. The Alpine Linux Wiki contains the user handbook for Alpine configuration.
Refer to the official documentation for installing Alpine Linux on Windows, MacOS, and Unix operating systems.
Installing Python in Alpine Without Docker Image
Step 1 : Check preinstalled python package
The Alpine Linux package comes with a Python library pre-installed in the package library. You can check the version of the installed Python package with ‘version’ command.
python -- version //or python -- V //or python -- VV
In case you perform a manual installation of the Alpine Linux, there is a chance the Python package will not be installed, and you can get the error as shown in the above image. This flaw has been corrected in the newer updated versions.
Step 2 : Install python package
If you get the above output, it means you don’t have Python installed in your directory. But worry not! installing python is a piece of cake in Linux! The following command could be used to install the latest version of Python on the Linux system.
sudo apt-get install python3
In case you want to install a specific version of Python, mention it after the Python string with the version you want to install.
sudo apt-get install python3.6 //or sudo apt-get install python3.8 //or sudo apt-get install python3.9
Step 3 : Create a Softlink
Softlinks are used as shortcuts, instead of browsing through the file manager, shortcuts make it easier to point out a file or directory. Similarly, the softlink points to the shortcut path where you have saved the file or particular directory.
ln -sf <path to the file/folder to be linked> <the path of the link to be created>
Using this format, we can save the Python directory wherever we want in the file system.
ln -sf python3 /usr/bin/python
where ‘/usr/bin/python‘ is our storage path in the file, and ‘python3‘ is our created file/folder.
Step 4 : Install setup tools for pip3
pip3 is the official package manager and pip command for Python 3. It enables the installation and management of third-party software packages with features and functionality not found in the Python standard library. Pip3 installs packages from the PyPI (Python Package Index).
pip3 install --no-cache --upgrade pip setuptools
Installing Python with Docker Image
What is Docker?
Docker maintains the development lifecycle of software by allowing developers to work in standardized environments using local containers which provide your applications and services. Containers are efficient in continuous integration and continuous delivery (CI/CD) workflows. They provide a viable, cost-effective alternative to hypervisor-based virtual machines, so you can use more of your server capacity to achieve your business goals. Docker is perfect for high-density environments and for small and medium product deployments where businesses need to do more with fewer resources. You can read more about Docker configuration and working from their official documentation.
Step 1 : Python fresh install in docker
Most developers use Docker for working as it uses less storage and works efficiently. Similarly, Alpine and Docker work hand in hand for software deployments. For installing Python in Alpine with Docker image, you will need to make use of the apk – the alpine package manager.
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
Step 2 : Check the installed version
Check the version of the Python package installed in the docker file
python3 - -version
We saw how we can install the python3 package in Alpine Linux with and without Docker images. You can opt for any one of the methods to install the python3 package. As a beginner, you’ll need installation without docker support.
Hope this gives you an idea of the installation process. Follow the step-by-step procedure, and you’re good to go!