Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install another version of python on Linux?

There already exists a version(2.6) of python on my linux server. I want to install another version(maybe 2.7 or 3) to another directory(maybe "/home/zhangxudong") and then run my python script using this new python. How can I do the above through command line? Very thanks.

like image 275
zhangxudong Avatar asked Mar 09 '26 11:03

zhangxudong


2 Answers

well I encountered the same situation. I have python3.9.1 installed in my linux machine. But I required python3.8 or lower. The way that worked for me goes like this...

  1. download the python package you want in the directory of your choice.
  2. unzip the folder and cd into it.
  3. run the commands sequentially
  • sudo ./configure --enable-optimizations --prefix=/opt/Python3.8.7 --exec-prefix=/opt/Python3.8.7

here /opt/Python3.8.7 is the directory path in which new python is to be installed. --enable-optimizations is optional.

  • sudo make

  • sudo make altinstall

this is important because if you use sudo make install it will mess up with your previously installed python

now to check this python version go to the directory in which you installed python and directly run the python executable in the bin directory.

/opt/python3.8.7/bin/python --version

do chmod +x <executable> if necessary.

further you may create a virtual environment to work with the new python /opt/python3.8.7/bin/python -m venv ~/Desktop/my_env

source ~/Desktop/my_env/bin/activate

now check the python version by doing python --version

like image 173
Albatrozz Avatar answered Mar 10 '26 23:03

Albatrozz


First of all, I'm just pointing out your question is awkward, there are many ways to do this. You are probably looking to install python from source (google it) which can be done to a particular director or by using virtual environments (also google it). If you just want say python3 installed, you can do this easily.

Get prerequisites:

sudo apt-get install python-setuptools python-dev build-essential

Note: This is specific to Ubuntu and other Debian distros, you can use a built in package manager or install in the distro of your choice by replacing apt-get.

Now install python 3

sudo apt-get install python3

You can also use VirtualEnv or Docker which create virtual instances on your machine. They are handy but a little involved to setup.

http://docs.python-guide.org/en/latest/dev/virtualenvs/

Alternatively, you could use Pip to install different Python interpreters such as pypy once prerequisites are met. The nice thing about this is once the python and setuptools are installed, it's consistent between OS's, including Windows:

pip -U install pypy

specific to a version of python

python3 -m pip -U install pypy

P.S. If you have access to a graphical desktop, I'd suggest using PyCharm where you can switch between versions of Python (python2, python3, cython, pypy, etc) on the fly. This requires a little bit of setup and learning but it isn't bad at all.

Good luck!

like image 31
jacktrader Avatar answered Mar 10 '26 23:03

jacktrader



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!